Package beagleutil

Class ThreadSafeIndexer<T>

java.lang.Object
beagleutil.ThreadSafeIndexer<T>
Type Parameters:
T - the type parameter.

public final class ThreadSafeIndexer<T> extends Object

Class ThreadSafeIndexer indexes objects.

Instances of class ThreadSafeIndexer are thread-safe.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The default initial capacity, which is 500.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new ThreadSafeIndexer instance with the default initial capacity.
    ThreadSafeIndexer(int initCapacity)
    Creates a new ThreadSafeIndexerinstance with the specified initial capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    getIndex(T object)
    Returns the index of the specified object.
    int
    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.
    item(int index)
    Returns the object with the specified index.
    Returns an listed of all indexed objects.
    items(int[] indices)
    Returns a list of objects with the specified indices.
    int
    Returns the number of indexed objects.
    Returns this.items().toString().

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • DEFAULT_INIT_CAPACITY

      public static final int DEFAULT_INIT_CAPACITY
      The default initial capacity, which is 500.
      See Also:
  • Constructor Details

    • ThreadSafeIndexer

      public ThreadSafeIndexer()
      Creates a new ThreadSafeIndexer instance with the default initial capacity.
      See Also:
    • ThreadSafeIndexer

      public ThreadSafeIndexer(int initCapacity)
      Creates a new ThreadSafeIndexerinstance with the specified initial capacity.
      Parameters:
      initCapacity - the initial capacity
      Throws:
      IllegalArgumentException - if initCapacity < 1
  • Method Details

    • 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:
      NullPointerException - if object==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:
      IllegalArgumentException - if there is a j satisfying (0 <= j && j < objects.length) && objects[j].isEmpty()
      NullPointerException - if objects == null
      NullPointerException - if there is a j 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:
      NullPointerException - if object == 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:
      IndexOutOfBoundsException - if index < 0 || index >= this.size()
    • items

      public 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:
      IndexOutOfBoundsException - if there exists a j satisfying (0 <= j && j < indices.length) && (indices[j] < 0 || indices[j] >= this.size())
    • items

      public List<T> items()
      Returns an listed of all indexed objects. The returned list will have size this.size(), and it will satisfy this.items().get(k).equals(this.item(k))==true for 0 <= k && k < this.size()
      Returns:
      an array of objects
    • toString

      public String toString()
      Returns this.items().toString().
      Overrides:
      toString in class Object
      Returns:
      a string representation of this