edu.stanford.nlp.util
Class Index<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by edu.stanford.nlp.util.Index<E>
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.List<E>, java.util.RandomAccess

public class Index<E>
extends java.util.AbstractList<E>
implements java.io.Serializable, java.util.RandomAccess

An Index is a list which maps between an Object vocabulary and a contiguous non-negative integer index beginning (inclusively) at 0. It supports constant-time lookup in both directions (via get(int) and indexOf(Object). The indexOf(Object) method compares objects by equals, as other Collections.

The typical usage would be:

Index index = new Index(collection);

followed by

int i = index.indexOf(object);

or

Object o = index.get(i);

The source contains a concrete example of use as the main method.

An Index can be locked or unlocked: a locked index cannot have new items added to it.

Since:
1.0
See Also:
AbstractList, Serialized Form

Field Summary
protected  java.util.Map<E,java.lang.Integer> indexes
           
protected  boolean locked
           
protected  java.util.List<E> objects
           
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
Index()
           
Index(java.util.Collection<? extends E> c)
           
 
Method Summary
 boolean add(E o)
          Adds an object to the Index.
 boolean addAll(java.util.Collection<? extends E> c)
           
 void clear()
          Clears this Index.
 boolean contains(java.lang.Object o)
           
static Index deserializeReadable(java.lang.String file)
           
 E get(int i)
           
 int indexOf(java.lang.Object o)
           
 int[] indices(java.util.List<E> elems)
          Returns the index of each elem in an array.
 boolean isLocked()
           
 int lastIndexOf(java.lang.Object o)
           
 void lock()
           
static void main(java.lang.String[] args)
           
 java.util.List<E> objectsList()
           
 void serializeReadable(java.lang.String file)
           
 int size()
           
 java.util.List<E> subList(int from, int to)
           
 void unlock()
           
 
Methods inherited from class java.util.AbstractList
add, addAll, equals, hashCode, iterator, listIterator, listIterator, remove, removeRange, set
 
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
 

Field Detail

objects

protected java.util.List<E> objects

indexes

protected java.util.Map<E,java.lang.Integer> indexes

locked

protected boolean locked
Constructor Detail

Index

public Index()

Index

public Index(java.util.Collection<? extends E> c)
Method Detail

clear

public void clear()
Clears this Index.

Specified by:
clear in interface java.util.Collection<E>
Specified by:
clear in interface java.util.List<E>
Overrides:
clear in class java.util.AbstractList<E>

indices

public int[] indices(java.util.List<E> elems)
Returns the index of each elem in an array.


size

public int size()
Specified by:
size in interface java.util.Collection<E>
Specified by:
size in interface java.util.List<E>
Specified by:
size in class java.util.AbstractCollection<E>

get

public E get(int i)
Specified by:
get in interface java.util.List<E>
Specified by:
get in class java.util.AbstractList<E>

objectsList

public java.util.List<E> objectsList()

isLocked

public boolean isLocked()

lock

public void lock()

unlock

public void unlock()

indexOf

public int indexOf(java.lang.Object o)
Specified by:
indexOf in interface java.util.List<E>
Overrides:
indexOf in class java.util.AbstractList<E>

lastIndexOf

public int lastIndexOf(java.lang.Object o)
Specified by:
lastIndexOf in interface java.util.List<E>
Overrides:
lastIndexOf in class java.util.AbstractList<E>

addAll

public boolean addAll(java.util.Collection<? extends E> c)
Specified by:
addAll in interface java.util.Collection<E>
Specified by:
addAll in interface java.util.List<E>
Overrides:
addAll in class java.util.AbstractCollection<E>

subList

public java.util.List<E> subList(int from,
                                 int to)
Specified by:
subList in interface java.util.List<E>
Overrides:
subList in class java.util.AbstractList<E>

add

public boolean add(E o)
Adds an object to the Index. If it was already in the Index, then nothing is done. If it is not in the Index, then it is added iff the Index hasn't been locked.

Specified by:
add in interface java.util.Collection<E>
Specified by:
add in interface java.util.List<E>
Overrides:
add in class java.util.AbstractList<E>
Returns:
true if the item was added to the index and false if the item was already in the index or if the index is locked

contains

public boolean contains(java.lang.Object o)
Specified by:
contains in interface java.util.Collection<E>
Specified by:
contains in interface java.util.List<E>
Overrides:
contains in class java.util.AbstractCollection<E>

serializeReadable

public void serializeReadable(java.lang.String file)

deserializeReadable

public static Index deserializeReadable(java.lang.String file)

main

public static void main(java.lang.String[] args)