Class AsSynchronizedGraph.CopyOnDemandSet<E>

  • Type Parameters:
    E - the class of the objects in the set
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.Set<E>
    Enclosing class:
    AsSynchronizedGraph<V,​E>

    private static class AsSynchronizedGraph.CopyOnDemandSet<E>
    extends java.lang.Object
    implements java.util.Set<E>, java.io.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 Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Set<E> copy  
      private boolean copyless  
      (package private) java.util.concurrent.locks.ReadWriteLock readWriteLock  
      private static long serialVersionUID  
      private java.util.Set<E> set  
      private static java.lang.String UNMODIFIABLE  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private CopyOnDemandSet​(java.util.Set<E> s, java.util.concurrent.locks.ReadWriteLock readWriteLock, boolean copyless)
      Constructor for CopyOnDemandSet.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(E e)
      boolean addAll​(java.util.Collection<? extends E> c)
      void clear()
      boolean contains​(java.lang.Object o)
      boolean containsAll​(java.util.Collection<?> c)
      boolean equals​(java.lang.Object o)
      Compares the specified object with this set for equality.
      void forEach​(java.util.function.Consumer<? super E> action)
      private java.util.Set<E> getCopy()
      Get the backing set's unmodifiable copy, or a direct reference to the backing set if in copyless mode.
      int hashCode()
      Return the backing set's hashcode.
      boolean isCopyless()
      Return whether copyless mode is used for iteration.
      boolean isEmpty()
      java.util.Iterator<E> iterator()
      Returns an iterator over the elements in the backing set's unmodifiable copy.
      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.
      java.util.stream.Stream<E> parallelStream()
      Return a possibly parallel Stream with the backing set's unmodifiable copy as its source.
      boolean remove​(java.lang.Object o)
      boolean removeAll​(java.util.Collection<?> c)
      boolean removeIf​(java.util.function.Predicate<? super E> filter)
      boolean retainAll​(java.util.Collection<?> c)
      int size()
      java.util.Spliterator<E> spliterator()
      Creates a Spliterator over the elements in the set's unmodifiable copy.
      java.util.stream.Stream<E> stream()
      Return a sequential Stream with the backing set's unmodifiable copy as its source.
      java.lang.Object[] toArray()
      <T> T[] toArray​(T[] a)
      java.lang.String toString()
      Return the backing set's toString result.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        toArray
    • Field Detail

      • set

        private java.util.Set<E> set
      • copyless

        private final boolean copyless
      • copy

        private transient volatile java.util.Set<E> copy
      • readWriteLock

        final java.util.concurrent.locks.ReadWriteLock readWriteLock
    • Constructor Detail

      • CopyOnDemandSet

        private CopyOnDemandSet​(java.util.Set<E> s,
                                java.util.concurrent.locks.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 Detail

      • 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 java.util.Collection<E>
        Specified by:
        size in interface java.util.Set<E>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<E>
        Specified by:
        isEmpty in interface java.util.Set<E>
      • 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>
      • iterator

        public java.util.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 java.util.Collection<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Specified by:
        iterator in interface java.util.Set<E>
        Returns:
        an iterator over the elements in the backing set's unmodifiable copy.
      • toArray

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

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

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

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Collection<E>
        Specified by:
        remove in interface java.util.Set<E>
      • containsAll

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

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

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

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

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

        public void forEach​(java.util.function.Consumer<? super E> action)
        Specified by:
        forEach in interface java.lang.Iterable<E>
      • removeIf

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

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

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

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

        public boolean equals​(java.lang.Object o)
        Compares the specified object with this set for equality.
        Specified by:
        equals in interface java.util.Collection<E>
        Specified by:
        equals in interface java.util.Set<E>
        Overrides:
        equals in class java.lang.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 java.util.Collection<E>
        Specified by:
        hashCode in interface java.util.Set<E>
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the backing set's hashcode.
      • toString

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

        private java.util.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.