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 @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 }