goldman.collection.tagged
Class TaggedCollectionWrapper<T,E>

java.lang.Object
  extended by goldman.collection.tagged.TaggedCollectionWrapper<T,E>
All Implemented Interfaces:
TaggedCollection<T,E>, Iterable<TaggedElement<T,E>>
Direct Known Subclasses:
DirectAddressingMapping, OpenAddressingMapping, SeparateChainingMapping, TaggedOrderedCollectionWrapper, TaggedPriorityQueueWrapper, TaggedSpatialCollectionWrapper

public class TaggedCollectionWrapper<T,E>
extends Object
implements TaggedCollection<T,E>

Rather than implement each of the tagged collections from scratch, we define a tagged collection wrapper that can be used to transform any untagged collection into a tagged collection. Each element in the collection is a tagged element. For a tagged collection to have the specified behavior, it is required that the equivalence tester over a tagged element be defined according to the tag.


Field Summary
protected  Collection<TaggedElement<T,E>> pairs
           
protected  MutableTaggedElement<T,E> target
           
 
Constructor Summary
TaggedCollectionWrapper(Collection<TaggedElement<T,E>> pairs)
           
 
Method Summary
 void accept(Visitor<? super TaggedElement<T,E>> v)
          Traverses the entire collection on behalf of a visitor.
 void clear()
          Removes all elements from this collection.
 boolean contains(T tag)
          Returns true if a tagged element with an equivalent tag exists in this collection.
 Iterator<E> elements()
          Returns an iterator over the elements that has been initialized to just before the first element in the iteration order.
 void ensureCapacity(int capacity)
          Increases the capacity of the collection, if necessary, to ensure that it can hold at least capacity elements.
 E get(T tag)
          Returns an element with the given tag.
 int getCapacity()
          Returns the current capacity of this tagged collection.
 Locator<TaggedElement<T,E>> getLocator(T tag)
          Returns a locator that has been initialized to a tagged element with an equivalent tag.
 int getSize()
          Returns the number of tagged elements, size, in this collection.
 boolean isEmpty()
          Returns true if this collection contains no elements, and otherwise returns false.
 Iterator<TaggedElement<T,E>> iterator()
          Creates a new iterator at FORE.
 void put(T tag, E data)
          Creates a new tagged element with the given values and inserts this tagged element into this collection.
 void putAll(TaggedCollection<T,E> tc)
          Adds all tagged elements in tc to this tagged collection.
 E remove(T tag)
          Removes some tagged element in this collection with an tag equivalent to the given tag
 Iterator<T> tags()
          Returns an iterator over the tags that has been initialized to just before the first tag in the iteration order.
 String toString()
          Returns a string that describes each tagged element in the collection, in the iteration order.
 void trimToSize()
          Trims the capacity of this collection to be its current size.
 Collection<E> values()
          This implementation creates a positional collection (specifically, an array), and the elements are inserted in the iteration order
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

pairs

protected Collection<TaggedElement<T,E>> pairs

target

protected MutableTaggedElement<T,E> target
Constructor Detail

TaggedCollectionWrapper

public TaggedCollectionWrapper(Collection<TaggedElement<T,E>> pairs)
Parameters:
pairs - the collection to wrap
Throws:
IllegalArgumentException - the provided collection is not empty
Method Detail

getCapacity

public int getCapacity()
Description copied from interface: TaggedCollection
Returns the current capacity of this tagged collection.

Specified by:
getCapacity in interface TaggedCollection<T,E>

getSize

public int getSize()
Description copied from interface: TaggedCollection
Returns the number of tagged elements, size, in this collection.

Specified by:
getSize in interface TaggedCollection<T,E>

isEmpty

public boolean isEmpty()
Description copied from interface: TaggedCollection
Returns true if this collection contains no elements, and otherwise returns false.

Specified by:
isEmpty in interface TaggedCollection<T,E>

contains

public boolean contains(T tag)
Description copied from interface: TaggedCollection
Returns true if a tagged element with an equivalent tag exists in this collection. Otherwise false is returned.

