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 final Set<E> set;
27
28
29
30
31
32
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 }