1. /*
  2. * @(#)CopyOnWriteArraySet.java 1.7 04/06/11
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util.concurrent;
  8. import java.util.*;
  9. /**
  10. * A {@link java.util.Set} that uses {@link
  11. * java.util.concurrent.CopyOnWriteArrayList} for all of its
  12. * operations. Thus, it shares the same basic properties:
  13. * <ul>
  14. * <li>It is best suited for applications in which set sizes generally
  15. * stay small, read-only operations
  16. * vastly outnumber mutative operations, and you need
  17. * to prevent interference among threads during traversal.
  18. * <li>It is thread-safe.
  19. * <li>Mutative operations(add, set, remove, etc) are expensive
  20. * since they usually entail copying the entire underlying array.
  21. * <li>Iterators do not support the mutative remove operation.
  22. * <li>Traversal via iterators is fast and cannot encounter
  23. * interference from other threads. Iterators rely on
  24. * unchanging snapshots of the array at the time the iterators were
  25. * constructed.
  26. * </ul>
  27. *
  28. * <p> <b>Sample Usage.</b> The following code sketch uses a
  29. * copy-on-write set to maintain a set of Handler objects that
  30. * perform some action upon state updates.
  31. *
  32. * <pre>
  33. * class Handler { void handle(); ... }
  34. *
  35. * class X {
  36. * private final CopyOnWriteArraySet<Handler> handlers = new CopyOnWriteArraySet<Handler>();
  37. * public void addHandler(Handler h) { handlers.add(h); }
  38. *
  39. * private long internalState;
  40. * private synchronized void changeState() { internalState = ...; }
  41. *
  42. * public void update() {
  43. * changeState();
  44. * for (Handler handler : handlers)
  45. * handler.handle();
  46. * }
  47. * }
  48. * </pre>
  49. *
  50. * <p>This class is a member of the
  51. * <a href="{@docRoot}/../guide/collections/index.html">
  52. * Java Collections Framework</a>.
  53. *
  54. * @see CopyOnWriteArrayList
  55. * @since 1.5
  56. * @author Doug Lea
  57. * @param <E> the type of elements held in this collection
  58. */
  59. public class CopyOnWriteArraySet<E> extends AbstractSet<E>
  60. implements java.io.Serializable {
  61. private static final long serialVersionUID = 5457747651344034263L;
  62. private final CopyOnWriteArrayList<E> al;
  63. /**
  64. * Creates an empty set.
  65. */
  66. public CopyOnWriteArraySet() {
  67. al = new CopyOnWriteArrayList<E>();
  68. }
  69. /**
  70. * Creates a set containing all of the elements of the specified
  71. * Collection.
  72. * @param c the collection
  73. */
  74. public CopyOnWriteArraySet(Collection<? extends E> c) {
  75. al = new CopyOnWriteArrayList<E>();
  76. al.addAllAbsent(c);
  77. }
  78. public int size() { return al.size(); }
  79. public boolean isEmpty() { return al.isEmpty(); }
  80. public boolean contains(Object o) { return al.contains(o); }
  81. public Object[] toArray() { return al.toArray(); }
  82. public <T> T[] toArray(T[] a) { return al.toArray(a); }
  83. public void clear() { al.clear(); }
  84. public Iterator<E> iterator() { return al.iterator(); }
  85. public boolean remove(Object o) { return al.remove(o); }
  86. public boolean add(E o) { return al.addIfAbsent(o); }
  87. public boolean containsAll(Collection<?> c) { return al.containsAll(c); }
  88. public boolean addAll(Collection<? extends E> c) { return al.addAllAbsent(c) > 0; }
  89. public boolean removeAll(Collection<?> c) { return al.removeAll(c); }
  90. public boolean retainAll(Collection<?> c) { return al.retainAll(c); }
  91. }