public class IndexedSet<T> extends Object implements Iterable<T>
This class is thread safe.
Example usage: We have a set of puppies:
class Puppy {
private final String mName;
private final long mId;
public Puppy(String name, long id) {
mName = name;
mId = id;
}
public String name() {
return mName;
}
public long id() {
return mId;
}
}
We want to be able to retrieve the set of puppies via a puppy's id or name, one way is to have
two maps like Map<String, Puppy> nameToPuppy and
Map<Long, Puppy> idToPuppy, another way is to use a single instance of
IndexedSet!
First, define the fields to be indexed:
FieldIndexidIndex = new FieldIndex {
| Modifier and Type | Class and Description |
|---|---|
static interface |
IndexedSet.FieldIndex<T>
An interface representing an index for this IndexedSet, each index for this set must implement
the interface to define how to get the value of the field chosen as the index.
|
| Constructor and Description |
|---|
IndexedSet(IndexedSet.FieldIndex<T> field,
IndexedSet.FieldIndex<T>... otherFields)
Constructs a new IndexedSet instance with at least one field as the index.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(T objToAdd)
Adds an object o to the set if there is no other object o2 such that
(o == null ? o2 == null : o.equals(o2)). |
void |
clear()
Removes all the entries in this set.
|
boolean |
contains(IndexedSet.FieldIndex<T> index,
Object value)
Whether there is an object with the specified field value in the set.
|
Set<T> |
getByField(IndexedSet.FieldIndex<T> index,
Object value)
Gets a subset of objects with the specified field value.
|
T |
getFirstByField(IndexedSet.FieldIndex<T> index,
Object value)
Gets the first object from the set of objects with the specified field value.
|
Iterator<T> |
iterator()
Returns an iterator over the elements in this set.
|
boolean |
remove(T object)
Removes an object from the set.
|
boolean |
removeByField(IndexedSet.FieldIndex<T> index,
Object value)
Removes the subset of objects with the specified field value.
|
int |
size() |
public IndexedSet(IndexedSet.FieldIndex<T> field, IndexedSet.FieldIndex<T>... otherFields)
field - at least one field is needed to index the set of objectsotherFields - other fields to index the setpublic void clear()
public boolean add(T objToAdd)
(o == null ? o2 == null : o.equals(o2)). If this set already contains the object, the
call leaves the set unchanged.objToAdd - the object to addpublic Iterator<T> iterator()
Iterable so that users can foreach the IndexedSet directly.
Note that the behaviour of the iterator is unspecified if the underlying collection is
modified while a thread is going through the iterator.public boolean contains(IndexedSet.FieldIndex<T> index, Object value)
index - the field indexvalue - the field valuepublic Set<T> getByField(IndexedSet.FieldIndex<T> index, Object value)
index - the field indexvalue - the field value to be satisfiedpublic T getFirstByField(IndexedSet.FieldIndex<T> index, Object value)
index - the field indexvalue - the field valuepublic boolean remove(T object)
object - the object to removepublic boolean removeByField(IndexedSet.FieldIndex<T> index, Object value)
index - the field indexvalue - the field valuepublic int size()
Copyright © 2015. All Rights Reserved.