public class CollectionSerializer<T extends java.util.Collection> extends Serializer<T>
Collection
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.
Modifier and Type | Class and Description |
---|---|
static interface |
CollectionSerializer.BindCollection
|
Constructor and Description |
---|
CollectionSerializer() |
Modifier and Type | Method and 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 by
read(Kryo, Input, Class) to create the new object. |
protected T |
createCopy(Kryo kryo,
T original)
Used by
copy(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 both
setElementClass(Class) and setElementSerializer(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 with
Kryo 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 for
create(Kryo, Input, Class, int) . |
getAcceptsNull, isImmutable, setAcceptsNull, setImmutable
public void setElementsCanBeNull(boolean elementsCanBeNull)
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).public void setElementClass(java.lang.Class elementClass)
public java.lang.Class getElementClass()
public void setElementClass(java.lang.Class elementClass, Serializer serializer)
setElementClass(Class)
and setElementSerializer(Serializer)
.public void setElementSerializer(Serializer elementSerializer)
Kryo
for each
element's type. Default is null.public Serializer getElementSerializer()
public void write(Kryo kryo, Output output, T collection)
Serializer
This method should not be called directly, instead this serializer can be passed to Kryo
write methods that accept a
serialier.
write
in class Serializer<T extends java.util.Collection>
collection
- May be null if Serializer.getAcceptsNull()
is true.protected void writeHeader(Kryo kryo, Output output, T collection)
create(Kryo, Input, Class, int)
. The default implementation does
nothing.protected T create(Kryo kryo, Input input, java.lang.Class<? extends T> type, int size)
read(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 in writeHeader(Kryo, Output, Collection)
.
The default implementation uses Kryo.newInstance(Class)
with special cases for ArrayList.public T read(Kryo kryo, Input input, java.lang.Class<? extends T> type)
Serializer
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 uses Kryo
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.
read
in class Serializer<T extends java.util.Collection>
Serializer.getAcceptsNull()
is true.protected T createCopy(Kryo kryo, T original)
copy(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 uses Kryo.newInstance(Class)
.public T copy(Kryo kryo, T original)
Serializer
Serializer.isImmutable()
is true,
else throws KryoException
. Subclasses can optionall override this method to support Kryo.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 uses Kryo
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.
copy
in class Serializer<T extends java.util.Collection>
Copyright © 2018. All Rights Reserved.