com.google.common.collect
Class Serialization

java.lang.Object
  extended by com.google.common.collect.Serialization

public final class Serialization
extends Object

Provides static method for serializing collection classes.

Author:
Jared Levy

Method Summary
static
<K,V> void
populateMap(Map<K,V> map, ObjectInputStream stream)
          Populates a map by reading an input stream, as part of deserialization.
static
<K,V> void
populateMultimap(Multimap<K,V> multimap, ObjectInputStream stream)
          Populates a multimap by reading an input stream, as part of deserialization.
static
<E> void
populateMultiset(Multiset<E> multiset, ObjectInputStream stream)
          Populates a multiset by reading an input stream, as part of deserialization.
static
<T> void
setFinalField(Class<T> clazz, T instance, String fieldName, Object value)
          Updates the value of a final field, to support field initialization during deserialization.
static
<K,V> void
writeMap(Map<K,V> map, ObjectOutputStream stream)
          Stores the contents of a map in an output stream, as part of serialization.
static
<K,V> void
writeMultimap(Multimap<K,V> multimap, ObjectOutputStream stream)
          Stores the contents of a multimap in an output stream, as part of serialization.
static
<E> void
writeMultiset(Multiset<E> multiset, ObjectOutputStream stream)
          Stores the contents of a multiset in an output stream, as part of serialization.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

writeMap

public static <K,V> void writeMap(Map<K,V> map,
                                  ObjectOutputStream stream)
                     throws IOException
Stores the contents of a map in an output stream, as part of serialization. It does not support concurrent maps whose content may change while the method is running.

The serialized output consists of the number of entries, first key, first value, second key, second value, and so on.

Throws:
IOException

populateMap

public static <K,V> void populateMap(Map<K,V> map,
                                     ObjectInputStream stream)
                        throws IOException,
                               ClassNotFoundException
Populates a map by reading an input stream, as part of deserialization. See writeMap(java.util.Map, java.io.ObjectOutputStream) for the data format.

Throws:
IOException
ClassNotFoundException

writeMultiset

public static <E> void writeMultiset(Multiset<E> multiset,
                                     ObjectOutputStream stream)
                          throws IOException
Stores the contents of a multiset in an output stream, as part of serialization. It does not support concurrent multisets whose content may change while the method is running.

The serialized output consists of the number of distinct elements, the first element, its count, the second element, its count, and so on.

Throws:
IOException

populateMultiset

public static <E> void populateMultiset(Multiset<E> multiset,
                                        ObjectInputStream stream)
                             throws IOException,
                                    ClassNotFoundException
Populates a multiset by reading an input stream, as part of deserialization. See writeMultiset(com.google.common.collect.Multiset, java.io.ObjectOutputStream) for the data format.

Throws:
IOException
ClassNotFoundException

writeMultimap

public static <K,V> void writeMultimap(Multimap<K,V> multimap,
                                       ObjectOutputStream stream)
                          throws IOException
Stores the contents of a multimap in an output stream, as part of serialization. It does not support concurrent multimaps whose content may change while the method is running. The Multimap.asMap() view determines the ordering in which data is written to the stream.

The serialized output consists of the number of distinct keys, and then for each distinct key: the key, the number of values for that key, and the key's values.

Throws:
IOException

populateMultimap

public static <K,V> void populateMultimap(Multimap<K,V> multimap,
                                          ObjectInputStream stream)
                             throws IOException,
                                    ClassNotFoundException
Populates a multimap by reading an input stream, as part of deserialization. See writeMultimap(com.google.common.collect.Multimap, java.io.ObjectOutputStream) for the data format.

Throws:
IOException
ClassNotFoundException

setFinalField

public static <T> void setFinalField(Class<T> clazz,
                                     T instance,
                                     String fieldName,
                                     Object value)
                          throws SecurityException,
                                 NoSuchFieldException
Updates the value of a final field, to support field initialization during deserialization.

Parameters:
clazz - the class in which the field is declared
instance - the instance containing the field to be updated
fieldName - the name of the field to update
value - the value to store in the field
Throws:
NoSuchFieldException - if no field has the specified name
SecurityException - if a security manager denies the request
IllegalArgumentException - if the value cannot be converted to the type of the field