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