View Javadoc
1   package org.djutils.immutablecollections;
2   
3   import java.util.Collection;
4   import java.util.Set;
5   import java.util.Spliterator;
6   import java.util.function.Consumer;
7   import java.util.stream.Stream;
8   
9   import org.djutils.exceptions.Throw;
10  
11  /**
12   * An abstract base class for an immutable wrapper for a Set.
13   * <p>
14   * Copyright (c) 2016-2019 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
15   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
16   * distributed under a three-clause BSD-style license, which can be found at
17   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
18   * </p>
19   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
20   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
21   * @author <a href="https://www.transport.citg.tudelft.nl">Wouter Schakel</a>
22   * @param <E> the type of content of this Set
23   */
24  public abstract class ImmutableAbstractSet<E> extends ImmutableAbstractCollection<E> implements ImmutableSet<E>
25  {
26      /** */
27      private static final long serialVersionUID = 20160507L;
28  
29      /** the set that is wrapped, without giving access to methods that can change it. */
30      private final Set<E> set;
31  
32      /**
33       * Construct an abstract immutable set. Make sure that the argument is a safe copy of the set of the right type! Copying
34       * does not take place in the Abstract class!
35       * @param set Set&lt;E&gt;; a safe copy of the set to use for the immutable set
36       * @param copyOrWrap Immutable; indicate whether the immutable is a copy or a wrap
37       */
38      protected ImmutableAbstractSet(final Set<E> set, final Immutable copyOrWrap)
39      {
40          super(copyOrWrap);
41          Throw.whenNull(set, "the collection argument cannot be null");
42          this.set = set;
43      }
44  
45      /** {@inheritDoc} */
46      @Override
47      public final Collection<E> toCollection()
48      {
49          return toSet();
50      }
51  
52      /** {@inheritDoc} */
53      @Override
54      protected Set<E> getCollection()
55      {
56          return this.set;
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public final int size()
62      {
63          return this.set.size();
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public final boolean isEmpty()
69      {
70          return this.set.isEmpty();
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public final boolean contains(final Object o)
76      {
77          return this.set.contains(o);
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public final Object[] toArray()
83      {
84          return this.set.toArray();
85      }
86  
87      /** {@inheritDoc} */
88      @Override
89      public final <T> T[] toArray(final T[] a)
90      {
91          return this.set.toArray(a);
92      }
93  
94      /** {@inheritDoc} */
95      @Override
96      public final ImmutableIterator<E> iterator()
97      {
98          return new ImmutableIterator<E>(this.set.iterator());
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     public final void forEach(final Consumer<? super E> action)
104     {
105         this.set.forEach(action);
106     }
107 
108     /** {@inheritDoc} */
109     @Override
110     public final Spliterator<E> spliterator()
111     {
112         return this.set.spliterator();
113     }
114 
115     /** {@inheritDoc} */
116     @Override
117     public final boolean containsAll(final Collection<?> c)
118     {
119         return this.set.containsAll(c);
120     }
121 
122     /** {@inheritDoc} */
123     @Override
124     public final boolean containsAll(final ImmutableCollection<?> c)
125     {
126         return this.set.containsAll(c.toCollection());
127     }
128 
129     /** {@inheritDoc} */
130     @Override
131     public final Stream<E> stream()
132     {
133         return this.set.stream();
134     }
135 
136     /** {@inheritDoc} */
137     @Override
138     public final Stream<E> parallelStream()
139     {
140         return this.set.parallelStream();
141     }
142 
143     /** {@inheritDoc} */
144     @Override
145     public final boolean isWrap()
146     {
147         return this.copyOrWrap.isWrap();
148     }
149 
150     /** {@inheritDoc} */
151     @Override
152     @SuppressWarnings("checkstyle:designforextension")
153     public int hashCode()
154     {
155         final int prime = 31;
156         int result = 1;
157         result = prime * result + ((this.set == null) ? 0 : this.set.hashCode());
158         return result;
159     }
160 
161     /** {@inheritDoc} */
162     @Override
163     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
164     public boolean equals(final Object obj)
165     {
166         if (this == obj)
167             return true;
168         if (obj == null)
169             return false;
170         if (getClass() != obj.getClass())
171             return false;
172         ImmutableAbstractSet<?> other = (ImmutableAbstractSet<?>) obj;
173         if (this.set == null)
174         {
175             if (other.set != null)
176                 return false;
177         }
178         else if (!this.set.equals(other.set))
179             return false;
180         return true;
181     }
182 
183     /** {@inheritDoc} */
184     @Override
185     @SuppressWarnings("checkstyle:designforextension")
186     public String toString()
187     {
188         return "Immutable[" + this.set.toString() + "]";
189     }
190 }