-
Notifications
You must be signed in to change notification settings - Fork 215
/
ImmutableImportedLabels.java
218 lines (182 loc) · 6.8 KB
/
ImmutableImportedLabels.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.policies.model;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonArrayBuilder;
import org.eclipse.ditto.json.JsonFactory;
/**
* Immutable implementation of {@link ImportedLabels}.
*/
@Immutable
final class ImmutableImportedLabels extends AbstractSet<Label> implements ImportedLabels {
private final Set<Label> entryLabels;
private ImmutableImportedLabels(final Set<Label> entryLabels) {
checkNotNull(entryLabels, "entryLabels");
this.entryLabels = Collections.unmodifiableSet(new LinkedHashSet<>(entryLabels));
}
/**
* Returns a new empty set of ImportedLabels.
*
* @return a new empty set of ImportedLabels.
*/
public static ImportedLabels none() {
return new ImmutableImportedLabels(Collections.emptySet());
}
/**
* Returns a new {@code ImportedLabels} object which is initialised with the given entry labels.
*
* @param entryLabel the mandatory entryLabel to initialise the result with.
* @param furtherEntryLabels additional entryLabels to initialise the result with.
* @return a new {@code ImportedLabels} object which is initialised with {@code entryLabel} and {@code
* furtherEntryLabels}.
* @throws NullPointerException if any argument is {@code null}.
*/
public static ImportedLabels of(final CharSequence entryLabel, final CharSequence... furtherEntryLabels) {
checkNotNull(entryLabel, "entryLabel");
checkNotNull(furtherEntryLabels, "furtherEntryLabels");
final Set<Label> entryLabels = new LinkedHashSet<>(1 + furtherEntryLabels.length);
entryLabels.add(Label.of(entryLabel));
Collections.addAll(entryLabels,
Arrays.stream(furtherEntryLabels).map(Label::of).toArray(Label[]::new));
return new ImmutableImportedLabels(entryLabels);
}
/**
* Returns a new {@code ImportedLabels} object which is initialised with the given entryLabels.
*
* @param entryLabels the entryLabels to initialise the result with.
* @return a new {@code ImportedLabels} object which is initialised with {@code entryLabels}.
* @throws NullPointerException if {@code entryLabels} is {@code null}.
*/
public static ImportedLabels of(final Collection<Label> entryLabels) {
checkNotNull(entryLabels, "entryLabels");
final Set<Label> entryLabelSet = new LinkedHashSet<>(entryLabels);
return new ImmutableImportedLabels(entryLabelSet);
}
@Override
public boolean contains(final CharSequence label, final CharSequence... furtherLabels) {
checkNotNull(label, "label");
checkNotNull(furtherLabels, "furtherLabels");
final Set<Label> entryLabelSet = new LinkedHashSet<>();
entryLabelSet.add(Label.of(label));
entryLabelSet.addAll(Arrays.stream(furtherLabels)
.map(Label::of)
.collect(Collectors.toCollection(LinkedHashSet::new)));
return entryLabels.containsAll(entryLabelSet);
}
@Override
public boolean contains(final ImportedLabels importedLabels) {
checkNotNull(importedLabels, "importedLabels");
return this.entryLabels.containsAll(importedLabels);
}
@Override
public int size() {
return entryLabels.size();
}
@Override
public JsonArray toJson() {
final JsonArrayBuilder jsonArrayBuilder = JsonFactory.newArrayBuilder();
entryLabels.stream().map(Label::toString).forEach(jsonArrayBuilder::add);
return jsonArrayBuilder.build();
}
// now all methods from Collection which should not be supported as we have an immutable data structure:
@Nonnull
@Override
public Iterator<Label> iterator() {
return new Iterator<Label>() {
private final Iterator<Label> i = entryLabels.iterator();
@Override
public boolean hasNext() {
return i.hasNext();
}
@Override
public Label next() {
return i.next();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public void forEachRemaining(final Consumer<? super Label> action) {
// Use backing collection version
i.forEachRemaining(action);
}
};
}
@Override
public boolean add(final Label e) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(final Object o) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(@Nonnull final Collection<? extends Label> coll) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(@Nonnull final Collection<?> coll) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(@Nonnull final Collection<?> coll) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
@Override
public boolean removeIf(final Predicate<? super Label> filter) {
throw new UnsupportedOperationException();
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
final ImmutableImportedLabels strings = (ImmutableImportedLabels) o;
return Objects.equals(entryLabels, strings.entryLabels);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), entryLabels);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"entryLabels=" + entryLabels +
"]";
}
}