/
ThingsModelFactory.java
executable file
·843 lines (763 loc) · 35.7 KB
/
ThingsModelFactory.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
/*
* 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.things.model;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
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.base.model.entity.metadata.Metadata;
import org.eclipse.ditto.base.model.entity.metadata.MetadataBuilder;
import org.eclipse.ditto.base.model.entity.metadata.MetadataModelFactory;
import org.eclipse.ditto.base.model.exceptions.DittoJsonException;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonKey;
import org.eclipse.ditto.json.JsonKeyInvalidException;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonParseException;
import org.eclipse.ditto.json.JsonPointer;
/**
* Factory that creates new {@code things} objects.
*/
@Immutable
public final class ThingsModelFactory {
public static final JsonKey FEATURE_ID_WILDCARD = JsonKey.of("*");
/*
* Inhibit instantiation of this utility class.
*/
private ThingsModelFactory() {
throw new AssertionError();
}
/**
* Returns a new immutable empty {@link Attributes}.
*
* @return the new immutable empty {@code Attributes}.
*/
public static Attributes emptyAttributes() {
return AttributesModelFactory.emptyAttributes();
}
/**
* Returns a new immutable {@link Attributes} which represents {@code null}.
*
* @return the new {@code null}-like {@code Attributes}.
*/
public static Attributes nullAttributes() {
return AttributesModelFactory.nullAttributes();
}
/**
* Returns a new immutable {@link Attributes} 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 Attributes}.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
*/
public static Attributes newAttributes(final JsonObject jsonObject) {
return AttributesModelFactory.newAttributes(jsonObject);
}
/**
* Returns a new immutable {@link Attributes} 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 Attributes}.
* @throws DittoJsonException if {@code jsonString} cannot be parsed to {@code Attributes}.
*/
public static Attributes newAttributes(final String jsonString) {
return AttributesModelFactory.newAttributes(jsonString);
}
/**
* Returns a new empty builder for a {@link Attributes}.
*
* @return the builder.
*/
public static AttributesBuilder newAttributesBuilder() {
return AttributesModelFactory.newAttributesBuilder();
}
/**
* Returns a new builder for a {@link Attributes} which is initialised with the values of the given JSON object.
*
* @param jsonObject provides the initial values of the result.
* @return the builder.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
*/
public static AttributesBuilder newAttributesBuilder(final JsonObject jsonObject) {
return AttributesModelFactory.newAttributesBuilder(jsonObject);
}
/**
* Returns a new immutable {@link ThingDefinition} which is initialised with the parsed {@code thingDefinition}.
*
* @param thingDefinition Definition identifier which should be parsed as ThingDefinition.
* @return new ThingDefinition with the parsed definition identifier.
*/
public static ThingDefinition newDefinition(@Nullable final CharSequence thingDefinition) {
if (null != thingDefinition) {
return ImmutableThingDefinition.ofParsed(thingDefinition);
}
return nullDefinition();
}
/**
* Returns a new immutable {@link ThingDefinition} which represents {@code null}.
*
* @return the new {@code null}-like {@code ThingDefinition}.
*/
public static ThingDefinition nullDefinition() {
return NullThingDefinition.getInstance();
}
/**
* Returns an immutable instance of {@link DefinitionIdentifier}.
*
* @param namespace the namespace of the returned Identifier.
* @param name the name of the returned Identifier.
* @param version the version of the returned Identifier.
* @return the instance.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if any argument is empty.
*/
public static DefinitionIdentifier newFeatureDefinitionIdentifier(final CharSequence namespace,
final CharSequence name, final CharSequence version) {
return ImmutableFeatureDefinitionIdentifier.getInstance(namespace, name, version);
}
/**
* Parses the specified CharSequence and returns an immutable instance of {@link DefinitionIdentifier}.
*
* @param featureIdentifierAsCharSequence CharSequence-representation of a FeatureDefinition Identifier.
* @return the instance.
* @throws NullPointerException if {@code featureIdentifierAsCharSequence} is {@code null}.
* @throws DefinitionIdentifierInvalidException if {@code featureIdentifierAsCharSequence} is invalid.
*/
public static DefinitionIdentifier newFeatureDefinitionIdentifier(
final CharSequence featureIdentifierAsCharSequence) {
if (featureIdentifierAsCharSequence instanceof DefinitionIdentifier) {
return (DefinitionIdentifier) featureIdentifierAsCharSequence;
}
return ImmutableFeatureDefinitionIdentifier.ofParsed(featureIdentifierAsCharSequence);
}
/**
* Creates a new instance of {@code ImmutableFeatureDefinition} based on the passed {@code definitionIdentifiers}.
*
* @param definitionIdentifiers the Identifiers of the FeatureDefinition to be returned.
* @return the instance.
* @throws NullPointerException if {@code definitionIdentifiers} is {@code null}.
* @since 3.0.0
*/
public static FeatureDefinition newFeatureDefinition(final Collection<DefinitionIdentifier> definitionIdentifiers) {
return ImmutableFeatureDefinition.of(definitionIdentifiers);
}
/**
* Parses the specified JsonArray and returns an immutable instance of {@code FeatureDefinition} which is
* initialised with the values of the given JSON array.
*
* @param jsonArray JSON array containing the identifiers of the FeatureDefinition to be returned. Non-string values
* are ignored.
* @return the instance.
* @throws NullPointerException if {@code jsonArray} is {@code null}.
* @throws FeatureDefinitionEmptyException if {@code jsonArray} is empty.
* @throws DefinitionIdentifierInvalidException if any Identifier string of the array is invalid.
*/
public static FeatureDefinition newFeatureDefinition(final JsonArray jsonArray) {
checkNotNull(jsonArray, "JSON array");
if (!jsonArray.isNull()) {
return ImmutableFeatureDefinition.fromJson(jsonArray);
}
return nullFeatureDefinition();
}
/**
* Returns a new immutable {@link FeatureDefinition} which is initialised with the values of the given JSON string.
* This string is required to be a valid {@link JsonArray}.
*
* @param jsonString provides the initial values of the result;
* @return the new immutable initialised {@code FeatureDefinition}.
* @throws DittoJsonException if {@code jsonString} cannot be parsed to {@code FeatureDefinition}.
* @throws FeatureDefinitionEmptyException if the JSON array is empty.
* @throws DefinitionIdentifierInvalidException if any Identifier of the JSON array is invalid.
*/
public static FeatureDefinition newFeatureDefinition(final String jsonString) {
final JsonArray jsonArray =
DittoJsonException.wrapJsonRuntimeException(() -> JsonFactory.newArray(jsonString));
return newFeatureDefinition(jsonArray);
}
/**
* Returns a new immutable {@link FeatureDefinition} which represents {@code null}.
*
* @return the new {@code null}-like {@code FeatureDefinition}.
*/
public static FeatureDefinition nullFeatureDefinition() {
return NullFeatureDefinition.getInstance();
}
/**
* Parses the specified CharSequence and returns a mutable builder with a fluent API for an immutable {@code
* FeatureDefinition}. The returned builder is initialised with the parsed Identifier as its first one.
*
* @param firstIdentifier CharSequence-representation of the first FeatureDefinition Identifier.
* @return the instance.
* @throws NullPointerException if {@code firstIdentifier} is {@code null}.
* @throws DefinitionIdentifierInvalidException if {@code firstIdentifier} is invalid.
*/
public static FeatureDefinitionBuilder newFeatureDefinitionBuilder(final CharSequence firstIdentifier) {
return ImmutableFeatureDefinition.getBuilder(newFeatureDefinitionIdentifier(firstIdentifier));
}
/**
* Returns a new builder for an immutable {@link FeatureDefinition} which is initialised with the values of the
* given JSON array.
*
* @param jsonArray provides the initial values of the result.
* @return the builder.
* @throws NullPointerException if {@code jsonArray} is {@code null}.
* @throws DefinitionIdentifierInvalidException if any Identifier of the array is invalid.
*/
public static FeatureDefinitionBuilder newFeatureDefinitionBuilder(final JsonArray jsonArray) {
return ImmutableFeatureDefinition.Builder.getInstance().addAll(newFeatureDefinition(jsonArray));
}
/**
* Returns a new immutable empty {@link FeatureProperties}.
*
* @return the new immutable empty {@code FeatureProperties}.
*/
public static FeatureProperties emptyFeatureProperties() {
return ImmutableFeatureProperties.empty();
}
/**
* Returns a new immutable {@link FeatureProperties} which represents {@code null}.
*
* @return the new {@code null}-like {@code FeatureProperties}.
*/
public static FeatureProperties nullFeatureProperties() {
return NullFeatureProperties.newInstance();
}
/**
* Returns a new immutable {@link FeatureProperties} 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 FeatureProperties}.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
* @throws org.eclipse.ditto.json.JsonKeyInvalidException if a property name in the passed {@code jsonObject}
* was not valid according to pattern
* {@link org.eclipse.ditto.base.model.entity.id.RegexPatterns#NO_CONTROL_CHARS_NO_SLASHES_PATTERN}.
*/
public static FeatureProperties newFeatureProperties(final JsonObject jsonObject) {
checkNotNull(jsonObject, "JSON object for initialization");
if (!jsonObject.isNull()) {
return ImmutableFeatureProperties.of(jsonObject);
} else {
return nullFeatureProperties();
}
}
/**
* Returns a new immutable {@link FeatureProperties} 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 FeatureProperties}.
* @throws DittoJsonException if {@code jsonString} cannot be parsed to {@code FeatureProperties}.
*/
public static FeatureProperties newFeatureProperties(final String jsonString) {
final JsonObject jsonObject =
DittoJsonException.wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString));
return newFeatureProperties(jsonObject);
}
/**
* Returns a new empty builder for an immutable {@link FeatureProperties}.
*
* @return the builder.
*/
public static FeaturePropertiesBuilder newFeaturePropertiesBuilder() {
return ImmutableFeaturePropertiesBuilder.empty();
}
/**
* Returns a new builder for an immutable {@link FeatureProperties} which is initialised with the values of the
* given JSON object.
*
* @param jsonObject provides the initial values of the result.
* @return the builder.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
*/
public static FeaturePropertiesBuilder newFeaturePropertiesBuilder(final JsonObject jsonObject) {
return ImmutableFeaturePropertiesBuilder.of(jsonObject);
}
/**
* Returns a new immutable {@link Feature} which represents {@code null}.
*
* @param featureId the ID of the new Feature.
* @return the new {@code null}-like {@code Feature}.
* @throws NullPointerException if {@code featureId} is {@code null}.
* @throws org.eclipse.ditto.json.JsonKeyInvalidException if {@code featureId} was not valid according to pattern
* {@link org.eclipse.ditto.base.model.entity.id.RegexPatterns#NO_CONTROL_CHARS_NO_SLASHES_PATTERN}.
*/
public static Feature nullFeature(final String featureId) {
return NullFeature.of(featureId);
}
/**
* Returns a new immutable {@link Feature} with the given ID.
*
* @param featureId the ID of the new Feature.
* @return the new immutable {@code Feature}.
* @throws NullPointerException if {@code featureId} is {@code null}.
* @throws org.eclipse.ditto.json.JsonKeyInvalidException if {@code featureId} was not valid according to pattern
* {@link org.eclipse.ditto.base.model.entity.id.RegexPatterns#NO_CONTROL_CHARS_NO_SLASHES_PATTERN}.
*/
public static Feature newFeature(final String featureId) {
return ImmutableFeature.of(featureId);
}
/**
* Returns a new immutable {@link Feature} with the given ID and properties.
*
* @param featureId the ID of the new feature.
* @param featureProperties the properties of the new Feature or {@code null}.
* @return the new immutable {@code Feature}.
* @throws NullPointerException if {@code featureId} is {@code null}.
* @throws org.eclipse.ditto.json.JsonKeyInvalidException if {@code featureId} was not valid according to pattern
* {@link org.eclipse.ditto.base.model.entity.id.RegexPatterns#NO_CONTROL_CHARS_NO_SLASHES_PATTERN}.
*/
public static Feature newFeature(final String featureId, @Nullable final FeatureProperties featureProperties) {
return ImmutableFeature.of(featureId, featureProperties);
}
/**
* Returns a new immutable {@link Feature} with the given ID, properties and Definition.
*
* @param featureId the ID of the new feature.
* @param featureDefinition the Definition of the new Feature or {@code null}.
* @return the new immutable {@code Feature}.
* @throws NullPointerException if {@code featureId} is {@code null}.
* @throws org.eclipse.ditto.json.JsonKeyInvalidException if {@code featureId} was not valid according to pattern
* {@link org.eclipse.ditto.base.model.entity.id.RegexPatterns#NO_CONTROL_CHARS_NO_SLASHES_PATTERN}.
*/
public static Feature newFeature(final String featureId, @Nullable final FeatureDefinition featureDefinition) {
return ImmutableFeature.of(featureId, featureDefinition, null);
}
/**
* Returns a new immutable {@link Feature} with the given ID, properties and Definition.
*
* @param featureId the ID of the new feature.
* @param featureDefinition the Definition of the new Feature or {@code null}.
* @param featureProperties the properties of the new Feature or {@code null}.
* @return the new immutable {@code Feature}.
* @throws NullPointerException if {@code featureId} is {@code null}.
* @throws org.eclipse.ditto.json.JsonKeyInvalidException if {@code featureId} was not valid according to pattern
* {@link org.eclipse.ditto.base.model.entity.id.RegexPatterns#NO_CONTROL_CHARS_NO_SLASHES_PATTERN}.
*/
public static Feature newFeature(final String featureId, @Nullable final FeatureDefinition featureDefinition,
@Nullable final FeatureProperties featureProperties) {
return ImmutableFeature.of(featureId, featureDefinition, featureProperties);
}
/**
* Returns a new immutable {@link Feature} with the given ID, properties, desired Properties and Definition.
*
* @param featureId the ID of the new feature.
* @param featureDefinition the Definition of the new Feature or {@code null}.
* @param featureProperties the properties of the new Feature or {@code null}.
* @param desiredFeatureProperties the desired properties of the new Feature or {@code null}.
* @return the new immutable {@code Feature}.
* @throws NullPointerException if {@code featureId} is {@code null}.
* @throws org.eclipse.ditto.json.JsonKeyInvalidException if {@code featureId} was not valid according to pattern
* {@link org.eclipse.ditto.base.model.entity.id.RegexPatterns#NO_CONTROL_CHARS_NO_SLASHES_PATTERN}.
* @since 1.5.0
*/
public static Feature newFeature(final CharSequence featureId,
@Nullable final FeatureDefinition featureDefinition,
@Nullable final FeatureProperties featureProperties,
@Nullable FeatureProperties desiredFeatureProperties) {
return ImmutableFeature.of(featureId, featureDefinition, featureProperties, desiredFeatureProperties);
}
/**
* Returns a new builder for an immutable {@link Feature} from scratch with a fluent API.
*
* @return the builder.
*/
public static FeatureBuilder.FromScratchBuildable newFeatureBuilder() {
return ImmutableFeatureFromScratchBuilder.newFeatureFromScratch();
}
/**
* Returns a new builder for an immutable {@link Feature} which is initialised with the values of the given Feature.
*
* @param feature provides the initial values for the result.
* @return the builder.
* @throws NullPointerException if {@code feature} is {@code null}.
*/
public static FeatureBuilder.FromCopyBuildable newFeatureBuilder(final Feature feature) {
return ImmutableFeatureFromCopyBuilder.of(feature);
}
/**
* Returns a new builder for an immutable {@link Feature} which is initialised with the values of the given JSON
* object.
*
* @param jsonObject provides the initial values for the result.
* @return the builder.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
*/
public static FeatureBuilder.FromJsonBuildable newFeatureBuilder(final JsonObject jsonObject) {
return ImmutableFeatureFromScratchBuilder.newFeatureFromJson(jsonObject);
}
/**
* Returns a new builder for an immutable {@link Feature} which is initialised with the values of the given JSON
* string. The JSON string is parsed in a fault tolerant way. I. e. all properties which cannot be deserialized are
* supposed to not exist.
*
* @param jsonString string the JSON string representation of a Feature.
* @return the builder.
* @throws DittoJsonException if {@code jsonString} cannot be parsed to {@code Feature}.
* @see #newFeatureBuilder(JsonObject)
*/
public static FeatureBuilder.FromJsonBuildable newFeatureBuilder(final String jsonString) {
final JsonObject jsonObject =
DittoJsonException.wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString));
return newFeatureBuilder(jsonObject);
}
/**
* Returns a new immutable empty {@link Features}.
*
* @return the new immutable empty {@code Features}.
*/
public static Features emptyFeatures() {
return ImmutableFeatures.empty();
}
/**
* Returns a new immutable {@link Features} which represents {@code null}.
*
* @return the new {@code null}-like {@code Features}.
*/
public static Features nullFeatures() {
return NullFeatures.newInstance();
}
/**
* Returns a new immutable {@link Features} which is initialised with the features of the given Iterable.
*
* @param features the features to initialise the result with.
* @return the new immutable {@code Features} which is initialised with {@code features}.
* @throws NullPointerException if {@code features} is {@code null}.
*/
public static Features newFeatures(final Iterable<Feature> features) {
return ImmutableFeatures.of(features);
}
/**
* Returns a new immutable {@link Features} based on the given JSON object.
*
* @param jsonObject provides the initial values for the result.
* @return the new immutable {@code Features} which is initialised by the data of {@code jsonObject}.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
* @throws DittoJsonException if any JSON field which is supposed to represent a Feature is not a JSON object.
*/
public static Features newFeatures(final JsonObject jsonObject) {
checkNotNull(jsonObject, "Features JSON object");
final Features result;
if (!jsonObject.isNull()) {
final Set<Feature> features = jsonObject.stream()
.filter(field -> !Objects.equals(field.getKey(), JsonSchemaVersion.getJsonKey()))
.peek(field -> {
if (!(field.getValue().isObject())) {
final String errorMsgTemplate =
"The Feature value is not an object for Feature with ID ''{0}'': {1}";
final String errorMsg =
MessageFormat.format(errorMsgTemplate, field.getKey(), field.getValue());
throw new DittoJsonException(new JsonParseException(errorMsg));
}
})
.map(field -> ImmutableFeatureFromScratchBuilder.newFeatureFromJson(field.getValue().asObject())
.useId(field.getKeyName())
.build())
.collect(Collectors.toCollection(LinkedHashSet::new));
result = ImmutableFeatures.of(features);
} else {
result = nullFeatures();
}
return result;
}
/**
* Returns a new immutable {@link Features} based on the given JSON string.
*
* @param jsonString provides the initial values of the result.
* @return the new immutable initialised {@code Features}.
* @throws DittoJsonException if {@code jsonString} cannot be parsed to {@code Features}.
*/
public static Features newFeatures(final String jsonString) {
final JsonObject featuresJsonObject =
DittoJsonException.wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString));
return newFeatures(featuresJsonObject);
}
/**
* Returns a new immutable {@link Features} which is initialised with the given features.
*
* @param feature the initial Feature of the result.
* @param additionalFeatures additional features of the result.
* @return the new immutable {@code Features} which is initialised with {@code feature} and potentially with {@code
* additionalFeatures}.
* @throws NullPointerException if any argument is {@code null}.
*/
public static Features newFeatures(final Feature feature, final Feature... additionalFeatures) {
return ImmutableFeatures.of(feature, additionalFeatures);
}
/**
* Returns a new mutable builder with a fluent API for an immutable {@link Features}.
*
* @return the builder.
*/
public static FeaturesBuilder newFeaturesBuilder() {
return ImmutableFeaturesBuilder.newInstance();
}
/**
* Returns a new mutable builder with a fluent API for an immutable {@link Features}. The builder is initialised
* with the given features.
*
* @param features the initial features of the new builder.
* @return the builder.
* @throws NullPointerException if {@code features} is {@code null}.
*/
public static FeaturesBuilder newFeaturesBuilder(final Iterable<Feature> features) {
final FeaturesBuilder result = ImmutableFeaturesBuilder.newInstance();
result.setAll(features);
return result;
}
/**
* Validates the given {@link JsonPointer} to a feature property.
*
* @param jsonPointer {@code jsonPointer} that is validated
* @return the same {@code jsonPointer} if validation was successful
* @throws JsonKeyInvalidException if {@code jsonPointer} was not valid according to
* pattern {@link org.eclipse.ditto.base.model.entity.id.RegexPatterns#NO_CONTROL_CHARS_NO_SLASHES_PATTERN}.
* @since 1.2.0
*/
public static JsonPointer validateFeaturePropertyPointer(final JsonPointer jsonPointer) {
return JsonKeyValidator.validate(jsonPointer);
}
/**
* Validates the given {@link JsonObject} containing only valid keys.
*
* @param jsonObject {@code jsonObject} that is validated
* @throws org.eclipse.ditto.json.JsonKeyInvalidException if {@code jsonObject} was not valid according to
* pattern {@link org.eclipse.ditto.base.model.entity.id.RegexPatterns#NO_CONTROL_CHARS_NO_SLASHES_PATTERN}.
* @since 1.3.0
*/
public static void validateJsonKeys(final JsonObject jsonObject) {
JsonKeyValidator.validateJsonKeys(jsonObject);
}
/**
* Returns a new immutable empty {@link Metadata}.
*
* @return the new immutable empty {@code Metadata}.
* @since 1.2.0
*/
public static Metadata emptyMetadata() {
return MetadataModelFactory.emptyMetadata();
}
/**
* Returns a new immutable {@link Metadata} which represents {@code null}.
*
* @return the new {@code null}-like {@code Metadata}.
* @since 1.2.0
*/
public static Metadata nullMetadata() {
return MetadataModelFactory.nullMetadata();
}
/**
* Returns a new immutable {@link Metadata} 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 Metadata}.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
* @since 1.2.0
*/
public static Metadata newMetadata(final JsonObject jsonObject) {
return MetadataModelFactory.newMetadata(jsonObject);
}
/**
* Returns a new immutable {@link Metadata} 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 Metadata}.
* @throws DittoJsonException if {@code jsonString} cannot be parsed to {@code Metadata}.
* @since 1.2.0
*/
public static Metadata newMetadata(final String jsonString) {
return MetadataModelFactory.newMetadata(jsonString);
}
/**
* Returns a new empty builder for a {@link Metadata}.
*
* @return the builder.
* @since 1.2.0
*/
public static MetadataBuilder newMetadataBuilder() {
return MetadataModelFactory.newMetadataBuilder();
}
/**
* Returns a new builder for a {@link Metadata} which is initialised with the values of the given Metadata.
*
* @param metadata provides the initial values of the result.
* @return the builder.
* @throws NullPointerException if {@code metadata} is {@code null}.
* @since 1.2.0
*/
public static MetadataBuilder newMetadataBuilder(final Metadata metadata) {
return MetadataModelFactory.newMetadataBuilder(metadata);
}
/**
* Returns a new immutable {@link ThingRevision} which is initialised with the given revision number.
*
* @param revisionNumber the {@code long} value of the revision.
* @return the new immutable {@code ThingRevision}.
*/
public static ThingRevision newThingRevision(final long revisionNumber) {
return ImmutableThingRevision.of(revisionNumber);
}
/**
* Returns a new immutable {@link Thing} based on the given JSON object.
*
* @param jsonObject the JSON object representation of a Thing.
* @return the new Thing.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
* @throws DittoJsonException if {@code jsonObject} cannot be parsed to {@code Thing}.
*/
public static Thing newThing(final JsonObject jsonObject) {
return newThingBuilder(jsonObject).build();
}
/**
* Returns a new immutable {@link Thing} based on the given JSON string.
*
* @param jsonString the JSON string representation of a Thing.
* @return the new Thing.
* @throws NullPointerException if {@code jsonString} is {@code null}.
* @throws DittoJsonException if {@code jsonString} cannot be parsed to {@code Thing}.
*/
public static Thing newThing(final String jsonString) {
final JsonObject jsonObject =
DittoJsonException.wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString));
return newThingBuilder(jsonObject).build();
}
/**
* Returns a mutable builder with a fluent API for an immutable {@link Thing} from scratch.
*
* @return the new builder.
*/
public static ThingBuilder.FromScratch newThingBuilder() {
return ImmutableThingFromScratchBuilder.newInstance();
}
/**
* Returns a mutable builder with a fluent API for an immutable {@link Thing} based on the given JSON object. The
* JSON object is parsed in a fault-tolerant way. I.e. all properties which cannot be deserialized are supposed to
* not exist.
*
* @param jsonObject the JSON object representation of a Thing.
* @return the new builder.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
* @throws DittoJsonException if {@code jsonObject} cannot be parsed to {@code Thing}.
*/
public static ThingBuilder.FromCopy newThingBuilder(final JsonObject jsonObject) {
return DittoJsonException.wrapJsonRuntimeException(() -> ImmutableThingFromCopyBuilder.of(jsonObject));
}
/**
* Returns a mutable builder with a fluent API for an immutable {@link Thing} based on the given JSON string. The
* JSON string is parsed in a fault-tolerant way. I.e. all properties which cannot be deserialized are supposed to
* not exist.
*
* @param jsonString string the JSON string representation of a Thing.
* @return the new builder.
* @throws DittoJsonException if {@code jsonString} cannot be parsed to {@code Thing}.
* @see #newThingBuilder(JsonObject)
*/
public static ThingBuilder.FromCopy newThingBuilder(final String jsonString) {
final JsonObject jsonObject =
DittoJsonException.wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString));
return newThingBuilder(jsonObject);
}
/**
* Returns a mutable builder with a fluent API for an immutable {@link Thing}. The builder is initialised with the
* properties of the given Thing.
*
* @param thing the Thing which provides the initial properties of the builder.
* @return the new builder.
* @throws NullPointerException if {@code thing} is {@code null}.
*/
public static ThingBuilder.FromCopy newThingBuilder(final Thing thing) {
return ImmutableThingFromCopyBuilder.of(thing);
}
/**
* Returns a new instance of {@link JsonFieldSelector} with expanded feature id wildcard.
*
* @return the new instance.
* @since 2.3.0
*/
public static JsonFieldSelector expandFeatureIdWildcards(final Features features,
final JsonFieldSelector jsonFieldSelector) {
final Collection<JsonKey> featureIds =
features.stream().map(Feature::getId).map(JsonKey::of).collect(Collectors.toList());
return expandFeatureIdWildcards(featureIds, jsonFieldSelector);
}
/**
* Returns a new instance of {@link JsonFieldSelector} with expanded feature id wildcard.
*
* @return the new instance.
* @since 2.3.0
*/
public static JsonFieldSelector expandFeatureIdWildcards(final Collection<JsonKey> featureIds,
final JsonFieldSelector jsonFieldSelector) {
final List<JsonPointer> jsonPointerList = jsonFieldSelector.getPointers().stream()
.flatMap(jsonPointer -> expandFeatureIdWildcard(featureIds, jsonPointer)).collect(Collectors.toList());
return JsonFactory.newFieldSelector(jsonPointerList);
}
/**
* Returns a stream of {@link JsonPointer} with expanded feature id wildcard.
*
* @return a stream of {@link JsonPointer}.
* @since 2.3.0
*/
public static Stream<JsonPointer> expandFeatureIdWildcard(final Collection<JsonKey> featureIds,
final JsonPointer jsonPointer) {
if (hasMetadataFeatureIdWildcard(jsonPointer)) {
return featureIds.stream().map(fid -> Thing.JsonFields.METADATA.getPointer()
.append(Thing.JsonFields.FEATURES.getPointer())
.append(JsonPointer.of(fid))
.append(jsonPointer.getSubPointer(3).orElse(JsonPointer.empty())));
} else if (hasFeatureIdWildcard(jsonPointer)) {
return featureIds.stream().map(fid -> Thing.JsonFields.FEATURES.getPointer()
.append(JsonPointer.of(fid))
.append(jsonPointer.getSubPointer(2).orElse(JsonPointer.empty())));
} else {
return Stream.of(jsonPointer);
}
}
private static boolean hasFeatureIdWildcard(final JsonPointer pointer) {
return pointer.getLevelCount() > 1 &&
isFeaturePath(pointer) && pointer.get(1).filter(FEATURE_ID_WILDCARD::equals).isPresent();
}
private static boolean isFeaturePath(final JsonPointer pointer) {
return pointer.getRoot()
.filter(root -> Thing.JsonFields.FEATURES.getPointer().equals(JsonPointer.of(root)))
.isPresent();
}
private static boolean hasMetadataFeatureIdWildcard(final JsonPointer pointer) {
return pointer.getLevelCount() > 2 &&
isMetadataFeaturePath(pointer) &&
pointer.get(2).filter(FEATURE_ID_WILDCARD::equals).isPresent();
}
private static boolean isMetadataFeaturePath(final JsonPointer pointer) {
return pointer.getRoot()
.filter(root -> Thing.JsonFields.METADATA.getPointer().equals(JsonPointer.of(root)))
.isPresent()
&&
pointer.getSubPointer(1)
.map(metadataRoot -> metadataRoot.getRoot()
.filter(subRoot -> Thing.JsonFields.FEATURES.getPointer()
.equals(JsonPointer.of(subRoot)))
.isPresent()
).orElse(false);
}
}