fulmine.util.reference
Class AutoCreatingStore<KEY,TYPE>

java.lang.Object
  extended by fulmine.util.reference.AutoCreatingStore<KEY,TYPE>
Type Parameters:
KEY - The type of the key used to access objects in this store
TYPE - The type of canonical objects this returns
All Implemented Interfaces:
IDestroyable, IAutoCreatingStore<KEY,TYPE>

public final class AutoCreatingStore<KEY,TYPE>
extends Object
implements IAutoCreatingStore<KEY,TYPE>

This implementation manages a Map of objects against their key. When the store is requested for an object, identified by its key, it returns the cached object. The store uses an internal IObjectBuilder to create instances of objects that do not exist.

This is thread safe; all methods are synchronized and return a copy of the collection where appropriate.

Author:
Ramon Servadei

Constructor Summary
AutoCreatingStore(IObjectBuilder<KEY,TYPE> builder)
          Standard constructor
 
Method Summary
 void clear()
          Clear all keys and values from the store.
 boolean containsKey(KEY key)
          Find out if there is an object held against the key.
 void destroy()
          This is called when the object is being removed from application use.
 boolean equals(Object obj)
           
 TYPE get(KEY key)
          Get the object for the key.
 int hashCode()
           
 Set<KEY> keySet()
          Get a copy collection of all the keys in this store.
 TYPE remove(KEY key)
          Remove the object associated with the key.
 String toString()
           
 Collection<TYPE> values()
          Get a copy collection of the values held in this store.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AutoCreatingStore

public AutoCreatingStore(IObjectBuilder<KEY,TYPE> builder)
Standard constructor

Parameters:
builder - the builder to use when constructing new canonical object instances
Method Detail

get

public TYPE get(KEY key)
Description copied from interface: IAutoCreatingStore
Get the object for the key. This will create an instance of the object on the first call with the key. Keys are matched based on the Object.equals(Object) method.

Specified by:
get in interface IAutoCreatingStore<KEY,TYPE>
Parameters:
key - the key representation of the object
Returns:
an object that is the same object for all subsequent requests of this key argument

remove

public TYPE remove(KEY key)
Description copied from interface: IAutoCreatingStore
Remove the object associated with the key. The object is no longer held in this store. Subsequent calls to IAutoCreatingStore.get(Object) for the same key may create a new object instance. Keys are matched based on the Object.equals(Object) method.

Specified by:
remove in interface IAutoCreatingStore<KEY,TYPE>
Parameters:
key - the key identifying the object that will be removed
Returns:
the object that is associated with the key argument, or null if there was no object

containsKey

public boolean containsKey(KEY key)
Description copied from interface: IAutoCreatingStore
Find out if there is an object held against the key. Keys are matched based on the Object.equals(Object) method.

Specified by:
containsKey in interface IAutoCreatingStore<KEY,TYPE>
Parameters:
key - the key to find
Returns:
true if there is an object held against the key

clear

public void clear()
Description copied from interface: IAutoCreatingStore
Clear all keys and values from the store.

Specified by:
clear in interface IAutoCreatingStore<KEY,TYPE>

keySet

public Set<KEY> keySet()
Description copied from interface: IAutoCreatingStore
Get a copy collection of all the keys in this store.

Specified by:
keySet in interface IAutoCreatingStore<KEY,TYPE>
Returns:
a copy set of the keys in this store.

values

public Collection<TYPE> values()
Description copied from interface: IAutoCreatingStore
Get a copy collection of the values held in this store.

Specified by:
values in interface IAutoCreatingStore<KEY,TYPE>
Returns:
a copy collection of the values held in this store.

destroy

public void destroy()
Description copied from interface: IDestroyable
This is called when the object is being removed from application use. This releases all system resources and performs clean up prior to the object becoming redundant. This operation is idempotent.

This method must not throw a RuntimeException.

Specified by:
destroy in interface IDestroyable

toString

public String toString()
Overrides:
toString in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Overrides:
equals in class Object


Copyright © 2007-2009. All Rights Reserved.