/
ImmutableEnforcement.java
127 lines (111 loc) · 4.54 KB
/
ImmutableEnforcement.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
/*
* Copyright (c) 2017-2018 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
* SPDX-License-Identifier: EPL-2.0
*
*/
package org.eclipse.ditto.model.connectivity;
import java.util.Collections;
import java.util.HashSet;
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.json.JsonCollectors;
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.JsonValue;
import org.eclipse.ditto.model.base.json.JsonSchemaVersion;
/**
* Holds the data required to apply enforcement of Thing IDs. The target can be an arbitrary String containing the ID,
* which must match against the passed filters (which may contain placeholders like {@code {{ thing:id }}} etc.
*/
@Immutable
final class ImmutableEnforcement implements Enforcement {
private final String input;
private final Set<String> filters;
private ImmutableEnforcement(final String input, final Set<String> filters) {
this.input = input;
this.filters = Collections.unmodifiableSet(new HashSet<>(filters));
}
/**
* Create a ThingIdEnforcement with default error message.
*
* @param input input of the signal to apply Thing ID enforcement.
* @param filters filters to match against the input .
* @return ThingIdEnforcement with default error message.
*/
static ImmutableEnforcement of(final String input, final Set<String> filters) {
return new ImmutableEnforcement(input, filters);
}
/**
* Retrieve the string to match against filters.
*
* @return the string that is supposed to match one of the filters.
*/
public String getInput() {
return input;
}
/**
* Retrieve set of filters that are comared against the input string.
* Filters contain placeholders ({@code {{ ... }}}).
*
* @return the filters.
*/
public Set<String> getFilters() {
return filters;
}
@Override
public JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate) {
final Predicate<JsonField> predicate = schemaVersion.and(thePredicate);
final JsonObjectBuilder jsonObjectBuilder = JsonFactory.newObjectBuilder();
jsonObjectBuilder.set(JsonFields.INPUT, input, predicate);
jsonObjectBuilder.set(JsonFields.FILTERS, filters.stream()
.map(JsonFactory::newValue)
.collect(JsonCollectors.valuesToArray()), predicate.and(Objects::nonNull));
return jsonObjectBuilder.build();
}
/**
* Creates a new {@code Source} object from the specified JSON object.
*
* @param jsonObject a JSON object which provides the data for the Enforcement to be created.
* @return a new Source which is initialised with the extracted data from {@code jsonObject}.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
* @throws org.eclipse.ditto.json.JsonParseException if {@code jsonObject} is not an appropriate JSON object.
*/
public static Enforcement fromJson(final JsonObject jsonObject) {
final Set<String> readFilters = jsonObject.getValue(JsonFields.FILTERS)
.map(array -> array.stream()
.map(JsonValue::asString)
.collect(Collectors.toSet())).orElse(Collections.emptySet());
final String readInput =
jsonObject.getValueOrThrow(JsonFields.INPUT);
return new ImmutableEnforcement(readInput, readFilters);
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final ImmutableEnforcement that = (ImmutableEnforcement) o;
return Objects.equals(input, that.input) &&
Objects.equals(filters, that.filters);
}
@Override
public int hashCode() {
return Objects.hash(input, filters);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"input=" + input +
", filters=" + filters +
"]";
}
}