-
Notifications
You must be signed in to change notification settings - Fork 215
/
MergedJsonObjectMap.java
155 lines (132 loc) · 4.85 KB
/
MergedJsonObjectMap.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
/*
* Copyright (c) 2020 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.connectivity.service.mapping;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.json.JsonCollectors;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonValue;
/**
* Merge 2 JSON objects and present them as a map.
*/
@Immutable
final class MergedJsonObjectMap implements Map<String, JsonValue> {
private final JsonObject jsonObject;
private final JsonObject fallbackObject;
private MergedJsonObjectMap(final JsonObject jsonObject, final JsonObject fallbackObject) {
this.jsonObject = jsonObject;
this.fallbackObject = fallbackObject;
}
static MergedJsonObjectMap of(final JsonObject jsonObject, final JsonObject fallbackObject) {
return new MergedJsonObjectMap(jsonObject, fallbackObject);
}
static MergedJsonObjectMap of(final Map<String, JsonValue> map) {
if (map instanceof MergedJsonObjectMap mergedJsonObjectMap) {
return mergedJsonObjectMap;
} else {
return new MergedJsonObjectMap(map.entrySet().stream()
.map(entry -> JsonField.newInstance(entry.getKey(), entry.getValue()))
.collect(JsonCollectors.fieldsToObject()), JsonObject.empty());
}
}
@Override
public int size() {
return entrySet().size();
}
@Override
public boolean isEmpty() {
return jsonObject.isEmpty() && fallbackObject.isEmpty();
}
@Override
public boolean containsKey(final Object o) {
if (!(o instanceof CharSequence)) {
return false;
}
final CharSequence key = (CharSequence) o;
return jsonObject.contains(key) || fallbackObject.contains(key);
}
@Override
public boolean containsValue(final Object o) {
throw new UnsupportedOperationException("Not supported");
}
@Override
@Nullable
public JsonValue get(final Object o) {
if (!(o instanceof CharSequence)) {
return null;
}
final CharSequence key = (CharSequence) o;
return jsonObject.getValue(key).orElseGet(() -> fallbackObject.getValue(key).orElse(null));
}
@Override
public JsonValue put(final String charSequence, final JsonValue jsonValue) {
throw new UnsupportedOperationException("This object is immutable");
}
@Override
public JsonValue remove(final Object o) {
throw new UnsupportedOperationException("This object is immutable");
}
@Override
public void putAll(final Map<? extends String, ? extends JsonValue> map) {
throw new UnsupportedOperationException("This object is immutable");
}
@Override
public void clear() {
throw new UnsupportedOperationException("This object is immutable");
}
@Override
public Set<String> keySet() {
return Stream.concat(
fallbackObject.getKeys().stream().map(Object::toString),
jsonObject.getKeys().stream().map(Object::toString)
).collect(Collectors.toSet());
}
@Override
public Collection<JsonValue> values() {
return Stream.concat(fallbackObject.stream(), jsonObject.stream())
.map(JsonField::getValue)
.collect(Collectors.toSet());
}
@Override
public Set<Entry<String, JsonValue>> entrySet() {
return Stream.concat(fallbackObject.stream(), jsonObject.stream())
.map(field -> new AbstractMap.SimpleEntry<>(field.getKey().toString(), field.getValue()))
.collect(Collectors.toSet());
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final MergedJsonObjectMap that = (MergedJsonObjectMap) o;
return Objects.equals(jsonObject, that.jsonObject) && Objects.equals(fallbackObject, that.fallbackObject);
}
@Override
public int hashCode() {
return Objects.hash(jsonObject, fallbackObject);
}
@Override
public String toString() {
return getClass().getSimpleName() +
"[jsonObject=" + jsonObject +
",fallbackObject=" + fallbackObject +
"]";
}
}