T
- the type of object@ThreadSafe public class IndexedSet<T> extends AbstractSet<T>
IndexedSet
instances may specify different fields to index. The field type
must be comparable. The field value must not be changed after an object is added to the set,
otherwise, behavior for all operations is not specified.
If concurrent adds or removes for objects which are equivalent, but not the same exact object,
the behavior is undefined. Therefore, do not add or remove "clones" objects in the
IndexedSet
.
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:
IndexDefinition<Puppy> idIndex = new IndexDefinition<Puppy>(true) { @Override Object getFieldValue(Puppy o) { return o.id(); } } IndexDefinition<Puppy> nameIndex = new IndexDefinition<Puppy>(true) { @Override Object getFieldValue(Puppy o) { return o.name(); } }Then create an
IndexedSet
and add puppies:
IndexedSet<Puppy> puppies = new IndexedSet<Puppy>(idIndex, nameIndex); puppies.add(new Puppy("sweet", 0)); puppies.add(new Puppy("heart", 1));Then retrieve the puppy named sweet:
Puppy sweet = puppies.getFirstByField(nameIndex, "sweet");and retrieve the puppy with id 1:
Puppy heart = puppies.getFirstByField(idIndex, 1L);
Constructor and Description |
---|
IndexedSet(IndexDefinition<T,?> primaryIndexDefinition,
IndexDefinition<T,?>... otherIndexDefinitions)
Constructs a new
IndexedSet instance with at least one field as the index. |
Modifier and Type | Method and Description |
---|---|
boolean |
add(T object)
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.
|
<V> boolean |
contains(IndexDefinition<T,V> indexDefinition,
V value)
Whether there is an object with the specified index field value in the set.
|
<V> Set<T> |
getByField(IndexDefinition<T,V> indexDefinition,
V value)
Gets a subset of objects with the specified field value.
|
<V> T |
getFirstByField(IndexDefinition<T,V> indexDefinition,
V value)
Gets the 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(Object object)
Removes an object from the set.
|
<V> int |
removeByField(IndexDefinition<T,V> indexDefinition,
V value)
Removes the subset of objects with the specified index field value.
|
int |
size() |
equals, hashCode, removeAll
addAll, contains, containsAll, isEmpty, retainAll, toArray, toArray, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
addAll, contains, containsAll, isEmpty, retainAll, spliterator, toArray, toArray
parallelStream, removeIf, stream
@SafeVarargs public IndexedSet(IndexDefinition<T,?> primaryIndexDefinition, IndexDefinition<T,?>... otherIndexDefinitions)
IndexedSet
instance with at least one field as the index.primaryIndexDefinition
- at least one field is needed to index the set of objects. This
primaryIndexDefinition is used to initialize mPrimaryIndex
and is
recommended to be unique in consideration of performance.otherIndexDefinitions
- other index definitions to index the setpublic void clear()
clear
in interface Collection<T>
clear
in interface Set<T>
clear
in class AbstractCollection<T>
public boolean add(T object)
(o == null ? o2 == null : o.equals(o2))
. If this set already contains the object, the
call leaves the set unchanged.add
in interface Collection<T>
add
in interface Set<T>
add
in class AbstractCollection<T>
object
- the object to addpublic Iterator<T> iterator()
Iterable
so that users can foreach the IndexedSet
directly.
Note that the behavior of the iterator is unspecified if the underlying collection is
modified while a thread is going through the iterator.iterator
in interface Iterable<T>
iterator
in interface Collection<T>
iterator
in interface Set<T>
iterator
in class AbstractCollection<T>
IndexedSet
public <V> boolean contains(IndexDefinition<T,V> indexDefinition, V value)
V
- the field typeindexDefinition
- the field index definitionvalue
- the field valuepublic <V> Set<T> getByField(IndexDefinition<T,V> indexDefinition, V value)
V
- the field typeindexDefinition
- the field index definitionvalue
- the field value to be satisfiedpublic <V> T getFirstByField(IndexDefinition<T,V> indexDefinition, V value)
V
- the field typeindexDefinition
- the field index definitionvalue
- the field valuepublic boolean remove(Object object)
remove
in interface Collection<T>
remove
in interface Set<T>
remove
in class AbstractCollection<T>
object
- the object to removepublic <V> int removeByField(IndexDefinition<T,V> indexDefinition, V value)
V
- the field typeindexDefinition
- the field indexvalue
- the field valuepublic int size()
size
in interface Collection<T>
size
in interface Set<T>
size
in class AbstractCollection<T>
Copyright © 2023. All Rights Reserved.