java.util.concurrent.ConcurrentSkipListSet Maven / Gradle / Ivy
Show all versions of jtransc-rt Show documentation
/*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
*/
package java.util.concurrent;
import java.util.*;
// BEGIN android-note
// removed link to collections framework docs
// END android-note
/**
* A scalable concurrent {@link NavigableSet} implementation based on
* a {@link ConcurrentSkipListMap}. The elements of the set are kept
* sorted according to their {@linkplain Comparable natural ordering},
* or by a {@link Comparator} provided at set creation time, depending
* on which constructor is used.
*
* This implementation provides expected average log(n) time
* cost for the {@code contains}, {@code add}, and {@code remove}
* operations and their variants. Insertion, removal, and access
* operations safely execute concurrently by multiple threads.
* Iterators are weakly consistent, returning elements
* reflecting the state of the set at some point at or since the
* creation of the iterator. They do not throw {@link
* ConcurrentModificationException}, and may proceed concurrently with
* other operations. Ascending ordered views and their iterators are
* faster than descending ones.
*
*
Beware that, unlike in most collections, the {@code size}
* method is not a constant-time operation. Because of the
* asynchronous nature of these sets, determining the current number
* of elements requires a traversal of the elements, and so may report
* inaccurate results if this collection is modified during traversal.
* Additionally, the bulk operations {@code addAll},
* {@code removeAll}, {@code retainAll}, {@code containsAll},
* {@code equals}, and {@code toArray} are not guaranteed
* to be performed atomically. For example, an iterator operating
* concurrently with an {@code addAll} operation might view only some
* of the added elements.
*
*
This class and its iterators implement all of the
* optional methods of the {@link Set} and {@link Iterator}
* interfaces. Like most other concurrent collection implementations,
* this class does not permit the use of {@code null} elements,
* because {@code null} arguments and return values cannot be reliably
* distinguished from the absence of elements.
*
* @author Doug Lea
* @param the type of elements maintained by this set
* @since 1.6
*/
public class ConcurrentSkipListSet extends AbstractSet implements NavigableSet, Cloneable, java.io.Serializable {
ConcurrentNavigableMap m;
public ConcurrentSkipListSet() {
m = new ConcurrentSkipListMap();
}
public ConcurrentSkipListSet(Comparator super E> comparator) {
m = new ConcurrentSkipListMap(comparator);
}
public ConcurrentSkipListSet(Collection extends E> c) {
m = new ConcurrentSkipListMap();
addAll(c);
}
public ConcurrentSkipListSet(SortedSet s) {
m = new ConcurrentSkipListMap(s.comparator());
addAll(s);
}
ConcurrentSkipListSet(ConcurrentNavigableMap m) {
this.m = m;
}
public ConcurrentSkipListSet clone() {
try {
@SuppressWarnings("unchecked")
ConcurrentSkipListSet clone =
(ConcurrentSkipListSet) super.clone();
clone.m = new ConcurrentSkipListMap(m);
return clone;
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
public int size() {
return m.size();
}
public boolean isEmpty() {
return m.isEmpty();
}
public boolean contains(Object o) {
return m.containsKey(o);
}
public boolean add(E e) {
return m.putIfAbsent(e, Boolean.TRUE) == null;
}
public boolean remove(Object o) {
return m.remove(o, Boolean.TRUE);
}
public void clear() {
m.clear();
}
public Iterator iterator() {
return m.navigableKeySet().iterator();
}
public Iterator descendingIterator() {
return m.descendingKeySet().iterator();
}
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Set)) return false;
Collection> c = (Collection>) o;
try {
return containsAll(c) && c.containsAll(this);
} catch (ClassCastException unused) {
return false;
} catch (NullPointerException unused) {
return false;
}
}
public boolean removeAll(Collection> c) {
boolean modified = false;
for (Object e : c) if (remove(e)) modified = true;
return modified;
}
public E lower(E e) {
return m.lowerKey(e);
}
public E floor(E e) {
return m.floorKey(e);
}
public E ceiling(E e) {
return m.ceilingKey(e);
}
public E higher(E e) {
return m.higherKey(e);
}
public E pollFirst() {
Map.Entry e = m.pollFirstEntry();
return (e == null) ? null : e.getKey();
}
public E pollLast() {
Map.Entry e = m.pollLastEntry();
return (e == null) ? null : e.getKey();
}
public Comparator super E> comparator() {
return m.comparator();
}
public E first() {
return m.firstKey();
}
public E last() {
return m.lastKey();
}
public NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
return new ConcurrentSkipListSet
(m.subMap(fromElement, fromInclusive,
toElement, toInclusive));
}
public NavigableSet headSet(E toElement, boolean inclusive) {
return new ConcurrentSkipListSet(m.headMap(toElement, inclusive));
}
public NavigableSet tailSet(E fromElement, boolean inclusive) {
return new ConcurrentSkipListSet(m.tailMap(fromElement, inclusive));
}
public NavigableSet subSet(E fromElement, E toElement) {
return subSet(fromElement, true, toElement, false);
}
public NavigableSet headSet(E toElement) {
return headSet(toElement, false);
}
public NavigableSet tailSet(E fromElement) {
return tailSet(fromElement, true);
}
public NavigableSet descendingSet() {
return new ConcurrentSkipListSet(m.descendingMap());
}
}