|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.devexperts.util.AbstractConcurrentSet<V>
com.devexperts.util.IndexedSet<K,V>
public class IndexedSet<K,V>
A collection that contains no duplicate elements according to specified identification strategy.
The IndexedSet class implements and obeys general contracts of Set
interface
and provides additional benefits over standard implementations:
The IndexedSet assumes that identity of an element can be represented by a variable number
of attributes, therefore it delegates identification to an external strategy - the Indexer
.
In order to fulfil contracts of Map
interface and for convenience, the IndexedSet
supports concept of explicit key object and also numeric key representation, but these
identification means are optional and need not be supported by all strategies.
Note that the IndexedSet is not synchronized! Concurrent modifications of IndexedSet from multiple threads must be synchronized externally to preserve data integrity. On the other side, the IndexedSet fully supports concurrent asynchronous read operations, which works during concurrent modification by other thread. In case of concurrent modification each atomic read sees IndexedSet either before or after each atomic write operation.
The IndexedSet does not support null values, but it supports null keys
if they are supported by corresponding Indexer
. The IndexedSet can be serialized
if all elements and the Indexer
are serializable.
Constructor Summary | |
---|---|
IndexedSet()
Creates new empty set with default indexer Indexer.DEFAULT and default initial capacity. |
|
IndexedSet(Collection<V> c)
Creates a new set containing the elements in the specified collection. |
|
IndexedSet(Indexer<K,? super V> indexer)
Creates new empty set with specified indexer and default initial capacity. |
|
IndexedSet(Indexer<K,? super V> indexer,
Collection<? extends V> c)
Creates a new set with specified indexer containing the elements in the specified collection. |
|
IndexedSet(Indexer<K,? super V> indexer,
int initial_capacity)
Creates new empty set with specified indexer and specified initial capacity. |
|
IndexedSet(int initial_capacity)
Creates new empty set with default indexer Indexer.DEFAULT and specified initial capacity. |
Method Summary | ||
---|---|---|
boolean |
add(V value)
Adds specified element into this set and returns true if this operation has increased the size of this set. |
|
void |
clear()
Removes all elements from this set. |
|
IndexedSet<K,V> |
clone()
Returns a shallow copy of this set - the values themselves are not cloned. |
|
Iterator<V> |
concurrentIterator()
Returns concurrent iterator over the elements in this set. |
|
boolean |
contains(Object value)
Deprecated. Use containsValue(V) to be explicit about type and intent. |
|
boolean |
containsKey(K key)
Returns true if this set contains element which matches specified key. |
|
boolean |
containsKey(long key)
Returns true if this set contains element which matches specified key. |
|
boolean |
containsValue(V value)
Returns true if this set contains element which matches specified value. |
|
void |
ensureCapacity(int capacity)
Increases the capacity of this set instance, if necessary, to ensure that it can hold at least the number of elements specified by the capacity argument. |
|
Iterator<Map.Entry<K,V>> |
entryIterator()
Returns an iterator over the entries in this set. |
|
V |
getByKey(K key)
Returns the element from this set which matches specified key or null if none were found. |
|
V |
getByKey(long key)
Returns the element from this set which matches specified key or null if none were found. |
|
V |
getByValue(V value)
Returns the element from this set which matches specified value or null if none were found. |
|
Indexer<K,? super V> |
getIndexer()
Returns indexer used to distinguish and identify elements in this set. |
|
IndexedSetStats |
getStats()
Returns static structure statistics of this set. |
|
Iterator<V> |
iterator()
Returns an iterator over the elements in this set. |
|
Iterator<K> |
keyIterator()
Returns an iterator over the keys of elements in this set. |
|
static
|
of(V... objs)
Creates a new set with default indexer containing specified elements. |
|
V |
put(V value)
Puts specified element into this set and returns previous element that matches specified one. |
|
V |
putIfAbsentAndGet(V value)
Puts specified element into this set if it is absent and returns current element in the set that matches specified one. |
|
boolean |
remove(Object value)
Removes specified element from this set if it is present and returns true if this operation has decreased the size of this set. |
|
V |
removeKey(K key)
Removes the element from this set which matches specified key if it is present and returns removed element or null if none were found. |
|
V |
removeKey(long key)
Removes the element from this set which matches specified key if it is present and returns removed element or null if none were found. |
|
V |
removeValue(V value)
Removes the element from this set which matches specified value if it is present and returns removed element or null if none were found. |
|
int |
size()
Returns the number of elements in this set. |
|
Object[] |
toArray()
Returns an array containing all of the elements in this set. |
|
|
toArray(T[] a)
Returns an array containing all of the elements in this set whose runtime type is that of the specified array. |
|
void |
trimToSize()
Trims the capacity of this set instance to be the set's current size. |
Methods inherited from class com.devexperts.util.AbstractConcurrentSet |
---|
addAll, containsAll, equals, hashCode, isEmpty, removeAll, retainAll, toString |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public IndexedSet()
Indexer.DEFAULT
and default initial capacity.
public IndexedSet(int initial_capacity)
Indexer.DEFAULT
and specified initial capacity.
public IndexedSet(Indexer<K,? super V> indexer)
public IndexedSet(Indexer<K,? super V> indexer, int initial_capacity)
public IndexedSet(Collection<V> c)
IndexedSet
, then new indexed set uses same indexer,
otherwise it uses default indexer Indexer.DEFAULT
.
public IndexedSet(Indexer<K,? super V> indexer, Collection<? extends V> c)
Method Detail |
---|
public static <V> IndexedSet<V,V> of(V... objs)
public IndexedSet<K,V> clone()
clone
in class Object
public void ensureCapacity(int capacity)
public void trimToSize()
public void clear()
clear
in interface Collection<V>
clear
in interface Set<V>
clear
in class AbstractConcurrentSet<V>
public Indexer<K,? super V> getIndexer()
public int size()
size
in interface Collection<V>
size
in interface Set<V>
public V getByValue(V value)
public V getByKey(K key)
public V getByKey(long key)
public boolean contains(Object value)
containsValue(V)
to be explicit about type and intent.
This implementation delegates to (getByValue(value)
!= null) expression.
Note, that unlike HashSet.contains(java.lang.Object)
,
this method might throw ClassCastException
if value is of the wrong class.
contains
in interface Collection<V>
contains
in interface Set<V>
public boolean containsValue(V value)
This implementation delegates to (getByValue(value)
!= null) expression.
public boolean containsKey(K key)
This implementation delegates to (getByKey(key)
!= null) expression.
public boolean containsKey(long key)
This implementation delegates to (getByKey(key)
!= null) expression.
public Iterator<V> iterator()
iterator
in interface Iterable<V>
iterator
in interface Collection<V>
iterator
in interface Set<V>
public Iterator<K> keyIterator()
public Iterator<Map.Entry<K,V>> entryIterator()
public Iterator<V> concurrentIterator()
public Object[] toArray()
Collection.toArray()
method.
toArray
in interface Collection<V>
toArray
in interface Set<V>
toArray
in class AbstractConcurrentSet<V>
public <T> T[] toArray(T[] a)
Collection.toArray(Object[])
method.
toArray
in interface Collection<V>
toArray
in interface Set<V>
toArray
in class AbstractConcurrentSet<V>
public IndexedSetStats getStats()
public V put(V value)
public V putIfAbsentAndGet(V value)
if (set.containsValue(value)) { return set.getByValue(value); } else { set.put(value); return value; }except that the action is performed atomically if it is properly synchronized.
Note, that unlike ConcurrentMap.putIfAbsent(K, V)
,
this method returns specified value (not null) if the value was absent.
public boolean add(V value)
This implementation adds value using put(value)
method.
add
in interface Collection<V>
add
in interface Set<V>
public boolean remove(Object value)
This implementation removes value using removeValue(value)
method.
Note, that unlike HashSet.remove(java.lang.Object)
,
this method might throw ClassCastException
if value is of the wrong class.
remove
in interface Collection<V>
remove
in interface Set<V>
public V removeValue(V value)
public V removeKey(K key)
public V removeKey(long key)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |