-
Notifications
You must be signed in to change notification settings - Fork 215
/
ImmutableEffectedImports.java
164 lines (142 loc) · 6.34 KB
/
ImmutableEffectedImports.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
/*
* 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 static org.eclipse.ditto.base.model.exceptions.DittoJsonException.wrapJsonRuntimeException;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonValue;
/**
* An immutable implementation of {@link EffectedImports}.
*/
@Immutable
final class ImmutableEffectedImports implements EffectedImports {
private final ImportedLabels includedImportedLabels;
private final ImportedLabels excludedImportedLabels;
private ImmutableEffectedImports(final ImportedLabels included, final ImportedLabels excluded) {
includedImportedLabels = included;
excludedImportedLabels = excluded;
}
/**
* Returns a new {@code EffectedImports} object of the given {@code includedImportedLabels} and {@code
* excludedImportedLabels}.
*
* @param includedImportedLabels the ImportedLabels which should be added as "included".
* @param excludedImportedLabels the ImportedLabels which should be added as "excluded".
* @return a new {@code EffectedImports} object.
* @throws NullPointerException if any argument is {@code null}.
*/
public static EffectedImports of(final Iterable<Label> includedImportedLabels,
final Iterable<Label> excludedImportedLabels) {
final ImportedLabels included =
toImportedEntries(toSet(checkNotNull(includedImportedLabels, "includedImportedLabels")));
final ImportedLabels excluded =
toImportedEntries(toSet(checkNotNull(excludedImportedLabels, "excludedImportedLabels")));
return new ImmutableEffectedImports(included, excluded);
}
private static Collection<Label> toSet(final Iterable<Label> iterable) {
final Set<Label> result;
if (iterable instanceof Set) {
result = (Set<Label>) iterable;
} else {
result = new LinkedHashSet<>();
iterable.forEach(result::add);
}
return result;
}
private static ImportedLabels toImportedEntries(final Collection<Label> labels) {
return labels.isEmpty()
? PoliciesModelFactory.noImportedEntries()
: PoliciesModelFactory.newImportedEntries(labels);
}
/**
* Creates a new {@code EffectedImports} object from the specified JSON object.
*
* @param jsonObject a JSON object which provides the data for the EffectedImports to be created.
* @return a new EffectedImports which is initialised with the extracted data from {@code jsonObject}.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
* @throws org.eclipse.ditto.base.model.exceptions.DittoJsonException if the passed in {@code jsonObject} was not in
* the expected 'EffectedImports' format.
*/
public static EffectedImports fromJson(final JsonObject jsonObject) {
checkNotNull(jsonObject, "JSON object");
final Set<Label> included =
wrapJsonRuntimeException(() -> getImportedEntriesFor(jsonObject, JsonFields.INCLUDED));
final Set<Label> excluded =
wrapJsonRuntimeException(() -> getImportedEntriesFor(jsonObject, JsonFields.EXCLUDED));
return of(included, excluded);
}
private static Set<Label> getImportedEntriesFor(final JsonObject jsonObject,
final JsonFieldDefinition<JsonArray> effect) {
return jsonObject.getValue(effect)
.orElse(JsonArray.empty())
.stream()
.filter(JsonValue::isString)
.map(JsonValue::asString)
.map(Label::of)
.collect(Collectors.toCollection(LinkedHashSet::new));
}
@Override
public ImportedLabels getImportedLabels(final ImportedEffect effect) {
switch (checkNotNull(effect, "imported entry effect")) {
case INCLUDED:
return PoliciesModelFactory.newImportedEntries(includedImportedLabels);
case EXCLUDED:
return PoliciesModelFactory.newImportedEntries(excludedImportedLabels);
default:
throw new IllegalArgumentException("Imported effect <" + effect + "> is unknown!");
}
}
@Override
public JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate) {
final Predicate<JsonField> predicate = schemaVersion.and(thePredicate);
return JsonFactory.newObjectBuilder()
.set(JsonFields.INCLUDED, includedImportedLabels.toJson(), predicate)
.set(JsonFields.EXCLUDED, excludedImportedLabels.toJson(), predicate)
.build();
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final ImmutableEffectedImports that = (ImmutableEffectedImports) o;
return Objects.equals(includedImportedLabels, that.includedImportedLabels) && Objects
.equals(excludedImportedLabels, that.excludedImportedLabels);
}
@Override
public int hashCode() {
return Objects.hash(includedImportedLabels, excludedImportedLabels);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"includedImportedLabels=" + includedImportedLabels +
", excludedImportedLabels=" + excludedImportedLabels +
"]";
}
}