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-2022 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   * @param <E> the type of content of this Set
22   */
23  public abstract class ImmutableAbstractSet<E> extends ImmutableAbstractCollection<E> implements ImmutableSet<E>
24  {
25      /** */
26      private static final long serialVersionUID = 20160507L;
27  
28      /** the set that is wrapped, without giving access to methods that can change it. */
29      private final Set<E> set;
30  
31      /**
32       * Construct an abstract immutable set. Make sure that the argument is a safe copy of the set of the right type! Copying
33       * does not take place in the Abstract class!
34       * @param set Set&lt;E&gt;; a safe copy of the set to use for the immutable set
35       * @param copyOrWrap Immutable; indicate whether the immutable is a copy or a wrap
36       */
37      protected ImmutableAbstractSet(final Set<E> set, final Immutable copyOrWrap)
38      {
39          super(copyOrWrap);
40          Throw.whenNull(set, "the collection argument cannot be null");
41          this.set = set;
42      }
43  
44      /** {@inheritDoc} */
45      @Override
46      public final Collection<E> toCollection()
47      {
48          return toSet();
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      protected Set<E> getUnderlyingCollection()
54      {
55          return this.set;
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public final int size()
61      {
62          return this.set.size();
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public final boolean isEmpty()
68      {
69          return this.set.isEmpty();
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public final boolean contains(final Object o)
75      {
76          return this.set.contains(o);
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public final Object[] toArray()
82      {
83          return this.set.toArray();
84      }
85  
86      /** {@inheritDoc} */
87      @Override
88      public final <T> T[] toArray(final T[] a)
89      {
90          return this.set.toArray(a);
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      public final ImmutableIterator<E> iterator()
96      {
97          return new ImmutableIterator<E>(this.set.iterator());
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     public final void forEach(final Consumer<? super E> action)
103     {
104         this.set.forEach(action);
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     public final Spliterator<E> spliterator()
110     {
111         return this.set.spliterator();
112     }
113 
114     /** {@inheritDoc} */
115     @Override
116     public final boolean containsAll(final Collection<?> c)
117     {
118         return this.set.containsAll(c);
119     }
120 
121     /** {@inheritDoc} */
122     @Override
123     public final boolean containsAll(final ImmutableCollection<?> c)
124     {
125         return this.set.containsAll(c.toCollection());
126     }
127 
128     /** {@inheritDoc} */
129     @Override
130     public final Stream<E> stream()
131     {
132         return this.set.stream();
133     }
134 
135     /** {@inheritDoc} */
136     @Override
137     public final Stream<E> parallelStream()
138     {
139         return this.set.parallelStream();
140     }
141 
142     /** {@inheritDoc} */
143     @Override
144     public final boolean isWrap()
145     {
146         return this.copyOrWrap.isWrap();
147     }
148 
149     /** {@inheritDoc} */
150     @Override
151     @SuppressWarnings("checkstyle:designforextension")
152     public int hashCode()
153     {
154         final int prime = 31;
155         int result = 1;
156         result = prime * result + ((this.set == null) ? 0 : this.set.hashCode());
157         return result;
158     }
159 
160     /** {@inheritDoc} */
161     @Override
162     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
163     public boolean equals(final Object obj)
164     {
165         if (this == obj)
166             return true;
167         if (obj == null)
168             return false;
169         if (getClass() != obj.getClass())
170             return false;
171         ImmutableAbstractSet<?> other = (ImmutableAbstractSet<?>) obj;
172         if (this.set == null)
173         {
174             if (other.set != null)
175                 return false;
176         }
177         else if (!this.set.equals(other.set))
178             return false;
179         return true;
180     }
181 
182 }