-
Notifications
You must be signed in to change notification settings - Fork 215
/
ImmutablePolicyImport.java
147 lines (127 loc) · 5.81 KB
/
ImmutablePolicyImport.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
/*
* 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.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.exceptions.DittoJsonException;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonParseException;
import org.eclipse.ditto.json.JsonValue;
/**
* An immutable implementation of {@link PolicyImport}.
*/
@Immutable
final class ImmutablePolicyImport implements PolicyImport {
private final PolicyId importedPolicyId;
@Nullable private final EffectedImports effectedImports;
private ImmutablePolicyImport(final PolicyId importedPolicyId,
@Nullable final EffectedImports effectedPermissions) {
this.importedPolicyId = checkNotNull(importedPolicyId, "importedPolicyId");
this.effectedImports = effectedPermissions;
}
/**
* Creates a new {@code PolicyImport} object based on the given {@code importedPolicyId} with empty
* {@code effectedImports}.
*
* @param importedPolicyId the {@code PolicyId} where entries will be imported from.
* @return a new {@code PolicyImport} object.
* @throws NullPointerException if any argument is {@code null}.
*/
public static PolicyImport of(final PolicyId importedPolicyId) {
return new ImmutablePolicyImport(importedPolicyId, null);
}
/**
* Creates a new {@code PolicyImport} object based on the given {@code importedPolicyId} and {@code effectedImports}.
*
* @param importedPolicyId the {@code PolicyId} where entries will be imported from.
* @param effectedImports lists every {@code PolicyEntry} label from the imported {@code Policy} that will be either
* included or excluded - if {@code null}, all policy entries will be imported.
* @return a new {@code PolicyImport} object.
* @throws NullPointerException if {@code importedPolicyId} is {@code null}.
*/
public static PolicyImport of(final PolicyId importedPolicyId, @Nullable final EffectedImports effectedImports) {
return new ImmutablePolicyImport(importedPolicyId, effectedImports);
}
/**
* Creates a new {@code PolicyImport} object based on the given {@code importedPolicyId} and {@code jsonValue}.
*
* @param importedPolicyId the {@code PolicyId} where entries will be imported from.
* @param jsonValue the JSON value containing the effected permissions for the PolicyImport. This value is supposed to
* be a {@link JsonObject}.
* @return a new {@code PolicyImport} object.
* @throws NullPointerException if any argument is {@code null}.
* @throws DittoJsonException if {@code jsonValue} is not a JSON object or the JSON has not the expected format.
*/
public static PolicyImport fromJson(final PolicyId importedPolicyId, final JsonValue jsonValue) {
checkNotNull(jsonValue, "jsonValue");
final Optional<JsonObject> jsonFields = Optional.of(jsonValue)
.filter(JsonValue::isObject)
.map(JsonValue::asObject);
final JsonObject jsonObject = wrapJsonRuntimeException(() ->
jsonFields.orElseThrow(() -> new DittoJsonException(JsonParseException.newBuilder()
.message("The value of the policy import of the 'imported policy' with ID '" +
importedPolicyId + "' is missing or not an object.")
.build())));
final EffectedImports effectedImports = jsonObject.isEmpty() ? null :
ImmutableEffectedImports.fromJson(jsonObject);
return of(importedPolicyId, effectedImports);
}
@Override
public PolicyId getImportedPolicyId() {
return importedPolicyId;
}
@Override
public Optional<EffectedImports> getEffectedImports() {
return Optional.ofNullable(effectedImports);
}
@Override
public JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate) {
final JsonObjectBuilder jsonObjectBuilder = JsonFactory.newObjectBuilder();
if (null != effectedImports) {
jsonObjectBuilder.setAll(effectedImports.toJson(schemaVersion, thePredicate));
}
return jsonObjectBuilder.build();
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final ImmutablePolicyImport resource = (ImmutablePolicyImport) o;
return Objects.equals(importedPolicyId, resource.importedPolicyId) &&
Objects.equals(effectedImports, resource.effectedImports);
}
@Override
public int hashCode() {
return Objects.hash(importedPolicyId, effectedImports);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"importedPolicyId=" + importedPolicyId +
", effectedImports=" + effectedImports +
"]";
}
}