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 theCollection
interface.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 interface
CollectionSerializer.BindCollection
-
Constructor Summary
Constructors Constructor Description CollectionSerializer()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description T
copy(Kryo kryo, T original)
Returns a copy of the specified object.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.protected T
createCopy(Kryo kryo, T original)
Used bycopy(Kryo, Collection)
to create the new object.java.lang.Class
getElementClass()
Serializer
getElementSerializer()
T
read(Kryo kryo, Input input, java.lang.Class<? extends T> type)
Reads bytes and returns a new object of the specified concrete type.void
setElementClass(java.lang.Class elementClass)
The concrete class of the collection elements, or null if it is not known.void
setElementClass(java.lang.Class elementClass, Serializer serializer)
Sets bothsetElementClass(Class)
andsetElementSerializer(Serializer)
.void
setElementsCanBeNull(boolean elementsCanBeNull)
void
setElementSerializer(Serializer elementSerializer)
The serializer to be used for elements in collection, or null to use the serializer registered withKryo
for each element's type.void
write(Kryo kryo, Output output, T collection)
Writes the bytes for the object to the output.protected void
writeHeader(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 withKryo
for 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:Serializer
Writes the bytes for the object to the output.This method should not be called directly, instead this serializer can be passed to
Kryo
write methods that accept a serialier.- Specified by:
write
in 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 and HashSet.
-
read
public T read(Kryo kryo, Input input, java.lang.Class<? extends T> type)
Description copied from class:Serializer
Reads 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 usesKryo
to read a child object may need to be reentrant.This method should not be called directly, instead this serializer can be passed to
Kryo
read methods that accept a serialier.- Specified by:
read
in 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:Serializer
Returns 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 usesKryo
to copy a child object may need to be reentrant.This method should not be called directly, instead this serializer can be passed to
Kryo
copy methods that accept a serialier.- Overrides:
copy
in classSerializer<T extends java.util.Collection>
-
-