public class HashIndex<E> extends AbstractCollection<E> implements Index<E>, RandomAccess
get(int)
and indexOf(E)
.
The indexOf(E)
method compares objects by
equals()
, as other Collections.
The typical usage would be:
Index<String> index = new Index<String>(collection);
followed by
int i = index.indexOf(str);
or
String s = index.get(i);
An Index can be locked or unlocked: a locked index cannot have new items added to it.
AbstractCollection
,
Serialized FormConstructor and Description |
---|
HashIndex()
Creates a new Index.
|
HashIndex(Collection<? extends E> c)
Creates a new Index and adds every member of c to it.
|
HashIndex(Index<? extends E> index) |
HashIndex(int capacity)
Creates a new Index.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E o)
Adds an object to the Index.
|
boolean |
addAll(Collection<? extends E> c)
Adds every member of Collection to the Index.
|
int |
addToIndex(E o)
Takes an Object and returns the integer index of the Object.
|
void |
clear()
Clears this Index.
|
boolean |
contains(Object o)
Checks whether an Object already has an index in the Index
|
boolean |
equals(Object o) |
E |
get(int i)
Gets the object whose index is the integer argument.
|
int |
hashCode() |
int |
indexOf(E o)
Returns the integer index of the Object in the Index or -1 if the Object
is not already in the Index.
|
int |
indexOf(E o,
boolean add)
Deprecated.
|
int[] |
indices(Collection<E> elements)
Returns the index of each elem in a List.
|
boolean |
isLocked()
Queries the Index for whether it's locked or not.
|
Iterator<E> |
iterator()
Returns an iterator over the elements of the collection.
|
static Index<String> |
loadFromFilename(String file)
This assumes each line is of the form (number=value) and it adds each value in order of the lines in the file
|
static Index<String> |
loadFromFileWithList(String file)
This assumes each line is one value and creates index by adding values in the order of the lines in the file
|
static Index<String> |
loadFromReader(BufferedReader br)
This is the analogue of
loadFromFilename , and is intended to be included in a routine
that unpacks a text-serialized form of an object that incorporates an Index. |
void |
lock()
Locks the Index.
|
Collection<E> |
objects(int[] indices)
Looks up the objects corresponding to an array of indices, and returns them in a
Collection . |
List<E> |
objectsList()
Returns a complete
List of indexed objects, in the order of their indices. |
void |
saveToFilename(String file)
Save the contents of this index into a file.
|
void |
saveToWriter(Writer bw)
This saves the contents of this index into string form, as part of a larger
text-serialization.
|
int |
size()
Returns the number of indexed objects.
|
String |
toString()
Returns a readable version of the Index contents
|
String |
toString(int n)
Returns a readable version of at least part of the Index contents.
|
String |
toStringOneEntryPerLine() |
String |
toStringOneEntryPerLine(int n) |
void |
unlock()
Unlocks the Index.
|
HashIndex<E> |
unmodifiableView()
Returns an unmodifiable view of the Index.
|
containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
forEach, spliterator
parallelStream, removeIf, spliterator, stream
public HashIndex()
public HashIndex(int capacity)
capacity
- Initial capacity of Index.public HashIndex(Collection<? extends E> c)
c
- A collection of objectspublic void clear()
clear
in interface Index<E>
clear
in interface Collection<E>
clear
in class AbstractCollection<E>
public int[] indices(Collection<E> elements)
elements
- The list of itemspublic Collection<E> objects(int[] indices)
Collection
.
This collection is not a copy, but accesses the data structures of the Index.objects
in interface Index<E>
indices
- An array of indicesCollection
of the objects corresponding to the indices argument.public int size()
size
in interface Index<E>
size
in interface Collection<E>
size
in class AbstractCollection<E>
public E get(int i)
public List<E> objectsList()
List
of indexed objects, in the order of their indices. DANGER!
The current implementation returns the actual index list, not a defensive copy. Messing with this List
can seriously screw up the state of the Index. (perhaps this method needs to be eliminated? I don't think it's
ever used in ways that we couldn't use the Index itself for directly. --Roger, 12/29/04)objectsList
in interface Index<E>
List
of indexed objectspublic boolean isLocked()
public void lock()
add(E)
will
leave the Index unchanged and return false
).public void unlock()
add(E)
will
leave the Index unchanged and return false
).public int indexOf(E o)
public int addToIndex(E o)
Index
addToIndex
in interface Index<E>
o
- the Object whose index is desired.@Deprecated public int indexOf(E o, boolean add)
Notes: The method indexOf(x, true) is the direct replacement for the number(x) method in the old Numberer class. This method now uses a Semaphore object to make the index safe for concurrent multithreaded usage. (CDM: Is this better than using a synchronized block?)
public boolean addAll(Collection<? extends E> c)
addAll
in interface Index<E>
addAll
in interface Collection<E>
addAll
in class AbstractCollection<E>
public boolean add(E o)
add
in interface Index<E>
add
in interface Collection<E>
add
in class AbstractCollection<E>
public boolean contains(Object o)
contains
in interface Index<E>
contains
in interface Collection<E>
contains
in class AbstractCollection<E>
o
- the object to be queried.public void saveToFilename(String file)
Index
saveToFilename
in interface Index<E>
file
- File name.public static Index<String> loadFromFilename(String file)
file
- Which file to loadpublic void saveToWriter(Writer bw) throws IOException
saveToFileName
.saveToWriter
in interface Index<E>
bw
- Writer to save to.IOException
- Exception thrown if cannot save.public static Index<String> loadFromReader(BufferedReader br) throws IOException
loadFromFilename
, and is intended to be included in a routine
that unpacks a text-serialized form of an object that incorporates an Index.
NOTE: presumes that the next readLine() will read in the first line of the
portion of the text file representing the saved Index. Currently reads until it
encounters a blank line, consuming that line and returning the Index.
TODO: figure out how best to terminate: currently a blank line is considered to be a terminator.br
- The Reader to read the index fromIOException
public String toString()
toString
in class AbstractCollection<E>
public String toStringOneEntryPerLine()
public String toString(int n)
n
- Show the first n items in the Indexpublic String toStringOneEntryPerLine(int n)
public Iterator<E> iterator()
iterator
in interface Iterable<E>
iterator
in interface Collection<E>
iterator
in class AbstractCollection<E>
public HashIndex<E> unmodifiableView()
public static Index<String> loadFromFileWithList(String file)
file
- Which file to loadpublic boolean equals(Object o)
equals
in interface Collection<E>
equals
in class Object
public int hashCode()
hashCode
in interface Collection<E>
hashCode
in class Object