Class AsSynchronizedGraph.CopyOnDemandSet<E>

java.lang.Object
org.jgrapht.graph.concurrent.AsSynchronizedGraph.CopyOnDemandSet<E>
Type Parameters:
E - the class of the objects in the set
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, Set<E>
Enclosing class:
AsSynchronizedGraph<V,E>

private static class AsSynchronizedGraph.CopyOnDemandSet<E> extends Object implements Set<E>, Serializable
Create a synchronized (thread-safe) and unmodifiable Set backed by the specified Set. In order to guarantee serial access, it is critical that all access to the backing Set is accomplished through the created Set.

When a traversal over the set is started via a method such as iterator(), a snapshot of the underlying set is copied for iteration purposes (unless copyless mode is enabled).

The created Set's hashCode is equal to the backing Set's hashCode. And the created Set is equal to another set if they are the same Set or the backing Set is equal to the other Set.

The created set will be serializable if the backing set is serializable.

  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • set

      private Set<E> set
    • copyless

      private final boolean copyless
    • copy

      private transient volatile Set<E> copy
    • readWriteLock

      final ReadWriteLock readWriteLock
    • UNMODIFIABLE

      private static final String UNMODIFIABLE
      See Also:
  • Constructor Details

    • CopyOnDemandSet

      private CopyOnDemandSet(Set<E> s, ReadWriteLock readWriteLock, boolean copyless)
      Constructor for CopyOnDemandSet.
      Parameters:
      s - the backing set.
      readWriteLock - the ReadWriteLock on which to locked
      copyless - whether copyless mode should be used
  • Method Details

    • isCopyless

      public boolean isCopyless()
      Return whether copyless mode is used for iteration.
      Returns:
      true if the set uses copyless mode, false otherwise
    • size

      public int size()
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface Set<E>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<E>
      Specified by:
      isEmpty in interface Set<E>
    • contains

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface Set<E>
    • iterator

      public Iterator<E> iterator()
      Returns an iterator over the elements in the backing set's unmodifiable copy. The elements are returned in the same order of the backing set.
      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface Set<E>
      Returns:
      an iterator over the elements in the backing set's unmodifiable copy.
    • toArray

      public Object[] toArray()
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface Set<E>
    • toArray

      public <T> T[] toArray(T[] a)
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface Set<E>
    • add

      public boolean add(E e)
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface Set<E>
    • remove

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface Set<E>
    • containsAll

      public boolean containsAll(Collection<?> c)
      Specified by:
      containsAll in interface Collection<E>
      Specified by:
      containsAll in interface Set<E>
    • addAll

      public boolean addAll(Collection<? extends E> c)
      Specified by:
      addAll in interface Collection<E>
      Specified by:
      addAll in interface Set<E>
    • retainAll

      public boolean retainAll(Collection<?> c)
      Specified by:
      retainAll in interface Collection<E>
      Specified by:
      retainAll in interface Set<E>
    • removeAll

      public boolean removeAll(Collection<?> c)
      Specified by:
      removeAll in interface Collection<E>
      Specified by:
      removeAll in interface Set<E>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<E>
      Specified by:
      clear in interface Set<E>
    • forEach

      public void forEach(Consumer<? super E> action)
      Specified by:
      forEach in interface Iterable<E>
    • removeIf

      public boolean removeIf(Predicate<? super E> filter)
      Specified by:
      removeIf in interface Collection<E>
    • spliterator

      public Spliterator<E> spliterator()
      Creates a Spliterator over the elements in the set's unmodifiable copy.
      Specified by:
      spliterator in interface Collection<E>
      Specified by:
      spliterator in interface Iterable<E>
      Specified by:
      spliterator in interface Set<E>
      Returns:
      a Spliterator over the elements in the backing set's unmodifiable copy.
    • stream

      public Stream<E> stream()
      Return a sequential Stream with the backing set's unmodifiable copy as its source.
      Specified by:
      stream in interface Collection<E>
      Returns:
      a sequential Stream with the backing set's unmodifiable copy as its source.
    • parallelStream

      public Stream<E> parallelStream()
      Return a possibly parallel Stream with the backing set's unmodifiable copy as its source.
      Specified by:
      parallelStream in interface Collection<E>
      Returns:
      a possibly parallel Stream with the backing set's unmodifiable copy as its source.
    • equals

      public boolean equals(Object o)
      Compares the specified object with this set for equality.
      Specified by:
      equals in interface Collection<E>
      Specified by:
      equals in interface Set<E>
      Overrides:
      equals in class Object
      Parameters:
      o - object to be compared for equality with this set.
      Returns:
      true if o and this set are the same object or o is equal to the backing object, false otherwise.
    • hashCode

      public int hashCode()
      Return the backing set's hashcode.
      Specified by:
      hashCode in interface Collection<E>
      Specified by:
      hashCode in interface Set<E>
      Overrides:
      hashCode in class Object
      Returns:
      the backing set's hashcode.
    • toString

      public String toString()
      Return the backing set's toString result.
      Overrides:
      toString in class Object
      Returns:
      the backing set's toString result.
    • getCopy

      private Set<E> getCopy()
      Get the backing set's unmodifiable copy, or a direct reference to the backing set if in copyless mode.
      Returns:
      the backing set or its unmodifiable copy
    • modified

      private void modified()
      If the backing set is modified, call this method to let this set knows the backing set's copy need to update.