Package beagleutil
Class ThreadSafeIndexer<T>
- java.lang.Object
-
- beagleutil.ThreadSafeIndexer<T>
-
- Type Parameters:
T
- the type parameter.
public final class ThreadSafeIndexer<T> extends java.lang.Object
Class
Instances of classThreadSafeIndexer
indexes objects.ThreadSafeIndexer
are thread-safe.
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_INIT_CAPACITY
The default initial capacity, which is 500.
-
Constructor Summary
Constructors Constructor Description ThreadSafeIndexer()
Creates a newThreadSafeIndexer
instance with the default initial capacity.ThreadSafeIndexer(int initCapacity)
Creates a newThreadSafeIndexer
instance with the specified initial capacity.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
getIndex(T object)
Returns the index of the specified object.int
getIndexIfIndexed(T object)
Returns the index of the specified object, or returns-1
if the specified object is not indexed.int[]
getIndices(T[] objects)
Returns an array of object indices corresponding to the specified object array.T
item(int index)
Returns the object with the specified index.java.util.List<T>
items()
Returns an listed of all indexed objects.java.util.List<T>
items(int[] indices)
Returns a list of objects with the specified indices.int
size()
Returns the number of indexed objects.java.lang.String
toString()
Returnsthis.items().toString()
.
-
-
-
Field Detail
-
DEFAULT_INIT_CAPACITY
public static final int DEFAULT_INIT_CAPACITY
The default initial capacity, which is 500.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ThreadSafeIndexer
public ThreadSafeIndexer()
Creates a newThreadSafeIndexer
instance with the default initial capacity.- See Also:
DEFAULT_INIT_CAPACITY
-
ThreadSafeIndexer
public ThreadSafeIndexer(int initCapacity)
Creates a newThreadSafeIndexer
instance with the specified initial capacity.- Parameters:
initCapacity
- the initial capacity- Throws:
java.lang.IllegalArgumentException
- ifinitCapacity < 1
-
-
Method Detail
-
getIndex
public int getIndex(T object)
Returns the index of the specified object. If the object is not yet indexed, the object will be indexed. Indices are assigned in consecutive order beginning with 0.- Parameters:
object
- the object whose index will be retrieved- Returns:
- the index of the specified object
- Throws:
java.lang.NullPointerException
- ifobject==null
-
getIndices
public int[] getIndices(T[] objects)
Returns an array of object indices corresponding to the specified object array. If an object is not yet indexed, the object will be indexed. Object indices are assigned in increasing order starting with 0.- Parameters:
objects
- an array of objects- Returns:
- an array of object identifier indices
- Throws:
java.lang.IllegalArgumentException
- if there is aj
satisfying(0 <= j && j < objects.length) && objects[j].isEmpty()
java.lang.NullPointerException
- ifobjects == null
java.lang.NullPointerException
- if there is aj
satisfying(0 <= j && j < objects.length) && (objects[j] == null)
-
getIndexIfIndexed
public int getIndexIfIndexed(T object)
Returns the index of the specified object, or returns-1
if the specified object is not indexed.- Parameters:
object
- an object- Returns:
- the index of the specified object, or
-1
if the specified object is not indexed - Throws:
java.lang.NullPointerException
- ifobject == null
.
-
size
public int size()
Returns the number of indexed objects.- Returns:
- the number of indexed objects
-
item
public T item(int index)
Returns the object with the specified index.- Parameters:
index
- an object index- Returns:
- the object with the specified index
- Throws:
java.lang.IndexOutOfBoundsException
- ifindex < 0 || index >= this.size()
-
items
public java.util.List<T> items(int[] indices)
Returns a list of objects with the specified indices.- Parameters:
indices
- an array of object indices- Returns:
- a list of objects with the specified indices
- Throws:
java.lang.IndexOutOfBoundsException
- if there exists aj
satisfying(0 <= j && j < indices.length) && (indices[j] < 0 || indices[j] >= this.size())
-
items
public java.util.List<T> items()
Returns an listed of all indexed objects. The returned list will have sizethis.size()
, and it will satisfythis.items().get(k).equals(this.item(k))==true
for0 <= k && k < this.size()
- Returns:
- an array of objects
-
toString
public java.lang.String toString()
Returnsthis.items().toString()
.- Overrides:
toString
in classjava.lang.Object
- Returns:
- a string representation of
this
-
-