Specified by:
contains in interface TaggedCollection<T,E>
Parameters:
tag - the target tag
Returns:
true if and only if there is some tagged element in this collection with an equivalent tag to the target

get

public E get(T tag)
Description copied from interface: TaggedCollection
Returns an element with the given tag. This method requires that tag be in use. It throws a NoSuchElementException otherwise

Specified by:
get in interface TaggedCollection<T,E>
Parameters:
tag - the target tag
Returns:
a tagged element with an equivalent tag
Throws:
NoSuchElementException - there is no element with an equivalent tag

getLocator

public Locator<TaggedElement<T,E>> getLocator(T tag)
Description copied from interface: TaggedCollection
Returns a locator that has been initialized to a tagged element with an equivalent tag. Like the iterator method, this method enables navigation, but from a specified starting point. This method throws a NoSuchElementException if there is no tagged element with an equivalent tag in the collection.

Specified by:
getLocator in interface TaggedCollection<T,E>
Parameters:
tag - the target tag
Returns:
a locator that has been placed at a tagged element with an equivalent tag
Throws:
NoSuchElementException - there is no element with an equivalent tag

values

public Collection<E> values()
This implementation creates a positional collection (specifically, an array), and the elements are inserted in the iteration order

Specified by:
values in interface TaggedCollection<T,E>
Returns:
a collection of all elements in this tagged collection.

toString

public String toString()
Description copied from interface: TaggedCollection
Returns a string that describes each tagged element in the collection, in the iteration order.

Specified by:
toString in interface TaggedCollection<T,E>
Overrides:
toString in class Object
Returns:
a string that describes each tagged element in the collection

accept

public void accept(Visitor<? super TaggedElement<T,E>> v)
Traverses the entire collection on behalf of a visitor.

Specified by:
accept in interface TaggedCollection<T,E>
Parameters:
v - a visitor
Throws:
VisitAbortedException - the traversal is aborted due to an exception raised by the visitor, in which case the cause held by the VisitAbortedException is the exception thrown by the visitor.

ensureCapacity

public void ensureCapacity(int capacity)
Increases the capacity of the collection, if necessary, to ensure that it can hold at least capacity elements.

Specified by:
ensureCapacity in interface TaggedCollection<T,E>
Parameters:
capacity - the desired capacity for the collection

trimToSize

public void trimToSize()
Trims the capacity of this collection to be its current size. An application can use this operation to minimize space usage.

Specified by:
trimToSize in interface TaggedCollection<T,E>

put

public void put(T tag,
                E data)
Creates a new tagged element with the given values and inserts this tagged element into this collection.

Specified by:
put in interface TaggedCollection<T,E>
Parameters:
tag - the tag for the tagged element to add
data - the associated data
Throws:
AtCapacityException - the collection is already at capacity.

putAll

public void putAll(TaggedCollection<T,E> tc)
Description copied from interface: TaggedCollection
Adds all tagged elements in tc to this tagged collection.

Specified by:
putAll in interface TaggedCollection<T,E>
Parameters:
tc - the tagged collection to be added to this collection

remove

public E remove(T tag)
Removes some tagged element in this collection with an tag equivalent to the given tag

Specified by:
remove in interface TaggedCollection<T,E>
Parameters:
tag - the target tag
Returns:
the removed element
Throws:
NoSuchElementException - there is no element with an equivalent tag

clear

public void clear()
Removes all elements from this collection.

Specified by:
clear in interface TaggedCollection<T,E>

iterator

public Iterator<TaggedElement<T,E>> iterator()
Creates a new iterator at FORE.

Specified by:
iterator in interface TaggedCollection<T,E>
Specified by:
iterator in interface Iterable<TaggedElement<T,E>>

tags

public Iterator<T> tags()
Description copied from interface: TaggedCollection
Returns an iterator over the tags that has been initialized to just before the first tag in the iteration order.

Specified by:
tags in interface TaggedCollection<T,E>

elements

public Iterator<E> elements()
Description copied from interface: TaggedCollection
Returns an iterator over the elements that has been initialized to just before the first element in the iteration order. The iteration order of the elements is defined based on the iteration order of the associated tags.

Specified by:
elements in interface TaggedCollection<T,E>