com.esotericsoftware.kryo.util
Class MapReferenceResolver

java.lang.Object
  extended by com.esotericsoftware.kryo.util.MapReferenceResolver
All Implemented Interfaces:
ReferenceResolver

public class MapReferenceResolver
extends java.lang.Object
implements ReferenceResolver

Uses an IdentityObjectIntMap to track objects that have already been written. This can handle graph with any number of objects, but is slightly slower than ListReferenceResolver for graphs with few objects.


Field Summary
protected  Kryo kryo
           
protected  java.util.ArrayList readObjects
           
protected  IdentityObjectIntMap writtenObjects
           
 
Constructor Summary
MapReferenceResolver()
           
 
Method Summary
 void addReadObject(int id, java.lang.Object object)
          Adds an object that has been read for the first time.
 int addWrittenObject(java.lang.Object object)
          Returns a new ID for an object that is being written for the first time.
 java.lang.Object getReadObject(java.lang.Class type, int id)
          Returns the object for the specified ID.
 int getWrittenId(java.lang.Object object)
          Returns an ID for the object if it has been written previously, otherwise returns -1.
 int nextReadId(java.lang.Class type)
          Returns the ID for the next object that will be read.
 void reset()
          Called by Kryo.reset().
 void setKryo(Kryo kryo)
          Sets the Kryo instance that this ClassResolver will be used for.
 boolean useReferences(java.lang.Class type)
          Returns false for all primitive wrappers.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

kryo

protected Kryo kryo

writtenObjects

protected final IdentityObjectIntMap writtenObjects

readObjects

protected final java.util.ArrayList readObjects
Constructor Detail

MapReferenceResolver

public MapReferenceResolver()
Method Detail

setKryo

public void setKryo(Kryo kryo)
Description copied from interface: ReferenceResolver
Sets the Kryo instance that this ClassResolver will be used for. This is called automatically by Kryo.

Specified by:
setKryo in interface ReferenceResolver

addWrittenObject

public int addWrittenObject(java.lang.Object object)
Description copied from interface: ReferenceResolver
Returns a new ID for an object that is being written for the first time.

Specified by:
addWrittenObject in interface ReferenceResolver
Returns:
The ID, which is stored more efficiently if it is positive and must not be -1 or -2.

getWrittenId

public int getWrittenId(java.lang.Object object)
Description copied from interface: ReferenceResolver
Returns an ID for the object if it has been written previously, otherwise returns -1.

Specified by:
getWrittenId in interface ReferenceResolver

nextReadId

public int nextReadId(java.lang.Class type)
Description copied from interface: ReferenceResolver
Returns the ID for the next object that will be read. This is called only the first time an object is encountered.

Specified by:
nextReadId in interface ReferenceResolver
Parameters:
type - The type of object that will be read.
Returns:
The ID, which is stored more efficiently if it is positive and must not be -1 or -2.

addReadObject

public void addReadObject(int id,
                          java.lang.Object object)
Description copied from interface: ReferenceResolver
Adds an object that has been read for the first time.

Specified by:
addReadObject in interface ReferenceResolver
Parameters:
id - The ID from ReferenceResolver.nextReadId(Class).

getReadObject

public java.lang.Object getReadObject(java.lang.Class type,
                                      int id)
Description copied from interface: ReferenceResolver
Returns the object for the specified ID. The ID and object are guaranteed to have been previously passed in a call to ReferenceResolver.addReadObject(int, Object).

Specified by:
getReadObject in interface ReferenceResolver

reset

public void reset()
Description copied from interface: ReferenceResolver
Called by Kryo.reset().

Specified by:
reset in interface ReferenceResolver

useReferences

public boolean useReferences(java.lang.Class type)
Returns false for all primitive wrappers.

Specified by:
useReferences in interface ReferenceResolver
Parameters:
type - Will never be a primitive type, but may be a primitive type wrapper.