Class SerializationUtils

java.lang.Object
org.moeaframework.util.SerializationUtils

public class SerializationUtils extends Object
Utility methods for serialization, primarily to assist in serializing collections in a type-safe manner and avoiding unchecked casts.
  • 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 values
      T - the return type
      Parameters:
      type - the expected type of each element in the list
      generator - a supplier responsible for creating an empty list
      object - 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 serialize
      stream - 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 values
      T - the return type
      Parameters:
      type - the expected type of each element in the list
      generator - a supplier responsible for creating an empty list
      stream - the object stream
      Returns:
      the typed list
      Throws:
      IOException - if an error occurred while writing the object
      ClassNotFoundException - if any type being serialized could not be found
    • castMap

      public static final <K extends Serializable, V extends Serializable, T extends Map<K, V>> T castMap(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 keys
      V - the type of the values
      T - the return type
      Parameters:
      keyType - the expected type of each key
      valueType - the expected type of each value
      generator - a supplier responsible for creating an empty map
      object - 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 IOException
      Writes the given map to the object stream.
      Type Parameters:
      K - the type of the keys
      V - the type of the values
      Parameters:
      map - the map to serialize
      stream - the object stream
      Throws:
      IOException - if an error occurred while writing the object
    • readMap

      public static final <K extends Serializable, V extends Serializable, T extends Map<K, V>> T readMap(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 keys
      V - the type of the values
      T - the return type
      Parameters:
      keyType - the expected type of each key
      valueType - the expected type of each value
      generator - a supplier responsible for creating an empty map
      stream - the object stream
      Returns:
      the typed map
      Throws:
      IOException - if an error occurred while writing the object
      ClassNotFoundException - if any type being deserialized could not be found