com.devexperts.util
Class IndexedSet<K,V>

java.lang.Object
  extended by com.devexperts.util.AbstractConcurrentSet<V>
      extended by com.devexperts.util.IndexedSet<K,V>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<V>, Collection<V>, Set<V>
Direct Known Subclasses:
SynchronizedIndexedSet

public class IndexedSet<K,V>
extends AbstractConcurrentSet<V>
implements Cloneable, Serializable

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.

See Also:
Serialized Form

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
<V> IndexedSet<V,V>
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.
<T> T[]
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

IndexedSet

public IndexedSet()
Creates new empty set with default indexer Indexer.DEFAULT and default initial capacity.


IndexedSet

public IndexedSet(int initial_capacity)
Creates new empty set with default indexer Indexer.DEFAULT and specified initial capacity.


IndexedSet

public IndexedSet(Indexer<K,? super V> indexer)
Creates new empty set with specified indexer and default initial capacity.


IndexedSet

public IndexedSet(Indexer<K,? super V> indexer,
                  int initial_capacity)
Creates new empty set with specified indexer and specified initial capacity.


IndexedSet

public IndexedSet(Collection<V> c)
Creates a new set containing the elements in the specified collection. If specified collection is an IndexedSet, then new indexed set uses same indexer, otherwise it uses default indexer Indexer.DEFAULT.


IndexedSet

public IndexedSet(Indexer<K,? super V> indexer,
                  Collection<? extends V> c)
Creates a new set with specified indexer containing the elements in the specified collection.

Method Detail

of

public static <V> IndexedSet<V,V> of(V... objs)
Creates a new set with default indexer containing specified elements.


clone

public IndexedSet<K,V> clone()
Returns a shallow copy of this set - the values themselves are not cloned.

Overrides:
clone in class Object

ensureCapacity

public 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.


trimToSize

public void trimToSize()
Trims the capacity of this set instance to be the set's current size. An application can use this operation to minimize the storage of this set instance.


clear

public void clear()
Removes all elements from this set.

Specified by:
clear in interface Collection<V>
Specified by:
clear in interface Set<V>
Overrides:
clear in class AbstractConcurrentSet<V>

getIndexer

public Indexer<K,? super V> getIndexer()
Returns indexer used to distinguish and identify elements in this set.


size

public int size()
Returns the number of elements in this set.

Specified by:
size in interface Collection<V>
Specified by:
size in interface Set<V>

getByValue

public V getByValue(V value)
Returns the element from this set which matches specified value or null if none were found.


getByKey

public V getByKey(K key)
Returns the element from this set which matches specified key or null if none were found.


getByKey

public V getByKey(long key)
Returns the element from this set which matches specified key or null if none were found.


contains

public boolean contains(Object value)
Deprecated. Use containsValue(V) to be explicit about type and intent.

Returns true if this set contains element which matches specified value.

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.

Specified by:
contains in interface Collection<V>
Specified by:
contains in interface Set<V>

containsValue

public boolean containsValue(V value)
Returns true if this set contains element which matches specified value.

This implementation delegates to (getByValue(value) != null) expression.


containsKey

public boolean containsKey(K key)
Returns true if this set contains element which matches specified key.

This implementation delegates to (getByKey(key) != null) expression.


containsKey

public boolean containsKey(long key)
Returns true if this set contains element which matches specified key.

This implementation delegates to (getByKey(key) != null) expression.


iterator

public Iterator<V> iterator()
Returns an iterator over the elements in this set.

Specified by:
iterator in interface Iterable<V>
Specified by:
iterator in interface Collection<V>
Specified by:
iterator in interface Set<V>

keyIterator

public Iterator<K> keyIterator()
Returns an iterator over the keys of elements in this set.


entryIterator

public Iterator<Map.Entry<K,V>> entryIterator()
Returns an iterator over the entries in this set.


concurrentIterator

public Iterator<V> concurrentIterator()
Returns concurrent iterator over the elements in this set.


toArray

public Object[] toArray()
Returns an array containing all of the elements in this set. Obeys the general contract of the Collection.toArray() method.

Specified by:
toArray in interface Collection<V>
Specified by:
toArray in interface Set<V>
Overrides:
toArray in class AbstractConcurrentSet<V>

toArray

public <T> T[] toArray(T[] a)
Returns an array containing all of the elements in this set whose runtime type is that of the specified array. Obeys the general contract of the Collection.toArray(Object[]) method.

Specified by:
toArray in interface Collection<V>
Specified by:
toArray in interface Set<V>
Overrides:
toArray in class AbstractConcurrentSet<V>

getStats

public IndexedSetStats getStats()
Returns static structure statistics of this set.


put

public V put(V value)
Puts specified element into this set and returns previous element that matches specified one.


putIfAbsentAndGet

public 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. This is equivalent to
   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.


add

public boolean add(V value)
Adds specified element into this set and returns true if this operation has increased the size of this set.

This implementation adds value using put(value) method.

Specified by:
add in interface Collection<V>
Specified by:
add in interface Set<V>

remove

public 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.

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.

Specified by:
remove in interface Collection<V>
Specified by:
remove in interface Set<V>

removeValue

public 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.


removeKey

public 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.


removeKey

public 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.



Copyright © 2013 Devexperts. All Rights Reserved.