1 7 8 package java.util.concurrent; 9 import java.util.*; 10 import sun.misc.Unsafe; 11 12 55 public class ConcurrentSkipListSet<E> 56 extends AbstractSet<E> 57 implements NavigableSet<E>, Cloneable , java.io.Serializable { 58 59 private static final long serialVersionUID = -2479143111061671589L; 60 61 66 private final ConcurrentNavigableMap <E,Object > m; 67 68 72 public ConcurrentSkipListSet() { 73 m = new ConcurrentSkipListMap <E,Object >(); 74 } 75 76 84 public ConcurrentSkipListSet(Comparator<? super E> comparator) { 85 m = new ConcurrentSkipListMap <E,Object >(comparator); 86 } 87 88 99 public ConcurrentSkipListSet(Collection<? extends E> c) { 100 m = new ConcurrentSkipListMap <E,Object >(); 101 addAll(c); 102 } 103 104 112 public ConcurrentSkipListSet(SortedSet<E> s) { 113 m = new ConcurrentSkipListMap <E,Object >(s.comparator()); 114 addAll(s); 115 } 116 117 120 ConcurrentSkipListSet(ConcurrentNavigableMap <E,Object > m) { 121 this.m = m; 122 } 123 124 130 public ConcurrentSkipListSet <E> clone() { 131 ConcurrentSkipListSet <E> clone = null; 132 try { 133 clone = (ConcurrentSkipListSet <E>) super.clone(); 134 clone.setMap(new ConcurrentSkipListMap (m)); 135 } catch (CloneNotSupportedException e) { 136 throw new InternalError (); 137 } 138 139 return clone; 140 } 141 142 143 144 160 public int size() { 161 return m.size(); 162 } 163 164 168 public boolean isEmpty() { 169 return m.isEmpty(); 170 } 171 172 183 public boolean contains(Object o) { 184 return m.containsKey(o); 185 } 186 187 201 public boolean add(E e) { 202 return m.putIfAbsent(e, Boolean.TRUE) == null; 203 } 204 205 219 public boolean remove(Object o) { 220 return m.remove(o, Boolean.TRUE); 221 } 222 223 226 public void clear() { 227 m.clear(); 228 } 229 230 235 public Iterator<E> iterator() { 236 return m.navigableKeySet().iterator(); 237 } 238 239 244 public Iterator<E> descendingIterator() { 245 return m.descendingKeySet().iterator(); 246 } 247 248 249 250 251 263 public boolean equals(Object o) { 264 if (o == this) 266 return true; 267 if (!(o instanceof Set)) 268 return false; 269 Collection<?> c = (Collection<?>) o; 270 try { 271 return containsAll(c) && c.containsAll(this); 272 } catch (ClassCastException unused) { 273 return false; 274 } catch (NullPointerException unused) { 275 return false; 276 } 277 } 278 279 292 public boolean removeAll(Collection<?> c) { 293 boolean modified = false; 295 for (Iterator<?> i = c.iterator(); i.hasNext(); ) 296 if (remove(i.next())) 297 modified = true; 298 return modified; 299 } 300 301 302 303 307 public E lower(E e) { 308 return m.lowerKey(e); 309 } 310 311 315 public E floor(E e) { 316 return m.floorKey(e); 317 } 318 319 323 public E ceiling(E e) { 324 return m.ceilingKey(e); 325 } 326 327 331 public E higher(E e) { 332 return m.higherKey(e); 333 } 334 335 public E pollFirst() { 336 Map.Entry<E,Object > e = m.pollFirstEntry(); 337 return e == null? null : e.getKey(); 338 } 339 340 public E pollLast() { 341 Map.Entry<E,Object > e = m.pollLastEntry(); 342 return e == null? null : e.getKey(); 343 } 344 345 346 347 348 349 public Comparator<? super E> comparator() { 350 return m.comparator(); 351 } 352 353 356 public E first() { 357 return m.firstKey(); 358 } 359 360 363 public E last() { 364 return m.lastKey(); 365 } 366 367 373 public NavigableSet<E> subSet(E fromElement, 374 boolean fromInclusive, 375 E toElement, 376 boolean toInclusive) { 377 return new ConcurrentSkipListSet <E> 378 (m.subMap(fromElement, fromInclusive, 379 toElement, toInclusive)); 380 } 381 382 387 public NavigableSet<E> headSet(E toElement, boolean inclusive) { 388 return new ConcurrentSkipListSet <E>(m.headMap(toElement, inclusive)); 389 } 390 391 396 public NavigableSet<E> tailSet(E fromElement, boolean inclusive) { 397 return new ConcurrentSkipListSet <E>(m.tailMap(fromElement, inclusive)); 398 } 399 400 406 public NavigableSet<E> subSet(E fromElement, E toElement) { 407 return subSet(fromElement, true, toElement, false); 408 } 409 410 415 public NavigableSet<E> headSet(E toElement) { 416 return headSet(toElement, false); 417 } 418 419 424 public NavigableSet<E> tailSet(E fromElement) { 425 return tailSet(fromElement, true); 426 } 427 428 440 public NavigableSet<E> descendingSet() { 441 return new ConcurrentSkipListSet (m.descendingMap()); 442 } 443 444 private static final Unsafe unsafe = Unsafe.getUnsafe(); 446 private static final long mapOffset; 447 static { 448 try { 449 mapOffset = unsafe.objectFieldOffset 450 (ConcurrentSkipListSet .class.getDeclaredField("m")); 451 } catch (Exception ex) { throw new Error (ex); } 452 } 453 private void setMap(ConcurrentNavigableMap <E,Object > map) { 454 unsafe.putObjectVolatile(this, mapOffset, map); 455 } 456 457 } 458 | Popular Tags |