org.jrabbit.base.data.structures
Class BaseKeyedContainer<T extends Referenced>

java.lang.Object
  extended by org.jrabbit.base.data.structures.BaseKeyedContainer<T>
All Implemented Interfaces:
java.lang.Iterable<T>, Factory<T>, Accepter<T>, Container<T>, KeyedContainer<T>, Remover<T>
Direct Known Subclasses:
Cache, OrderedKeyedContainer

public class BaseKeyedContainer<T extends Referenced>
extends java.lang.Object
implements KeyedContainer<T>

BaseKeyedContainer provides a standard implementation of KeyedContainer. This will NOT maintain order of addition - if that functionality is desirable, use OrderedKeyedContainer. Basically, this uses a HashMap to fulfill the methods in KeyedContainer.

Author:
Chris Molini

Field Summary
protected  java.util.HashMap<java.lang.String,T> contents
          The LinkedHashMap that organizes all objects in the Container.
protected  Factory<T> factory
          The Factory used to create objects.
 
Constructor Summary
BaseKeyedContainer(Factory<T> factory)
          Creates an OrderedBaseKeyedContainer that uses the supplied Factory to make objects on demand.
 
Method Summary
 void add(java.lang.String... references)
          Attempts to create and add objects corresponding to every supplied reference.
 boolean add(java.lang.String reference)
          Creates an object from the indicated expression and attempts to add it.
 void add(T... objects)
          Attempts to add every supplied object.
 boolean add(T object)
          Adds an object, and checks whether the add was successful.
 void clear()
          Removes all objects from the container.
 boolean contains(java.lang.String reference)
          Checks to see if the Container contains an object with an identical reference to the one given.
 boolean contains(T object)
          Checks to see if the object is contained in the Container.
 T create(java.lang.String reference)
          Creates an object from the supplied reference.
protected  java.util.HashMap<java.lang.String,T> createMap()
          Obtains the HashMap used to maintain the list.
 Factory<T> factory()
          Accesses the object used to create new entries upon demand.
 T get(java.lang.String reference)
          Attempts to access the object in the list with the given reference.
 java.util.Iterator<T> iterator()
          Accesses an iterator through the list of entries.
 java.util.Collection<T> remove(java.lang.String... references)
          Attempts to remove and return all objects in the Container that correspond to the supplied references.
 T remove(java.lang.String reference)
          Attempts to remove the object with the indicated Reference from the list.
 void remove(T... objects)
          Removes a series of objects.
 boolean remove(T object)
          Removes an object.
 void setFactory(Factory<T> factory)
          Redefines the OrderedBaseKeyedContainer's Factory.
 int size()
          Learns how many objects are held.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

factory

protected Factory<T extends Referenced> factory
The Factory used to create objects.


contents

protected java.util.HashMap<java.lang.String,T extends Referenced> contents
The LinkedHashMap that organizes all objects in the Container.

Constructor Detail

BaseKeyedContainer

public BaseKeyedContainer(Factory<T> factory)
Creates an OrderedBaseKeyedContainer that uses the supplied Factory to make objects on demand.

Parameters:
factory - The Factory used to create entries on the fly, from String data.
Method Detail

createMap

protected java.util.HashMap<java.lang.String,T> createMap()
Obtains the HashMap used to maintain the list.

Returns:
A HashMap that will key Objects to Strings.

add

public boolean add(java.lang.String reference)
Creates an object from the indicated expression and attempts to add it.

Specified by:
add in interface KeyedContainer<T extends Referenced>
Parameters:
reference - The String to identify the new object in the list.
Returns:
True if the addition operation succeeded, false if not.

add

public void add(java.lang.String... references)
Attempts to create and add objects corresponding to every supplied reference.

Specified by:
add in interface KeyedContainer<T extends Referenced>
Parameters:
references - The Strings to identify the new objects in the list.

add

public boolean add(T object)
Adds an object, and checks whether the add was successful.

Specified by:
add in interface Accepter<T extends Referenced>
Parameters:
object - The object to add.
Returns:
Whether or not the add was successful.

add

public void add(T... objects)
Attempts to add every supplied object.

Specified by:
add in interface Accepter<T extends Referenced>
Parameters:
objects - The objects to add.

remove

public T remove(java.lang.String reference)
Attempts to remove the object with the indicated Reference from the list.

Specified by:
remove in interface KeyedContainer<T extends Referenced>
Parameters:
reference - The String that identifies the object in the list.
Returns:
If the object was removed, returns a reference to it, if the removal failed, returns null.

remove

public java.util.Collection<T> remove(java.lang.String... references)
Attempts to remove and return all objects in the Container that correspond to the supplied references.

Specified by:
remove in interface KeyedContainer<T extends Referenced>
Parameters:
references - The Strings indicating which objects to remove.
Returns:
A Collection containing all objects removed. If no objects are removed successfully, an empty Collection is returned.

remove

public boolean remove(T object)
Removes an object.

Specified by:
remove in interface Remover<T extends Referenced>
Parameters:
object - The object to remove.
Returns:
True if the operation succeeded, false if not.

remove

public void remove(T... objects)
Removes a series of objects.

Specified by:
remove in interface Remover<T extends Referenced>
Parameters:
objects - The objects to remove.

contains

public boolean contains(java.lang.String reference)
Checks to see if the Container contains an object with an identical reference to the one given.

Specified by:
contains in interface KeyedContainer<T extends Referenced>
Parameters:
reference - The String that identifies the object in the list.
Returns:
If the object is in the list, returns a true, otherwise, false.

contains

public boolean contains(T object)
Checks to see if the object is contained in the Container.

Specified by:
contains in interface Container<T extends Referenced>
Parameters:
object - The object to look for.
Returns:
True if the object is in the Container, false if not.

get

public T get(java.lang.String reference)
Attempts to access the object in the list with the given reference.

Specified by:
get in interface KeyedContainer<T extends Referenced>
Parameters:
reference - The String that identifies the object in the list.
Returns:
If the object is in the list, returns a reference to it. Otherwise, returns null.

clear

public void clear()
Removes all objects from the container.

Specified by:
clear in interface Container<T extends Referenced>

size

public int size()
Learns how many objects are held.

Specified by:
size in interface Container<T extends Referenced>
Returns:
The size of the Container (number of objects stored).

create

public T create(java.lang.String reference)
Creates an object from the supplied reference. This uses the OrderedBaseKeyedContainer's personal Factory to make the object.

Specified by:
create in interface Factory<T extends Referenced>
Parameters:
reference - The information we use to define the object.
Returns:
The object built from the String's data.

factory

public Factory<T> factory()
Accesses the object used to create new entries upon demand.

Returns:
The Factory that makes objects for the OrderedBaseKeyedContainer.

setFactory

public void setFactory(Factory<T> factory)
Redefines the OrderedBaseKeyedContainer's Factory.

Parameters:
factory - The new Factory for the Container to use.

iterator

public java.util.Iterator<T> iterator()
Accesses an iterator through the list of entries.

Specified by:
iterator in interface java.lang.Iterable<T extends Referenced>
Returns:
An Iterator that will access the contents of the list.