Class CollectionSerializer<T extends java.util.Collection>
- java.lang.Object
-
- com.esotericsoftware.kryo.Serializer<T>
-
- com.esotericsoftware.kryo.serializers.CollectionSerializer<T>
-
- Direct Known Subclasses:
DefaultSerializers.ArraysAsListSerializer,DefaultSerializers.PriorityQueueSerializer,DefaultSerializers.TreeSetSerializer
public class CollectionSerializer<T extends java.util.Collection> extends Serializer<T>
Serializes objects that implement theCollectioninterface.With the default constructor, a collection requires a 1-3 byte header and an extra 2-3 bytes is written for each element in the collection.
- Author:
- Nathan Sweet
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceCollectionSerializer.BindCollection
-
Constructor Summary
Constructors Constructor Description CollectionSerializer()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Tcopy(Kryo kryo, T original)Returns a copy of the specified object.protected Tcreate(Kryo kryo, Input input, java.lang.Class<? extends T> type, int size)Used byread(Kryo, Input, Class)to create the new object.protected TcreateCopy(Kryo kryo, T original)Used bycopy(Kryo, Collection)to create the new object.java.lang.ClassgetElementClass()SerializergetElementSerializer()Tread(Kryo kryo, Input input, java.lang.Class<? extends T> type)Reads bytes and returns a new object of the specified concrete type.voidsetElementClass(java.lang.Class elementClass)The concrete class of the collection elements, or null if it is not known.voidsetElementClass(java.lang.Class elementClass, Serializer serializer)Sets bothsetElementClass(Class)andsetElementSerializer(Serializer).voidsetElementsCanBeNull(boolean elementsCanBeNull)voidsetElementSerializer(Serializer elementSerializer)The serializer to be used for elements in collection, or null to use the serializer registered withKryofor each element's type.voidwrite(Kryo kryo, Output output, T collection)Writes the bytes for the object to the output.protected voidwriteHeader(Kryo kryo, Output output, T collection)Can be overidden to write data needed forcreate(Kryo, Input, Class, int).-
Methods inherited from class com.esotericsoftware.kryo.Serializer
getAcceptsNull, isImmutable, setAcceptsNull, setImmutable
-
-
-
-
Method Detail
-
setElementsCanBeNull
public void setElementsCanBeNull(boolean elementsCanBeNull)
- Parameters:
elementsCanBeNull- False if all elements are not null. This saves 1 byte per element if elementClass is set. True if it is not known (default).
-
setElementClass
public void setElementClass(java.lang.Class elementClass)
The concrete class of the collection elements, or null if it is not known. This saves 1-2 bytes per element. Only set to a non-null value if the elements in the collection are known to all be instances of this class (or null).
-
getElementClass
public java.lang.Class getElementClass()
-
setElementClass
public void setElementClass(java.lang.Class elementClass, Serializer serializer)Sets bothsetElementClass(Class)andsetElementSerializer(Serializer).
-
setElementSerializer
public void setElementSerializer(Serializer elementSerializer)
The serializer to be used for elements in collection, or null to use the serializer registered withKryofor each element's type. Default is null.
-
getElementSerializer
public Serializer getElementSerializer()
-
write
public void write(Kryo kryo, Output output, T collection)
Description copied from class:SerializerWrites the bytes for the object to the output.This method should not be called directly, instead this serializer can be passed to
Kryowrite methods that accept a serialier.- Specified by:
writein classSerializer<T extends java.util.Collection>collection- May be null ifSerializer.getAcceptsNull()is true.
-
writeHeader
protected void writeHeader(Kryo kryo, Output output, T collection)
Can be overidden to write data needed forcreate(Kryo, Input, Class, int). The default implementation does nothing.
-
create
protected T create(Kryo kryo, Input input, java.lang.Class<? extends T> type, int size)
Used byread(Kryo, Input, Class)to create the new object. This can be overridden to customize object creation (eg to call a constructor with arguments), optionally reading bytes written inwriteHeader(Kryo, Output, Collection). The default implementation usesKryo.newInstance(Class)with special cases for ArrayList.
-
read
public T read(Kryo kryo, Input input, java.lang.Class<? extends T> type)
Description copied from class:SerializerReads bytes and returns a new object of the specified concrete type.Before Kryo can be used to read child objects,
Kryo.reference(Object)must be called with the parent object to ensure it can be referenced by the child objects. Any serializer that usesKryoto read a child object may need to be reentrant.This method should not be called directly, instead this serializer can be passed to
Kryoread methods that accept a serialier.- Specified by:
readin classSerializer<T extends java.util.Collection>- Returns:
- May be null if
Serializer.getAcceptsNull()is true.
-
createCopy
protected T createCopy(Kryo kryo, T original)
Used bycopy(Kryo, Collection)to create the new object. This can be overridden to customize object creation, eg to call a constructor with arguments. The default implementation usesKryo.newInstance(Class).
-
copy
public T copy(Kryo kryo, T original)
Description copied from class:SerializerReturns a copy of the specified object. The default implementation returns the original ifSerializer.isImmutable()is true, else throwsKryoException. Subclasses can optionall override this method to supportKryo.copy(Object).Before Kryo can be used to copy child objects,
Kryo.reference(Object)must be called with the copy to ensure it can be referenced by the child objects. A serializer that usesKryoto copy a child object may need to be reentrant.This method should not be called directly, instead this serializer can be passed to
Kryocopy methods that accept a serialier.- Overrides:
copyin classSerializer<T extends java.util.Collection>
-
-