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.ObjectClass
Instances of classThreadSafeIndexerindexes objects.ThreadSafeIndexerare thread-safe.
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_INIT_CAPACITYThe default initial capacity, which is 500.
-
Constructor Summary
Constructors Constructor Description ThreadSafeIndexer()Creates a newThreadSafeIndexerinstance with the default initial capacity.ThreadSafeIndexer(int initCapacity)Creates a newThreadSafeIndexerinstance with the specified initial capacity.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intgetIndex(T object)Returns the index of the specified object.intgetIndexIfIndexed(T object)Returns the index of the specified object, or returns-1if the specified object is not indexed.int[]getIndices(T[] objects)Returns an array of object indices corresponding to the specified object array.Titem(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.intsize()Returns the number of indexed objects.java.lang.StringtoString()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 newThreadSafeIndexerinstance with the default initial capacity.- See Also:
DEFAULT_INIT_CAPACITY
-
ThreadSafeIndexer
public ThreadSafeIndexer(int initCapacity)
Creates a newThreadSafeIndexerinstance 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 ajsatisfying(0 <= j && j < objects.length) && objects[j].isEmpty()java.lang.NullPointerException- ifobjects == nulljava.lang.NullPointerException- if there is ajsatisfying(0 <= j && j < objects.length) && (objects[j] == null)
-
getIndexIfIndexed
public int getIndexIfIndexed(T object)
Returns the index of the specified object, or returns-1if the specified object is not indexed.- Parameters:
object- an object- Returns:
- the index of the specified object, or
-1if 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 ajsatisfying(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))==truefor0 <= k && k < this.size()- Returns:
- an array of objects
-
toString
public java.lang.String toString()
Returnsthis.items().toString().- Overrides:
toStringin classjava.lang.Object- Returns:
- a string representation of
this
-
-