Class WeakRegistry<K extends Serializable,V extends Serializable>

java.lang.Object
overit.geocall.util.WeakRegistry<K,V>
Type Parameters:
K - datatype associated to the keys
V - datatype associated to the values
All Implemented Interfaces:
com.esotericsoftware.kryo.KryoSerializable, Serializable

public class WeakRegistry<K extends Serializable,V extends Serializable> extends Object implements Serializable, com.esotericsoftware.kryo.KryoSerializable
The class has a very similar implementation of 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:
  • Field Details

  • Constructor Details

    • WeakRegistry

      public WeakRegistry()
      Creates a WeakRegistry instance
  • Method Details

    • putWeak

      public V putWeak(K name, V value)
      Inserts in the registry an entry that will be automatically removed when the value is dereferenced
      Parameters:
      name - The name of the key field
      value - The value associated to the key
      Returns:
      The value previously associated with the key, or null if the key / value mapping was not already present.
    • putStrong

      public V putStrong(K name, V value)
      Inserts in the registry a permanent entry
      Parameters:
      name - The name of the key field
      value - The value associated to the key
      Returns:
      The value previously associated with the key, or null if the key / value mapping was not already present.
    • putDependency

      public V putDependency(K name, V value, Serializable reference)
      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 field
      value - The value associated to the key
      reference - The object used as reference
      Returns:
      The value previously associated with the key, or null if the key / value mapping was not already present.
    • get

      public V get(K name)
      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 null if the entry is removed autonomously as a result of the garbage collector cycle
    • remove

      public V remove(K name)
      Removes the entry associated to the key
      Parameters:
      name - The name of the key field
      Returns:
      The value associated to the removed key or null if 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:
      write in interface com.esotericsoftware.kryo.KryoSerializable
    • read

      public void read(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Input input)
      Specified by:
      read in interface com.esotericsoftware.kryo.KryoSerializable
    • toString

      public String toString()
      Overrides:
      toString in class Object