org.elasticsearch.util.concurrent.highscalelib
Class NonBlockingHashSet<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<E>
          extended by org.elasticsearch.util.concurrent.highscalelib.NonBlockingHashSet<E>
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.Set<E>

public class NonBlockingHashSet<E>
extends java.util.AbstractSet<E>
implements java.io.Serializable

A simple wrapper around NonBlockingHashMap making it implement the Set interface. All operations are Non-Blocking and multi-thread safe.

Since:
1.5
See Also:
Serialized Form

Constructor Summary
NonBlockingHashSet()
          Make a new empty NonBlockingHashSet.
 
Method Summary
 boolean add(E o)
          Add o to the set.
 void clear()
          Empty the set.
 boolean contains(java.lang.Object o)
           
 java.util.Iterator<E> iterator()
           
 void readOnly()
          Atomically make the set immutable.
 boolean remove(java.lang.Object o)
          Remove o from the set.
 int size()
          Current count of elements in the set.
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
addAll, containsAll, isEmpty, retainAll, toArray, toArray
 

Constructor Detail

NonBlockingHashSet

public NonBlockingHashSet()
Make a new empty NonBlockingHashSet.

Method Detail

add

public boolean add(E o)
Add o to the set.

Specified by:
add in interface java.util.Collection<E>
Specified by:
add in interface java.util.Set<E>
Overrides:
add in class java.util.AbstractCollection<E>
Returns:
true if o was added to the set, false if o was already in the set.

contains

public boolean contains(java.lang.Object o)
Specified by:
contains in interface java.util.Collection<E>
Specified by:
contains in interface java.util.Set<E>
Overrides:
contains in class java.util.AbstractCollection<E>
Returns:
true if o is in the set.

remove

public boolean remove(java.lang.Object o)
Remove o from the set.

Specified by:
remove in interface java.util.Collection<E>
Specified by:
remove in interface java.util.Set<E>
Overrides:
remove in class java.util.AbstractCollection<E>
Returns:
true if o was removed to the set, false if o was not in the set.

size

public int size()
Current count of elements in the set. Due to concurrent racing updates, the size is only ever approximate. Updates due to the calling thread are immediately visible to calling thread.

Specified by:
size in interface java.util.Collection<E>
Specified by:
size in interface java.util.Set<E>
Specified by:
size in class java.util.AbstractCollection<E>
Returns:
count of elements.

clear

public void clear()
Empty the set.

Specified by:
clear in interface java.util.Collection<E>
Specified by:
clear in interface java.util.Set<E>
Overrides:
clear in class java.util.AbstractCollection<E>

iterator

public java.util.Iterator<E> iterator()
Specified by:
iterator in interface java.lang.Iterable<E>
Specified by:
iterator in interface java.util.Collection<E>
Specified by:
iterator in interface java.util.Set<E>
Specified by:
iterator in class java.util.AbstractCollection<E>

readOnly

public void readOnly()
Atomically make the set immutable. Future calls to mutate will throw an IllegalStateException. Existing mutator calls in other threads racing with this thread and will either throw IllegalStateException or their update will be visible to this thread. This implies that a simple flag cannot make the Set immutable, because a late-arriving update in another thread might see immutable flag not set yet, then mutate the Set after the readOnly() call returns. This call can be called concurrently (and indeed until the operation completes, all calls on the Set from any thread either complete normally or end up calling readOnly() internally).

This call is useful in debugging multi-threaded programs where the Set is constructed in parallel, but construction completes after some time; and after construction the Set is only read. Making the Set read-only will cause updates arriving after construction is supposedly complete to throw an IllegalStateException.