public class HashIndex<E> extends java.util.AbstractCollection<E> implements Index<E>, java.util.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(java.util.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.
|
HashIndex(java.util.function.Supplier<java.util.List<E>> objLookupFactory,
java.util.function.Supplier<java.util.Map<E,java.lang.Integer>> indexLookupFactory)
Create a new
HashIndex , backed by the given collection types. |
Modifier and Type | Method and Description |
---|---|
boolean |
add(E o)
Adds an object to the Index.
|
boolean |
addAll(java.util.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.
|
int |
addToIndexUnsafe(E o)
Add the given item to the index, but without taking any locks.
|
void |
clear()
Clears this Index.
|
boolean |
contains(java.lang.Object o)
Checks whether an Object already has an index in the Index
|
boolean |
equals(java.lang.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(java.util.Collection<E> elements)
Returns the index of each elem in a List.
|
boolean |
isLocked()
Queries the Index for whether it's locked or not.
|
java.util.Iterator<E> |
iterator()
Returns an iterator over the elements of the collection.
|
static Index<java.lang.String> |
loadFromFilename(java.lang.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<java.lang.String> |
loadFromFileWithList(java.lang.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<java.lang.String> |
loadFromReader(java.io.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.
|
java.util.Collection<E> |
objects(int[] indices)
Looks up the objects corresponding to an array of indices, and returns them in a
Collection . |
java.util.List<E> |
objectsList()
Returns a complete
List of indexed objects, in the order of their indices. |
void |
saveToFilename(java.lang.String file)
Save the contents of this index into a file.
|
void |
saveToWriter(java.io.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.
|
java.lang.String |
toString()
Returns a readable version of the Index contents
|
java.lang.String |
toString(int n)
Returns a readable version of at least part of the Index contents.
|
java.lang.String |
toStringOneEntryPerLine() |
java.lang.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
public HashIndex()
public HashIndex(int capacity)
capacity
- Initial capacity of Index.public HashIndex(java.util.function.Supplier<java.util.List<E>> objLookupFactory, java.util.function.Supplier<java.util.Map<E,java.lang.Integer>> indexLookupFactory)
HashIndex
, backed by the given collection types.objLookupFactory
- The constructor for the object lookup -- traditionally an ArrayList
.indexLookupFactory
- The constructor for the index lookup -- traditionally a HashMap
.public HashIndex(java.util.Collection<? extends E> c)
c
- A collection of objectspublic void clear()
public int[] indices(java.util.Collection<E> elements)
elements
- The list of itemspublic java.util.Collection<E> objects(int[] indices)
Collection
.
This collection is not a copy, but accesses the data structures of the Index.public int size()
public E get(int i)
public java.util.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.public int addToIndexUnsafe(E o)
Index#addToIndex(E)
@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(java.util.Collection<? extends E> c)
public boolean add(E o)
public boolean contains(java.lang.Object o)
public void saveToFilename(java.lang.String file)
Index
saveToFilename
in interface Index<E>
file
- File name.public static Index<java.lang.String> loadFromFilename(java.lang.String file)
file
- Which file to loadpublic void saveToWriter(java.io.Writer bw) throws java.io.IOException
saveToFileName
.saveToWriter
in interface Index<E>
bw
- Writer to save to.java.io.IOException
- Exception thrown if cannot save.public static Index<java.lang.String> loadFromReader(java.io.BufferedReader br) throws java.io.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 fromjava.io.IOException
public java.lang.String toString()
toString
in class java.util.AbstractCollection<E>
public java.lang.String toStringOneEntryPerLine()
public java.lang.String toString(int n)
n
- Show the first n items in the Indexpublic java.lang.String toStringOneEntryPerLine(int n)
public java.util.Iterator<E> iterator()
public HashIndex<E> unmodifiableView()
public static Index<java.lang.String> loadFromFileWithList(java.lang.String file)
file
- Which file to loadpublic boolean equals(java.lang.Object o)
equals
in interface java.util.Collection<E>
equals
in class java.lang.Object
public int hashCode()
hashCode
in interface java.util.Collection<E>
hashCode
in class java.lang.Object