Class WeakRegistry<K extends Serializable,V extends Serializable>
- Type Parameters:
K- datatype associated to the keysV- datatype associated to the values
- All Implemented Interfaces:
com.esotericsoftware.kryo.KryoSerializable,Serializable
WeakHashMap, with weak values.
An entry in the WeakRegistry will be automatically removed when its value is dereferenced;
more precisely, the presence of a key/ value mapping will not prevent the value from being cleaned up by
the garbage collector, which means that the relative entry is in turn definitively removed from the map
automatically.
The behaviour of the WaekRegistry class depends on the garbage collector actions, because it can delete
some values at any time. This class behaves as if it had a thread that silently removes certain entries.
So it is quite normal that the size() method returns smaller values with the passage of time, although
no methods are invoked to carry out insertions or removals on the registry.
The instances of this class enjoy the possibility of being serialized / deserialized; this implies, in turn, the fact that the
instances that can be added to the register must in turn be Serializable. For this reason it is appropriate to adopt
rules dictated by common sense, especially for the operations of population of the register. Particularly complex or heavy
objects increase the time necessary for register serialization and complexity in the subsequent deserialization phase.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classWeakRegistry.NamedReference<K extends Serializable,V extends Serializable> -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ReferenceQueue<WeakRegistry.NamedReference<K, V>> protected WeakHashMap<Serializable, HashSet<V>> protected HashMap<K, WeakRegistry.NamedReference<K, V>> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the value associated to a specific keyputDependency(K name, V value, Serializable reference) Inserts an entry in the registry whose "automatic removal" depends on one external reference.Inserts in the registry a permanent entryInserts in the registry an entry that will be automatically removed when the value is dereferencedvoidread(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Input input) Removes the entry associated to the keyintsize()Returns the number of entries of the registrytoString()voidwrite(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Output output)
-
Field Details
-
_registry
protected transient HashMap<K extends Serializable,WeakRegistry.NamedReference<K extends Serializable, _registryV extends Serializable>> -
_references
-
_queue
protected transient ReferenceQueue<WeakRegistry.NamedReference<K extends Serializable,V extends Serializable>> _queue
-
-
Constructor Details
-
WeakRegistry
public WeakRegistry()Creates a WeakRegistry instance
-
-
Method Details
-
putWeak
Inserts in the registry an entry that will be automatically removed when the value is dereferenced- Parameters:
name- The name of the key fieldvalue- The value associated to the key- Returns:
- The value previously associated with the key, or
nullif the key / value mapping was not already present.
-
putStrong
Inserts in the registry a permanent entry- Parameters:
name- The name of the key fieldvalue- The value associated to the key- Returns:
- The value previously associated with the key, or
nullif the key / value mapping was not already present.
-
putDependency
Inserts an entry in the registry whose "automatic removal" depends on one external reference. Specifically, the entry will remain in the register as long as the reference is not dereferenced.- Parameters:
name- The name of the key fieldvalue- The value associated to the keyreference- The object used as reference- Returns:
- The value previously associated with the key, or
nullif the key / value mapping was not already present.
-
get
Returns the value associated to a specific key- Parameters:
name- The name of the key field- Returns:
- The value associated to the key; it could return
nullif the entry is removed autonomously as a result of the garbage collector cycle
-
remove
Removes the entry associated to the key- Parameters:
name- The name of the key field- Returns:
- The value associated to the removed key or
nullif the key / value mapping was not present
-
size
public int size()Returns the number of entries of the registry- Returns:
- The number of entries of the registry. The method can return smaller values with the passage of time, although no methods are invoked to carry out insertions or removals on the registry.
-
write
public void write(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Output output) - Specified by:
writein interfacecom.esotericsoftware.kryo.KryoSerializable
-
read
public void read(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Input input) - Specified by:
readin interfacecom.esotericsoftware.kryo.KryoSerializable
-
toString
-