Package com.esotericsoftware.kryo.util
Class MapReferenceResolver
- java.lang.Object
-
- com.esotericsoftware.kryo.util.MapReferenceResolver
-
- All Implemented Interfaces:
ReferenceResolver
public class MapReferenceResolver extends Object implements ReferenceResolver
Uses anIdentityObjectIntMapto track objects that have already been written. This can handle a graph with any number of objects, but is slightly slower thanListReferenceResolverfor graphs with few objects. Compared toHashMapReferenceResolver, this may provide better performance since the IdentityObjectIntMap does not normally allocate for get or put.- Author:
- Nathan Sweet
-
-
Field Summary
Fields Modifier and Type Field Description protected Kryokryoprotected ArrayList<Object>readObjectsprotected IdentityObjectIntMap<Object>writtenObjects
-
Constructor Summary
Constructors Constructor Description MapReferenceResolver()Creates a reference resolver with a default maximum capacity of 2048MapReferenceResolver(int maximumCapacity)Creates a reference resolver with the specified maximum capacity.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intaddWrittenObject(Object object)Returns a new ID for an object that is being written for the first time.ObjectgetReadObject(Class type, int id)Returns the object for the specified ID.intgetWrittenId(Object object)Returns an ID for the object if it has been written previously, otherwise returns -1.intnextReadId(Class type)Reserves the ID for the next object that will be read.voidreset()Called byKryo.reset().voidsetKryo(Kryo kryo)Sets the Kryo instance that this ClassResolver will be used for.voidsetReadObject(int id, Object object)Sets the ID for an object that has been read.booleanuseReferences(Class type)Returns false for all primitive wrappers and enums.
-
-
-
Field Detail
-
kryo
protected Kryo kryo
-
writtenObjects
protected final IdentityObjectIntMap<Object> writtenObjects
-
-
Constructor Detail
-
MapReferenceResolver
public MapReferenceResolver()
Creates a reference resolver with a default maximum capacity of 2048
-
MapReferenceResolver
public MapReferenceResolver(int maximumCapacity)
Creates a reference resolver with the specified maximum capacity. The default value of 2048 is good enough in most cases. If the average object graph is larger than the default, increasing this value can provide better performance.- Parameters:
maximumCapacity- the capacity to trim written and read objects to whenreset()is called
-
-
Method Detail
-
setKryo
public void setKryo(Kryo kryo)
Description copied from interface:ReferenceResolverSets the Kryo instance that this ClassResolver will be used for. This is called automatically by Kryo.- Specified by:
setKryoin interfaceReferenceResolver
-
addWrittenObject
public int addWrittenObject(Object object)
Description copied from interface:ReferenceResolverReturns a new ID for an object that is being written for the first time.- Specified by:
addWrittenObjectin interfaceReferenceResolver- Returns:
- The ID, which is stored more efficiently if it is positive and must not be -1 or -2.
-
getWrittenId
public int getWrittenId(Object object)
Description copied from interface:ReferenceResolverReturns an ID for the object if it has been written previously, otherwise returns -1.- Specified by:
getWrittenIdin interfaceReferenceResolver
-
nextReadId
public int nextReadId(Class type)
Description copied from interface:ReferenceResolverReserves the ID for the next object that will be read. This is called only the first time an object is encountered.- Specified by:
nextReadIdin interfaceReferenceResolver- 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.
-
setReadObject
public void setReadObject(int id, Object object)Description copied from interface:ReferenceResolverSets the ID for an object that has been read.- Specified by:
setReadObjectin interfaceReferenceResolver- Parameters:
id- The ID fromReferenceResolver.nextReadId(Class).
-
getReadObject
public Object getReadObject(Class type, int id)
Description copied from interface:ReferenceResolverReturns the object for the specified ID. The ID and object are guaranteed to have been previously passed in a call toReferenceResolver.setReadObject(int, Object).- Specified by:
getReadObjectin interfaceReferenceResolver
-
reset
public void reset()
Description copied from interface:ReferenceResolverCalled byKryo.reset().- Specified by:
resetin interfaceReferenceResolver
-
useReferences
public boolean useReferences(Class type)
Returns false for all primitive wrappers and enums.- Specified by:
useReferencesin interfaceReferenceResolver- Parameters:
type- Will never be a primitive type, but may be a primitive type wrapper.
-
-