gov.sandia.cognition.collection
Class DefaultIndexer<ValueType>

java.lang.Object
  extended by gov.sandia.cognition.util.AbstractCloneableSerializable
      extended by gov.sandia.cognition.collection.DefaultIndexer<ValueType>
Type Parameters:
ValueType - The type of the value being indexed. Must have a valid equals and hashCode implementation.
All Implemented Interfaces:
Indexer<ValueType>, CloneableSerializable, Serializable, Cloneable

public class DefaultIndexer<ValueType>
extends AbstractCloneableSerializable
implements Indexer<ValueType>

A default implementation of the Indexer interface that simply maps objects to a range from 0 to n-1 in the order they are given. It stores the list of objects plus a mapping of values to indices. This implementation is not synchronized.

Since:
3.3.3
Author:
Justin Basilico
See Also:
Indexer, Serialized Form

Field Summary
protected  ArrayList<ValueType> valueList
          The list of values, which can be accessed by index.
protected  LinkedHashMap<ValueType,Integer> valueToIndexMap
          The mapping of values to their indices.
 
Constructor Summary
DefaultIndexer()
          Creates a new, empty DefaultIndexer.
DefaultIndexer(Collection<? extends ValueType> values)
          Creates a new DefaultIndexer and adds he given collection of values to it.
DefaultIndexer(int initialCapacity)
          Creates a new, empty DefaultIndexer with the given initial capacity.
 
Method Summary
 Integer add(ValueType value)
          Adds a value to the indexer and returns the index assigned to the value.
 void addAll(Iterable<? extends ValueType> values)
          Adds all of the given values to the index.
 Map<ValueType,Integer> asMap()
          Gets mapping of values to indices.
 DefaultIndexer<ValueType> clone()
          This makes public the clone method on the Object class and removes the exception that it throws.
 Integer getIndex(ValueType value)
          Gets the index associated with the value.
 ValueType getValue(int index)
          Gets the value associated with the given index.
 ValueType getValue(Integer index)
          Gets the value associated with the given index.
 boolean hasIndex(int index)
          Determines if the given index is valid for this indexer, which means it has a value associated with it.
 boolean hasIndex(Integer index)
          Determines if the given index is valid for this indexer, which means it has a value associated with it.
 boolean hasValue(ValueType value)
          Determines if the given value is known to the indexer and has an index.
 boolean isEmpty()
          Returns true if this indexer is empty, which means it has no values.
 int size()
          Gets the number of items in the index.
 List<ValueType> valueList()
          Gets the list of values in the index.
 Set<ValueType> valueSet()
          Gets the set of values in the index.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

valueList

protected ArrayList<ValueType> valueList
The list of values, which can be accessed by index.


valueToIndexMap

protected LinkedHashMap<ValueType,Integer> valueToIndexMap
The mapping of values to their indices.

Constructor Detail

DefaultIndexer

public DefaultIndexer()
Creates a new, empty DefaultIndexer.


DefaultIndexer

public DefaultIndexer(int initialCapacity)
Creates a new, empty DefaultIndexer with the given initial capacity.

Parameters:
initialCapacity - The initial capacity for the indexer. Must be positive.

DefaultIndexer

public DefaultIndexer(Collection<? extends ValueType> values)
Creates a new DefaultIndexer and adds he given collection of values to it.

Parameters:
values - The initial set of values to add to the indexer.
Method Detail

clone

public DefaultIndexer<ValueType> clone()
Description copied from class: AbstractCloneableSerializable
This makes public the clone method on the Object class and removes the exception that it throws. Its default behavior is to automatically create a clone of the exact type of object that the clone is called on and to copy all primitives but to keep all references, which means it is a shallow copy. Extensions of this class may want to override this method (but call super.clone() to implement a "smart copy". That is, to target the most common use case for creating a copy of the object. Because of the default behavior being a shallow copy, extending classes only need to handle fields that need to have a deeper copy (or those that need to be reset). Some of the methods in ObjectUtil may be helpful in implementing a custom clone method. Note: The contract of this method is that you must use super.clone() as the basis for your implementation.

Specified by:
clone in interface CloneableSerializable
Overrides:
clone in class AbstractCloneableSerializable
Returns:
A clone of this object.

add

public Integer add(ValueType value)
Description copied from interface: Indexer
Adds a value to the indexer and returns the index assigned to the value. If the value is already in the index, then its existing index will be returned.

Specified by:
add in interface Indexer<ValueType>
Parameters:
value - The value to add.
Returns:
The index assigned to the value.

addAll

public void addAll(Iterable<? extends ValueType> values)
Description copied from interface: Indexer
Adds all of the given values to the index.

Specified by:
addAll in interface Indexer<ValueType>
Parameters:
values - The values to add.

getIndex

public Integer getIndex(ValueType value)
Description copied from interface: Indexer
Gets the index associated with the value.

Specified by:
getIndex in interface Indexer<ValueType>
Parameters:
value - The value to get the index of.
Returns:
The index of the given value or null if there is no such index.

getValue

public ValueType getValue(int index)
Description copied from interface: Indexer
Gets the value associated with the given index. Will throw an exception if the index is invalid.

Specified by:
getValue in interface Indexer<ValueType>
Parameters:
index - The index to get.
Returns:
The value at the index.

getValue

public ValueType getValue(Integer index)
Description copied from interface: Indexer
Gets the value associated with the given index. Will throw an exception if the index is invalid.

Specified by:
getValue in interface Indexer<ValueType>
Parameters:
index - The index to get.
Returns:
The value at the index.

hasValue

public boolean hasValue(ValueType value)
Description copied from interface: Indexer
Determines if the given value is known to the indexer and has an index.

Specified by:
hasValue in interface Indexer<ValueType>
Parameters:
value - The value.
Returns:
True if the value has a valid index; otherwise, false.

hasIndex

public boolean hasIndex(int index)
Description copied from interface: Indexer
Determines if the given index is valid for this indexer, which means it has a value associated with it.

Specified by:
hasIndex in interface Indexer<ValueType>
Parameters:
index - The index.
Returns:
True if the index has a valid value; otherwise, false.

hasIndex

public boolean hasIndex(Integer index)
Description copied from interface: Indexer
Determines if the given index is valid for this indexer, which means it has a value associated with it.

Specified by:
hasIndex in interface Indexer<ValueType>
Parameters:
index - The index.
Returns:
True if the index has a valid value; otherwise, false.

isEmpty

public boolean isEmpty()
Description copied from interface: Indexer
Returns true if this indexer is empty, which means it has no values.

Specified by:
isEmpty in interface Indexer<ValueType>
Returns:
True if the indexer is empty, which means it has no values and its size is zero.

size

public int size()
Description copied from interface: Indexer
Gets the number of items in the index.

Specified by:
size in interface Indexer<ValueType>
Returns:
The number of items in the index.

valueSet

public Set<ValueType> valueSet()
Description copied from interface: Indexer
Gets the set of values in the index.

Specified by:
valueSet in interface Indexer<ValueType>
Returns:
The set of values.

valueList

public List<ValueType> valueList()
Description copied from interface: Indexer
Gets the list of values in the index.

Specified by:
valueList in interface Indexer<ValueType>
Returns:
The list of values in the index.

asMap

public Map<ValueType,Integer> asMap()
Description copied from interface: Indexer
Gets mapping of values to indices.

Specified by:
asMap in interface Indexer<ValueType>
Returns:
The mapping of values to indices.