-
Notifications
You must be signed in to change notification settings - Fork 215
/
SearchModelFactory.java
executable file
·305 lines (276 loc) · 12.2 KB
/
SearchModelFactory.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
/*
* 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.thingsearch.model;
import static java.util.Objects.requireNonNull;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import static org.eclipse.ditto.base.model.exceptions.DittoJsonException.wrapJsonRuntimeException;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
/**
* Factory for creating search results.
* <p>
* This factory provides static methods for creating {@link LogicalSearchFilter}s or {@link SearchProperty search
* properties}. The former are useful to concatenate {@link SearchFilter}s with boolean operators while the latter are
* mandatory for a building a {@link SearchQuery} .
*/
@Immutable
public final class SearchModelFactory {
/*
* Inhibit instantiation of this utility class.
*/
private SearchModelFactory() {
throw new AssertionError();
}
/**
* Creates a new {@link SearchResult}.
*
* @param items the items.
* @param nextPageOffset the offset of the next page or {@link org.eclipse.ditto.thingsearch.model.SearchResult#NO_NEXT_PAGE}.
* @return the new immutable search results object.
* @throws NullPointerException if {@code items} is {@code null}.
*/
public static SearchResult newSearchResult(final JsonArray items, final long nextPageOffset) {
return ImmutableSearchResult.of(items, nextPageOffset, null, null);
}
/**
* Creates a new {@link SearchResult}.
*
* @param items the items.
* @param nextPageOffset the offset of the next page or {@link org.eclipse.ditto.thingsearch.model.SearchResult#NO_NEXT_PAGE}.
* @param lastModified the last modified timestamp.
* @return the new immutable search results object.
* @throws NullPointerException if {@code items} is {@code null}.
* @since 3.0.0
*/
public static SearchResult newSearchResult(final JsonArray items, final long nextPageOffset,
@Nullable final Instant lastModified) {
return ImmutableSearchResult.of(items, nextPageOffset, null, lastModified);
}
/**
* Returns a new immutable empty {@link SearchResult}.
*
* @return the new immutable empty {@code SearchResult}.
*/
public static SearchResult emptySearchResult() {
return ImmutableSearchResult.empty();
}
/**
* Returns a new immutable {@link SearchResult} which is initialised with the values of the given JSON object.
*
* @param jsonObject provides the initial values of the result.
* @return the new immutable initialised {@code SearchResult}.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
*/
public static SearchResult newSearchResult(final JsonObject jsonObject) {
return wrapJsonRuntimeException(() -> ImmutableSearchResult.fromJson(jsonObject));
}
/**
* Returns a new immutable {@link SearchResult} which is initialised with the values of the given JSON string. This
* string is required to be a valid {@link JsonObject}.
*
* @param jsonString provides the initial values of the result;
* @return the new immutable initialised {@code SearchResult}.
* @throws org.eclipse.ditto.base.model.exceptions.DittoJsonException if {@code jsonString} cannot be parsed to {@code
* SearchResult}.
*/
public static SearchResult newSearchResult(final String jsonString) {
final JsonObject jsonObject = wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString));
return newSearchResult(jsonObject);
}
/**
* Returns a new builder with a fluent API for a {@link SearchResult}.
*
* @return the new builder.
*/
public static SearchResultBuilder newSearchResultBuilder() {
return ImmutableSearchResultBuilder.newInstance();
}
/**
* Returns a new builder with a fluent API for a {@link SearchResult} which is initialised with the properties of
* the given SearchResult.
*
* @param searchResult the search result which provides the initial properties of the returned builder.
* @return the new builder.
* @throws NullPointerException if {@code searchResult} is {@code null}.
*/
public static SearchResultBuilder newSearchResultBuilder(final SearchResult searchResult) {
return ImmutableSearchResultBuilder.of(searchResult);
}
/**
* Returns a new {@link LogicalSearchFilter} which concatenates the given filters with the boolean operator
* {@code AND}.
*
* @param filter1 the first filter to be concatenated with at least {@code filter2} and optionally additionally with
* {@code furtherFilters}.
* @param filter2 the second filter to be concatenated with {@code filter1} and {@code furtherFilters}.
* @param furtherFilters optional additional filters to be concatenated.
* @return the new logical {@code AND} search filter.
* @throws NullPointerException if any argument is {@code null}.
*/
public static LogicalSearchFilter and(final SearchFilter filter1, final SearchFilter filter2,
final SearchFilter... furtherFilters) {
return ImmutableLogicalFilter.and(filter1, filter2, furtherFilters);
}
/**
* Returns a new {@link LogicalSearchFilter} which concatenates the given filters with the boolean operator
* {@code OR}.
*
* @param filter1 the first filter to be concatenated with at least {@code filter2} and optionally additionally with
* {@code furtherFilters}.
* @param filter2 the second filter to be concatenated with {@code filter1} and {@code furtherFilters}.
* @param furtherFilters optional additional filters to be concatenated.
* @return the new logical {@code OR} search filter.
* @throws NullPointerException if any argument is {@code null}.
*/
public static LogicalSearchFilter or(final SearchFilter filter1, final SearchFilter filter2,
final SearchFilter... furtherFilters) {
return ImmutableLogicalFilter.or(filter1, filter2, furtherFilters);
}
/**
* Returns a new {@link LogicalSearchFilter} which negates the given filter with the boolean operator {@code NOT}.
*
* @param filter the filter to be negated.
* @return the new logical {@code NOT} filter.
* @throws NullPointerException if {@code filter} is {@code null}.
*/
public static LogicalSearchFilter not(final SearchFilter filter) {
return ImmutableLogicalFilter.not(filter);
}
/**
* Creates a {@link SearchProperty} for the given path. For example to search for a particular Thing attribute this
* method might be used as follows:
* <pre>
* final SearchProperty searchProperty = SearchModelFactory.property("attributes/manufacturer");
* final PropertySearchFilter searchFilter = searchProperty.eq("Bosch");
*
* final SearchQuery query = SearchModelFactory.newSearchQueryBuilder(searchFilter) //
* .limit(0, 25) //
* .build();
* </pre>
*
* @param propertyPath the name or path of the property to be searched for.
* @return the new SearchProperty.
* @throws NullPointerException if {@code propertyPath} is {@code null}.
*/
public static SearchProperty property(final JsonPointer propertyPath) {
return ImmutableSearchProperty.of(propertyPath);
}
/**
* Creates a {@link SearchProperty} for the given path. For example to search for a particular Thing attribute this
* method might be used as follows:
* <pre>
* final SearchProperty searchProperty = SearchModelFactory.property("attributes/manufacturer");
* final PropertySearchFilter searchFilter = searchProperty.eq("Bosch");
*
* final SearchQuery query = SearchModelFactory.newSearchQueryBuilder(searchFilter) //
* .limit(0, 25) //
* .build();
* </pre>
*
* @param propertyPath the name or path of the property to be searched for.
* @return the new SearchProperty.
* @throws NullPointerException if {@code propertyPath} is {@code null}.
*/
public static SearchProperty property(final CharSequence propertyPath) {
return property(JsonFactory.newPointer(propertyPath));
}
/**
* Returns a new immutable {@link SearchQuery} which is based on the given search filter.
*
* @param filter the search filter which is the crucial part of the search query.
* @return the new SearchQuery.
* @throws NullPointerException if {@code filter} is {@code null}.
*/
public static SearchQuery newSearchQuery(final SearchFilter filter) {
return newSearchQueryBuilder(filter).build();
}
/**
* Returns a mutable builder with a fluent API for an immutable {@link SearchQuery}.
*
* @param filter the search filter which is the crucial part of the search query.
* @return the new builder.
* @throws NullPointerException if {@code filter} is {@code null}.
*/
public static SearchQueryBuilder newSearchQueryBuilder(final SearchFilter filter) {
return ImmutableSearchQueryBuilder.of(filter);
}
/**
* Creates a new {@link SortOption} for the provided {@code sortOptionEntries} containing {@code propertyPath}
* and {@code order}.
*
* @param sortOptionEntries the {@link SortOptionEntry}s containing {@code propertyPath} and {@code order}
* @return the created SortOption.
*/
public static SortOption newSortOption(final List<SortOptionEntry> sortOptionEntries) {
return ImmutableSortOption.of(sortOptionEntries);
}
/**
* Creates a new {@link SortOption} with a single entry for the provided {@code order} and {@code propertyPath}.
*
* @param propertyPath the {@code propertyPath} of the SortOptionEntry to create
* @param sortOrder the {@code order} of the SortOptionEntry to create
* @return the created SortOption.
*/
public static SortOption newSortOption(final CharSequence propertyPath, final SortOptionEntry.SortOrder sortOrder) {
requireNonNull(sortOrder);
requireNonNull(propertyPath);
final SortOptionEntry entry = ImmutableSortOptionEntry.of(propertyPath, sortOrder);
return ImmutableSortOption.of(Collections.singletonList(entry));
}
/**
* Creates a new {@link SortOptionEntry} for the provided {@code order} and {@code propertyPath}.
*
* @param propertyPath the {@code propertyPath} of the SortOptionEntry to create
* @param sortOrder the {@code order} of the SortOptionEntry to create
* @return the created SortOption.
*/
public static SortOptionEntry newSortOptionEntry(final CharSequence propertyPath,
final SortOptionEntry.SortOrder sortOrder) {
return ImmutableSortOptionEntry.of(propertyPath, sortOrder);
}
/**
* Creates a new {@link LimitOption} for the provided {@code offset} and {@code count}.
*
* @param offset the offset to use for the LimitOption
* @param count the count to include in the LimitOption
* @return the created LimitOption
*/
public static LimitOption newLimitOption(final int offset, final int count) {
return ImmutableLimitOption.of(offset, count);
}
/**
* Creates a new {@link SizeOption}.
*
* @param size the maximum number of results
* @return the SizeOption
*/
public static SizeOption newSizeOption(final int size) {
return ImmutableSizeOption.of(size);
}
/**
* Creates a new {@link CursorOption}.
*
* @param cursor cursor of the new page
* @return the CursorOption
*/
public static CursorOption newCursorOption(final String cursor) {
return ImmutableCursorOption.of(checkNotNull(cursor, "cursor"));
}
}