Package beagleutil

Class ThreadSafeIndexer<T>

  • Type Parameters:
    T - the type parameter.

    public final class ThreadSafeIndexer<T>
    extends java.lang.Object

    Class ThreadSafeIndexer indexes objects.

    Instances of class 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 new ThreadSafeIndexer instance with the default initial capacity.
      ThreadSafeIndexer​(int initCapacity)
      Creates a new ThreadSafeIndexerinstance 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()
      Returns this.items().toString().
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • 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 new ThreadSafeIndexer instance with the default initial capacity.
        See Also:
        DEFAULT_INIT_CAPACITY
      • ThreadSafeIndexer

        public ThreadSafeIndexer​(int initCapacity)
        Creates a new ThreadSafeIndexerinstance with the specified initial capacity.
        Parameters:
        initCapacity - the initial capacity
        Throws:
        java.lang.IllegalArgumentException - if initCapacity < 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 - 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:
        java.lang.IllegalArgumentException - if there is a j satisfying (0 <= j && j < objects.length) && objects[j].isEmpty()
        java.lang.NullPointerException - if objects == null
        java.lang.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:
        java.lang.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:
        java.lang.IndexOutOfBoundsException - if index < 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 a j 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 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 java.lang.String toString()
        Returns this.items().toString().
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation of this