/
QueryThings.java
executable file
·346 lines (308 loc) · 13.6 KB
/
QueryThings.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* Copyright (c) 2017 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.signals.commands.thingsearch.query;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonCollectors;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.json.FieldType;
import org.eclipse.ditto.model.base.json.JsonParsableCommand;
import org.eclipse.ditto.model.base.json.JsonSchemaVersion;
import org.eclipse.ditto.signals.commands.base.AbstractCommand;
import org.eclipse.ditto.signals.commands.base.CommandJsonDeserializer;
/**
* Command for searching things.
*/
@Immutable
@JsonParsableCommand(typePrefix = QueryThings.TYPE_PREFIX, name = QueryThings.NAME)
public final class QueryThings extends AbstractCommand<QueryThings> implements ThingSearchQueryCommand<QueryThings> {
/**
* Name of the command.
*/
public static final String NAME = "queryThings";
/**
* Type of this command.
*/
public static final String TYPE = TYPE_PREFIX + NAME;
static final JsonFieldDefinition<String> JSON_FILTER =
JsonFactory.newStringFieldDefinition("filter", FieldType.REGULAR, JsonSchemaVersion.V_1,
JsonSchemaVersion.V_2);
static final JsonFieldDefinition<JsonArray> JSON_OPTIONS =
JsonFactory.newJsonArrayFieldDefinition("options", FieldType.REGULAR, JsonSchemaVersion.V_1,
JsonSchemaVersion.V_2);
static final JsonFieldDefinition<String> JSON_FIELDS =
JsonFactory.newStringFieldDefinition("fields", FieldType.REGULAR, JsonSchemaVersion.V_1,
JsonSchemaVersion.V_2);
static final JsonFieldDefinition<JsonArray> JSON_NAMESPACES =
JsonFactory.newJsonArrayFieldDefinition("namespaces", FieldType.REGULAR, JsonSchemaVersion.V_1,
JsonSchemaVersion.V_2);
@Nullable private final String filter;
@Nullable private final List<String> options;
@Nullable private final JsonFieldSelector fields;
@Nullable private final Set<String> namespaces;
private QueryThings(final DittoHeaders dittoHeaders, @Nullable final String filter,
@Nullable final List<String> options, @Nullable final JsonFieldSelector fields,
@Nullable final Collection<String> namespaces) {
super(TYPE, dittoHeaders);
this.filter = filter;
if (options != null) {
this.options = Collections.unmodifiableList(options);
} else {
this.options = null;
}
this.fields = fields;
if (namespaces != null) {
this.namespaces = Collections.unmodifiableSet(new HashSet<>(namespaces));
} else {
this.namespaces = null;
}
}
/**
* Returns a new instance of {@code QueryThings}.
*
* @param filter the optional query filter string
* @param options the optional query options
* @param fields the optional fields
* @param dittoHeaders the headers of the command.
* @return a new command for searching Things.
* @throws NullPointerException if any argument is {@code null}.
*/
public static QueryThings of(@Nullable final String filter, @Nullable final List<String> options,
@Nullable final JsonFieldSelector fields, @Nullable final Set<String> namespaces,
final DittoHeaders dittoHeaders) {
return new QueryThings(dittoHeaders, filter, options, fields, namespaces);
}
/**
* Returns a new instance of {@code QueryThings}.
*
* @param filter the optional query filter string
* @param options the query options
* @param dittoHeaders the headers of the command.
* @return a new command for searching Things.
* @throws NullPointerException if any argument is {@code null}.
*/
public static QueryThings of(final String filter, final List<String> options, final Set<String> namespaces,
final DittoHeaders dittoHeaders) {
return new QueryThings(dittoHeaders, filter, options, null, namespaces);
}
/**
* Returns a new instance of {@code QueryThings}.
*
* @param filter the optional query filter string
* @param fields the optional fields
* @param dittoHeaders the headers of the command.
* @return a new command for searching Things.
* @throws NullPointerException if any argument is {@code null}.
*/
public static QueryThings of(final String filter, final JsonFieldSelector fields, final Set<String> namespaces,
final DittoHeaders dittoHeaders) {
return new QueryThings(dittoHeaders, filter, null, fields, namespaces);
}
/**
* Returns a new instance of {@code QueryThings}.
*
* @param filter the optional query filter string
* @param dittoHeaders the headers of the command.
* @return a new command for searching Things.
* @throws NullPointerException if any argument is {@code null}.
*/
public static QueryThings of(final String filter, final DittoHeaders dittoHeaders) {
return new QueryThings(dittoHeaders, filter, null, null, null);
}
/**
* Returns a new instance of {@code QueryThings}.
*
* @param options the query options
* @param fields the optional fields
* @param dittoHeaders the headers of the command.
* @return a new command for searching Things.
* @throws NullPointerException if any argument is {@code null}.
*/
public static QueryThings of(final List<String> options, final JsonFieldSelector fields,
final Set<String> namespaces, final DittoHeaders dittoHeaders) {
return new QueryThings(dittoHeaders, null, options, fields, namespaces);
}
/**
* Returns a new instance of {@code QueryThings}.
*
* @param fields the optional fields
* @param dittoHeaders the headers of the command.
* @return a new command for searching Things.
* @throws NullPointerException if any argument is {@code null}.
*/
public static QueryThings of(final JsonFieldSelector fields, final DittoHeaders dittoHeaders) {
return new QueryThings(dittoHeaders, null, null, fields, null);
}
/**
* Returns a new instance of {@code QueryThings}.
*
* @param options the query options
* @param dittoHeaders the headers of the command.
* @return a new command for searching Things.
* @throws NullPointerException if any argument is {@code null}.
*/
public static QueryThings of(final List<String> options, final DittoHeaders dittoHeaders) {
return new QueryThings(dittoHeaders, null, options, null, null);
}
/**
* Returns a new instance of {@code QueryThings}.
*
* @param namespaces the namespaces for the query
* @param dittoHeaders the headers of the command.
* @return a new command for searching Things.
* @throws NullPointerException if any argument is {@code null}.
*/
public static QueryThings of(final Set<String> namespaces, final DittoHeaders dittoHeaders) {
return new QueryThings(dittoHeaders, null, null, null, namespaces);
}
/**
* Returns a new instance of {@code QueryThings}.
*
* @param dittoHeaders the headers of the command.
* @return a new command for searching Things.
* @throws NullPointerException if any argument is {@code null}.
*/
public static QueryThings of(final DittoHeaders dittoHeaders) {
return new QueryThings(dittoHeaders, null, null, null, null);
}
/**
* Creates a new {@code QueryThings} from a JSON string.
*
* @param jsonString the JSON string of which the command is to be created.
* @param dittoHeaders the headers of the command.
* @return the command.
* @throws NullPointerException if {@code jsonString} is {@code null}.
* @throws IllegalArgumentException if {@code jsonString} is empty.
* @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonString} was not in the expected
* format.
*/
public static QueryThings fromJson(final String jsonString, final DittoHeaders dittoHeaders) {
return fromJson(JsonFactory.newObject(jsonString), dittoHeaders);
}
/**
* Creates a new {@code QueryThings} from a JSON object.
*
* @param jsonObject the JSON object of which the command is to be created.
* @param dittoHeaders the headers of the command.
* @return the command.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
* @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected
* format.
*/
public static QueryThings fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) {
return new CommandJsonDeserializer<QueryThings>(TYPE, jsonObject).deserialize(() -> {
final String extractedFilter = jsonObject.getValue(JSON_FILTER).orElse(null);
final List<String> extractedOptions = jsonObject.getValue(JSON_OPTIONS)
.map(jsonArray -> jsonArray.stream()
.filter(JsonValue::isString)
.map(JsonValue::asString)
.collect(Collectors.toList()))
.orElse(null);
final JsonFieldSelector extractedFieldSelector = jsonObject.getValue(JSON_FIELDS)
.map(fields -> JsonFactory.newFieldSelector(fields, JsonFactory.newParseOptionsBuilder().build()))
.orElse(null);
final Set<String> extractedNamespaces = jsonObject.getValue(JSON_NAMESPACES)
.map(jsonValues -> jsonValues.stream()
.filter(JsonValue::isString)
.map(JsonValue::asString)
.collect(Collectors.toSet()))
.orElse(null);
return new QueryThings(dittoHeaders, extractedFilter, extractedOptions, extractedFieldSelector, extractedNamespaces);
});
}
@Override
public Optional<String> getFilter() {
return Optional.ofNullable(filter);
}
/**
* Get the optional options.
*
* @return the optional options.
*/
public Optional<List<String>> getOptions() {
return Optional.ofNullable(options);
}
/**
* Get the optional field selector.
*
* @return the optional field selector.
*/
public Optional<JsonFieldSelector> getFields() {
return Optional.ofNullable(fields);
}
@Override
public Optional<Set<String>> getNamespaces() {
return Optional.ofNullable(namespaces);
}
@Override
public QueryThings setNamespaces(@Nullable final Collection<String> namespaces) {
return new QueryThings(getDittoHeaders(), filter, options, fields, namespaces);
}
@Override
protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final JsonSchemaVersion schemaVersion,
final Predicate<JsonField> thePredicate) {
final Predicate<JsonField> predicate = schemaVersion.and(thePredicate);
getFilter().ifPresent(presentFilter -> jsonObjectBuilder.set(JSON_FILTER, presentFilter, predicate));
getOptions().ifPresent(presentOptions -> jsonObjectBuilder.set(JSON_OPTIONS, presentOptions.stream()
.map(JsonValue::of)
.collect(JsonCollectors.valuesToArray()), predicate));
getFields().ifPresent(presentFields -> jsonObjectBuilder.set(JSON_FIELDS, presentFields.toString(), predicate));
getNamespaces().ifPresent(presentOptions -> jsonObjectBuilder.set(JSON_NAMESPACES, presentOptions.stream()
.map(JsonValue::of)
.collect(JsonCollectors.valuesToArray()), predicate));
}
@Override
public QueryThings setDittoHeaders(final DittoHeaders dittoHeaders) {
return of(filter, options, fields, namespaces, dittoHeaders);
}
@Override
public boolean equals(@Nullable final Object o) {
if (this == o)
return true;
if (!(o instanceof QueryThings))
return false;
if (!super.equals(o))
return false;
final QueryThings that = (QueryThings) o;
return Objects.equals(filter, that.filter) &&
Objects.equals(options, that.options) &&
Objects.equals(fields, that.fields) &&
Objects.equals(namespaces, that.namespaces);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), filter, options, fields, namespaces);
}
@Override
public String toString() {
return getClass().getSimpleName() + "[" + "filter='" + filter + "', options=" + options + ", fields=" + fields
+ ", namespaces=" + namespaces + ']';
}
}