Package org.moeaframework.util
Class SerializationUtils
java.lang.Object
org.moeaframework.util.SerializationUtils
Utility methods for serialization, primarily to assist in serializing collections in a type-safe manner and avoiding
unchecked casts.
-
Method Summary
Modifier and TypeMethodDescriptionstatic final <V extends Serializable,
T extends List<V>>
TCasts an object, typically produced via deserialization, to a typed list.static final <K extends Serializable,
V extends Serializable, T extends Map<K, V>>
TCasts a map with the wildcard<?, ?>
type to a typed map, ensuring each element is if the correct type.static final <V extends Serializable,
T extends List<V>>
TreadList
(Class<V> type, Supplier<T> generator, ObjectInputStream stream) Reads a list from the object stream.static final <K extends Serializable,
V extends Serializable, T extends Map<K, V>>
TreadMap
(Class<K> keyType, Class<V> valueType, Supplier<T> generator, ObjectInputStream stream) Reads a map from the object stream.static final <V extends Serializable>
voidwriteList
(List<V> list, ObjectOutputStream stream) Writes the given list to the object stream.static final <K extends Serializable,
V extends Serializable>
voidwriteMap
(Map<K, V> map, ObjectOutputStream stream) Writes the given map to the object stream.
-
Method Details
-
castList
public static final <V extends Serializable,T extends List<V>> T castList(Class<V> type, Supplier<T> generator, Object object) Casts an object, typically produced via deserialization, to a typed list.- Type Parameters:
V
- the type of the valuesT
- the return type- Parameters:
type
- the expected type of each element in the listgenerator
- a supplier responsible for creating an empty listobject
- the object, which is expected to be a list- Returns:
- the typed list
- Throws:
ClassCastException
- if the object is not a list, or any element is not the required type
-
writeList
public static final <V extends Serializable> void writeList(List<V> list, ObjectOutputStream stream) throws IOException Writes the given list to the object stream.- Type Parameters:
V
- the type of the values- Parameters:
list
- the list to serializestream
- the object stream- Throws:
IOException
- if an error occurred while writing the object
-
readList
public static final <V extends Serializable,T extends List<V>> T readList(Class<V> type, Supplier<T> generator, ObjectInputStream stream) throws IOException, ClassNotFoundException Reads a list from the object stream.- Type Parameters:
V
- the type of the valuesT
- the return type- Parameters:
type
- the expected type of each element in the listgenerator
- a supplier responsible for creating an empty liststream
- the object stream- Returns:
- the typed list
- Throws:
IOException
- if an error occurred while writing the objectClassNotFoundException
- if any type being serialized could not be found
-
castMap
public static final <K extends Serializable,V extends Serializable, T castMapT extends Map<K, V>> (Class<K> keyType, Class<V> valueType, Supplier<T> generator, Object object) Casts a map with the wildcard<?, ?>
type to a typed map, ensuring each element is if the correct type.- Type Parameters:
K
- the type of the keysV
- the type of the valuesT
- the return type- Parameters:
keyType
- the expected type of each keyvalueType
- the expected type of each valuegenerator
- a supplier responsible for creating an empty mapobject
- the object, which is expected to be a map- Returns:
- the typed map
- Throws:
ClassCastException
- if the object is not a map, or any element is not the required type
-
writeMap
public static final <K extends Serializable,V extends Serializable> void writeMap(Map<K, V> map, ObjectOutputStream stream) throws IOExceptionWrites the given map to the object stream.- Type Parameters:
K
- the type of the keysV
- the type of the values- Parameters:
map
- the map to serializestream
- the object stream- Throws:
IOException
- if an error occurred while writing the object
-
readMap
public static final <K extends Serializable,V extends Serializable, T readMapT extends Map<K, V>> (Class<K> keyType, Class<V> valueType, Supplier<T> generator, ObjectInputStream stream) throws IOException, ClassNotFoundException Reads a map from the object stream.- Type Parameters:
K
- the type of the keysV
- the type of the valuesT
- the return type- Parameters:
keyType
- the expected type of each keyvalueType
- the expected type of each valuegenerator
- a supplier responsible for creating an empty mapstream
- the object stream- Returns:
- the typed map
- Throws:
IOException
- if an error occurred while writing the objectClassNotFoundException
- if any type being deserialized could not be found
-