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
13
14
15
16
17
18
19
20
21
22
23 public abstract class ImmutableAbstractSet<E> extends ImmutableAbstractCollection<E> implements ImmutableSet<E>
24 {
25
26 private static final long serialVersionUID = 20160507L;
27
28
29 private final Set<E> set;
30
31
32
33
34
35
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
45 @Override
46 public final Collection<E> toCollection()
47 {
48 return toSet();
49 }
50
51
52 @Override
53 protected Set<E> getUnderlyingCollection()
54 {
55 return this.set;
56 }
57
58
59 @Override
60 public final int size()
61 {
62 return this.set.size();
63 }
64
65
66 @Override
67 public final boolean isEmpty()
68 {
69 return this.set.isEmpty();
70 }
71
72
73 @Override
74 public final boolean contains(final Object o)
75 {
76 return this.set.contains(o);
77 }
78
79
80 @Override
81 public final Object[] toArray()
82 {
83 return this.set.toArray();
84 }
85
86
87 @Override
88 public final <T> T[] toArray(final T[] a)
89 {
90 return this.set.toArray(a);
91 }
92
93
94 @Override
95 public final ImmutableIterator<E> iterator()
96 {
97 return new ImmutableIterator<E>(this.set.iterator());
98 }
99
100
101 @Override
102 public final void forEach(final Consumer<? super E> action)
103 {
104 this.set.forEach(action);
105 }
106
107
108 @Override
109 public final Spliterator<E> spliterator()
110 {
111 return this.set.spliterator();
112 }
113
114
115 @Override
116 public final boolean containsAll(final Collection<?> c)
117 {
118 return this.set.containsAll(c);
119 }
120
121
122 @Override
123 public final boolean containsAll(final ImmutableCollection<?> c)
124 {
125 return this.set.containsAll(c.toCollection());
126 }
127
128
129 @Override
130 public final Stream<E> stream()
131 {
132 return this.set.stream();
133 }
134
135
136 @Override
137 public final Stream<E> parallelStream()
138 {
139 return this.set.parallelStream();
140 }
141
142
143 @Override
144 public final boolean isWrap()
145 {
146 return this.copyOrWrap.isWrap();
147 }
148
149
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
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 }