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-2024 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      @Override
45      public final Collection<E> toCollection()
46      {
47          return toSet();
48      }
49  
50      @Override
51      protected Set<E> getUnderlyingCollection()
52      {
53          return this.set;
54      }
55  
56      @Override
57      public final int size()
58      {
59          return this.set.size();
60      }
61  
62      @Override
63      public final boolean isEmpty()
64      {
65          return this.set.isEmpty();
66      }
67  
68      @Override
69      public final boolean contains(final Object o)
70      {
71          return this.set.contains(o);
72      }
73  
74      @Override
75      public final Object[] toArray()
76      {
77          return this.set.toArray();
78      }
79  
80      @Override
81      public final <T> T[] toArray(final T[] a)
82      {
83          return this.set.toArray(a);
84      }
85  
86      @Override
87      public final ImmutableIterator<E> iterator()
88      {
89          return new ImmutableIterator<E>(this.set.iterator());
90      }
91  
92      @Override
93      public final void forEach(final Consumer<? super E> action)
94      {
95          this.set.forEach(action);
96      }
97  
98      @Override
99      public final Spliterator<E> spliterator()
100     {
101         return this.set.spliterator();
102     }
103 
104     @Override
105     public final boolean containsAll(final Collection<?> c)
106     {
107         return this.set.containsAll(c);
108     }
109 
110     @Override
111     public final boolean containsAll(final ImmutableCollection<?> c)
112     {
113         return this.set.containsAll(c.toCollection());
114     }
115 
116     @Override
117     public final Stream<E> stream()
118     {
119         return this.set.stream();
120     }
121 
122     @Override
123     public final Stream<E> parallelStream()
124     {
125         return this.set.parallelStream();
126     }
127 
128     @Override
129     public final boolean isWrap()
130     {
131         return this.copyOrWrap.isWrap();
132     }
133 
134     @Override
135     @SuppressWarnings("checkstyle:designforextension")
136     public int hashCode()
137     {
138         final int prime = 31;
139         int result = 1;
140         result = prime * result + ((this.set == null) ? 0 : this.set.hashCode());
141         return result;
142     }
143 
144     @Override
145     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
146     public boolean equals(final Object obj)
147     {
148         if (this == obj)
149             return true;
150         if (obj == null)
151             return false;
152         if (getClass() != obj.getClass())
153             return false;
154         ImmutableAbstractSet<?> other = (ImmutableAbstractSet<?>) obj;
155         if (this.set == null)
156         {
157             if (other.set != null)
158                 return false;
159         }
160         else if (!this.set.equals(other.set))
161             return false;
162         return true;
163     }
164 
165 }