ValueType
- The type of the value being indexed. Must have a valid
equals and hashCode implementation.public class DefaultIndexer<ValueType> extends AbstractCloneableSerializable implements Indexer<ValueType>
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.Indexer
,
Serialized FormModifier and Type | Field and Description |
---|---|
protected java.util.ArrayList<ValueType> |
valueList
The list of values, which can be accessed by index.
|
protected java.util.LinkedHashMap<ValueType,java.lang.Integer> |
valueToIndexMap
The mapping of values to their indices.
|
Constructor and Description |
---|
DefaultIndexer()
Creates a new, empty
DefaultIndexer . |
DefaultIndexer(java.util.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. |
Modifier and Type | Method and Description |
---|---|
java.lang.Integer |
add(ValueType value)
Adds a value to the indexer and returns the index assigned to the value.
|
void |
addAll(java.lang.Iterable<? extends ValueType> values)
Adds all of the given values to the index.
|
java.util.Map<ValueType,java.lang.Integer> |
asMap()
Gets mapping of values to indices.
|
void |
clear()
Clears the contents of this index.
|
DefaultIndexer<ValueType> |
clone()
This makes public the clone method on the
Object class and
removes the exception that it throws. |
java.lang.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(java.lang.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(java.lang.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.
|
java.util.List<ValueType> |
valueList()
Gets the list of values in the index.
|
java.util.Set<ValueType> |
valueSet()
Gets the set of values in the index.
|
protected java.util.ArrayList<ValueType> valueList
protected java.util.LinkedHashMap<ValueType,java.lang.Integer> valueToIndexMap
public DefaultIndexer()
DefaultIndexer
.public DefaultIndexer(int initialCapacity)
DefaultIndexer
with the given initial
capacity.initialCapacity
- The initial capacity for the indexer. Must be
positive.public DefaultIndexer(java.util.Collection<? extends ValueType> values)
DefaultIndexer
and adds he given collection of
values to it.values
- The initial set of values to add to the indexer.public DefaultIndexer<ValueType> clone()
AbstractCloneableSerializable
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.clone
in interface CloneableSerializable
clone
in class AbstractCloneableSerializable
public java.lang.Integer add(ValueType value)
Indexer
public void addAll(java.lang.Iterable<? extends ValueType> values)
Indexer
public java.lang.Integer getIndex(ValueType value)
Indexer
public ValueType getValue(int index)
Indexer
public ValueType getValue(java.lang.Integer index)
Indexer
public boolean hasValue(ValueType value)
Indexer
public boolean hasIndex(int index)
Indexer
public boolean hasIndex(java.lang.Integer index)
Indexer
public boolean isEmpty()
Indexer
public int size()
Indexer
public java.util.Set<ValueType> valueSet()
Indexer
public java.util.List<ValueType> valueList()
Indexer
public java.util.Map<ValueType,java.lang.Integer> asMap()
Indexer