/
ImmutableTarget.java
133 lines (115 loc) · 4.47 KB
/
ImmutableTarget.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
/*
* Copyright (c) 2017 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
* Contributors:
* Bosch Software Innovations GmbH - initial contribution
*
*/
package org.eclipse.ditto.model.connectivity;
import java.util.Arrays;
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;
/**
* Immutable implementation of {@link Target}.
*/
@Immutable
final class ImmutableTarget implements Target {
private final String address;
private final Set<String> topics;
private ImmutableTarget(final String address, final Set<String> topics) {
this.address = address;
this.topics = Collections.unmodifiableSet(new HashSet<>(topics));
}
/**
* TODO doc
* @param address
* @param topics
* @return
*/
public static Target of(final String address, final Set<String> topics) {
return new ImmutableTarget(address, topics);
}
/**
* TODO doc
* @param address
* @param requiredTopic
* @param additionalTopics
* @return
*/
public static Target of(final String address, final String requiredTopic, final String... additionalTopics) {
final HashSet<String> topics = new HashSet<>(Collections.singletonList(requiredTopic));
topics.addAll(Arrays.asList(additionalTopics));
return new ImmutableTarget(address, topics);
}
@Override
public String getAddress() {
return address;
}
@Override
public Set<String> getTopics() {
return topics;
}
@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(Target.JsonFields.SCHEMA_VERSION, schemaVersion.toInt(), predicate);
jsonObjectBuilder.set(Target.JsonFields.ADDRESS, address, predicate);
jsonObjectBuilder.set(Target.JsonFields.TOPICS, topics.stream()
.map(JsonFactory::newValue)
.collect(JsonCollectors.valuesToArray()), predicate.and(Objects::nonNull));
return jsonObjectBuilder.build();
}
/**
* Creates a new {@code Target} object from the specified JSON object.
*
* @param jsonObject a JSON object which provides the data for the Target 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 Target fromJson(final JsonObject jsonObject) {
final String readAddress = jsonObject.getValueOrThrow(Target.JsonFields.ADDRESS);
final Set<String> readTopics = jsonObject.getValue(JsonFields.TOPICS)
.map(array -> array.stream()
.map(JsonValue::asString)
.collect(Collectors.toSet()))
.orElse(Collections.emptySet());
return ImmutableTarget.of(readAddress, readTopics);
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final ImmutableTarget that = (ImmutableTarget) o;
return Objects.equals(address, that.address) &&
Objects.equals(topics, that.topics);
}
@Override
public int hashCode() {
return Objects.hash(address, topics);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"address=" + address +
", topics=" + topics +
"]";
}
}