Skip to content

Commit 7c5dc5c

Browse files
author
jmarkerink
committed
feat: adopt pattern matching
1 parent 55cfe72 commit 7c5dc5c

13 files changed

Lines changed: 92 additions & 90 deletions

File tree

core/src/main/java/de/bwaldvogel/mongo/backend/DefaultQueryMatcher.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ private boolean checkMatchesValue(Object queryValue, Object value, boolean requi
336336

337337
if (queryValue instanceof Document queryObject) {
338338
if (queryObject.keySet().equals(Constants.REFERENCE_KEYS)) {
339-
if (value instanceof Document) {
340-
return matches((Document) value, queryObject);
339+
if (value instanceof Document documentValue) {
340+
return matches(documentValue, queryObject);
341341
} else {
342342
return false;
343343
}
@@ -520,10 +520,10 @@ static boolean matchTypes(Object value, Object expressionValue) {
520520
.map(BsonType::getAlias)
521521
.collect(Collectors.toList());
522522
return matchTypes(value, types);
523-
} else if (expressionValue instanceof String) {
524-
return matchTypes(value, BsonType.forString((String) expressionValue));
525-
} else if (expressionValue instanceof Number) {
526-
return matchTypes(value, BsonType.forNumber((Number) expressionValue));
523+
} else if (expressionValue instanceof String stringValue) {
524+
return matchTypes(value, BsonType.forString(stringValue));
525+
} else if (expressionValue instanceof Number numberValue) {
526+
return matchTypes(value, BsonType.forNumber(numberValue));
527527
} else if (expressionValue instanceof Collection<?> values) {
528528
for (Object type : values) {
529529
if (matchTypes(value, type)) {

core/src/main/java/de/bwaldvogel/mongo/backend/FieldUpdates.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,19 @@ private void handlePush(String key, Object changeValue) {
158158
break;
159159
case "$sort":
160160
Object sortValue = Utils.normalizeValue(entry.getValue());
161-
if (sortValue instanceof Number) {
162-
Number sortOrder = Utils.normalizeNumber((Number) sortValue);
161+
if (sortValue instanceof Number sortNumber) {
162+
Number sortOrder = Utils.normalizeNumber(sortNumber);
163163
if (sortOrder.equals(1)) {
164164
comparator = ValueComparator.asc();
165165
} else if (sortOrder.equals(-1)) {
166166
comparator = ValueComparator.desc();
167167
}
168-
} else if (sortValue instanceof Document) {
168+
} else if (sortValue instanceof Document sortDocument) {
169169
ValueComparator valueComparator = ValueComparator.asc();
170-
DocumentComparator documentComparator = new DocumentComparator((Document) sortValue);
170+
DocumentComparator documentComparator = new DocumentComparator(sortDocument);
171171
comparator = (o1, o2) -> {
172-
if (o1 instanceof Document && o2 instanceof Document) {
173-
return documentComparator.compare((Document) o1, (Document) o2);
172+
if (o1 instanceof Document doc1 && o2 instanceof Document doc2) {
173+
return documentComparator.compare(doc1, doc2);
174174
} else if (o1 instanceof Document || o2 instanceof Document) {
175175
return valueComparator.compare(o1, o2);
176176
} else {

core/src/main/java/de/bwaldvogel/mongo/backend/Utils.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ private static Object getSubdocumentValue(Document document, String key, boolean
5858
String subKey = joinTail(pathFragments);
5959
Assert.doesNotStartWith(subKey, "$.");
6060
Object subObject = getFieldValueListSafe(document, mainKey);
61-
if (subObject instanceof Document) {
62-
return getSubdocumentValue((Document) subObject, subKey, handleCollections);
61+
if (subObject instanceof Document subDocument) {
62+
return getSubdocumentValue(subDocument, subKey, handleCollections);
6363
} else if (handleCollections && subObject instanceof Collection<?> values) {
6464
List<Object> result = new ArrayList<>();
6565
for (Object o : values) {
66-
if (o instanceof Document) {
67-
Object subdocumentValue = getSubdocumentValue((Document) o, subKey, handleCollections);
66+
if (o instanceof Document doc) {
67+
Object subdocumentValue = getSubdocumentValue(doc, subKey, handleCollections);
6868
if (subdocumentValue instanceof Collection) {
6969
result.addAll((Collection<?>) subdocumentValue);
7070
} else {
@@ -93,12 +93,12 @@ public static boolean isTrue(Object value) {
9393
return false;
9494
}
9595

96-
if (value instanceof Boolean) {
97-
return ((Boolean) value).booleanValue();
96+
if (value instanceof Boolean bool) {
97+
return bool.booleanValue();
9898
}
9999

100-
if (value instanceof Number) {
101-
return ((Number) value).doubleValue() != 0.0;
100+
if (value instanceof Number number) {
101+
return number.doubleValue() != 0.0;
102102
}
103103

104104
return true;
@@ -110,8 +110,8 @@ static Object normalizeValue(Object value) {
110110
}
111111
if (value instanceof Long && cannotBeRepresentedAsDouble((Long) value)) {
112112
return value;
113-
} else if (value instanceof Number) {
114-
double doubleValue = ((Number) value).doubleValue();
113+
} else if (value instanceof Number number) {
114+
double doubleValue = number.doubleValue();
115115
if (doubleValue == -0.0) {
116116
doubleValue = 0.0;
117117
}
@@ -221,8 +221,8 @@ static Object getFieldValueListSafe(Object value, String field) throws IllegalAr
221221
} else {
222222
List<Object> values = new ArrayList<>();
223223
for (Object subValue : list) {
224-
if (subValue instanceof Document) {
225-
Object subDocumentValue = ((Document) subValue).getOrMissing(field);
224+
if (subValue instanceof Document subDocument) {
225+
Object subDocumentValue = subDocument.getOrMissing(field);
226226
if (!(subDocumentValue instanceof Missing)) {
227227
values.add(subDocumentValue);
228228
}
@@ -308,8 +308,8 @@ static boolean hasFieldValueListSafe(Object document, String field) throws Illeg
308308
} else {
309309
return false;
310310
}
311-
} else if (document instanceof Document) {
312-
return ((Document) document).containsKey(field);
311+
} else if (document instanceof Document doc) {
312+
return doc.containsKey(field);
313313
}
314314

315315
throw new IllegalArgumentException("illegal document: " + document);

core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/Expression.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,10 @@ Object apply(List<?> expressionValue, Document document) {
284284
Object apply(Object expressionValue, Document document) {
285285
// document values need to be evaluated lazily
286286
List<Object> values = new ArrayList<>();
287-
if (!(expressionValue instanceof Collection)) {
288-
values.add(expressionValue);
287+
if (expressionValue instanceof Collection<?> collectionValue) {
288+
values.addAll(collectionValue);
289289
} else {
290-
values.addAll(((Collection<?>) expressionValue));
290+
values.add(expressionValue);
291291
}
292292
return apply(values, document);
293293
}
@@ -370,10 +370,10 @@ Object apply(List<?> expression, Document document) {
370370

371371
private BsonType getBsonType(Object to) {
372372
try {
373-
if (to instanceof String) {
374-
return BsonType.forString((String) to);
375-
} else if (to instanceof Integer) {
376-
return BsonType.forNumber((Integer) to);
373+
if (to instanceof String toString) {
374+
return BsonType.forString(toString);
375+
} else if (to instanceof Integer toNumber) {
376+
return BsonType.forNumber(toNumber);
377377
} else if (to instanceof Number) {
378378
throw new IllegalArgumentException("In $convert, numeric 'to' argument is not an integer");
379379
} else {
@@ -1396,29 +1396,29 @@ Object apply(List<?> expressionValue, Document document) {
13961396
@Override
13971397
Object apply(List<?> expressionValue, Document document) {
13981398
TwoParameters parameters = requireTwoParameters(expressionValue);
1399-
Object one = parameters.getFirst();
1400-
Object other = parameters.getSecond();
1399+
Object first = parameters.getFirst();
1400+
Object second = parameters.getSecond();
14011401

1402-
if (isNullOrMissing(one) || isNullOrMissing(other)) {
1402+
if (isNullOrMissing(first) || isNullOrMissing(second)) {
14031403
return null;
14041404
}
14051405

1406-
if (one instanceof Number && other instanceof Number) {
1407-
return NumericUtils.subtractNumbers((Number) one, (Number) other);
1406+
if (first instanceof Number firstNumber && second instanceof Number secondNumber) {
1407+
return NumericUtils.subtractNumbers(firstNumber, secondNumber);
14081408
}
14091409

1410-
if (one instanceof Instant) {
1410+
if (first instanceof Instant firstInstant) {
14111411
// subtract two instants (returns the difference in milliseconds)
1412-
if (other instanceof Instant) {
1413-
return ((Instant) one).toEpochMilli() - ((Instant) other).toEpochMilli();
1412+
if (second instanceof Instant secondInstant) {
1413+
return firstInstant.toEpochMilli() - secondInstant.toEpochMilli();
14141414
}
14151415
// subtract milliseconds from instant
1416-
if (other instanceof Number) {
1417-
return Instant.ofEpochMilli(((Instant) one).toEpochMilli() - ((Number) other).longValue());
1416+
if (second instanceof Number secondNumber) {
1417+
return Instant.ofEpochMilli(firstInstant.toEpochMilli() - secondNumber.longValue());
14181418
}
14191419
}
14201420

1421-
throw new MongoServerError(16556, "cant " + name() + " a " + describeType(one) + " from a " + describeType(other));
1421+
throw new MongoServerError(16556, "cant " + name() + " a " + describeType(first) + " from a " + describeType(second));
14221422
}
14231423
},
14241424

@@ -1433,8 +1433,8 @@ Object apply(List<?> expressionValue, Document document) {
14331433
}
14341434
Number sum = 0;
14351435
for (Object value : expressionValue) {
1436-
if (value instanceof Number) {
1437-
sum = NumericUtils.addNumbers(sum, (Number) value);
1436+
if (value instanceof Number number) {
1437+
sum = NumericUtils.addNumbers(sum, number);
14381438
}
14391439
}
14401440
return sum;
@@ -1761,11 +1761,11 @@ public static Object evaluateDocument(Object documentWithExpression, Document do
17611761
}
17621762

17631763
static Object evaluate(Object expression, Document document) {
1764-
if (expression instanceof String && ((String) expression).startsWith("$")) {
1764+
if (expression instanceof String expressionString && expressionString.startsWith("$")) {
17651765
if (KEYWORD_EXPRESSIONS.contains(expression)) {
17661766
return expression;
17671767
}
1768-
String value = ((String) expression).substring(1);
1768+
String value = expressionString.substring(1);
17691769
if (value.startsWith("$")) {
17701770
final String variableName;
17711771
if (value.contains(".")) {
@@ -1799,8 +1799,8 @@ static Object evaluate(Object expression, Document document) {
17991799
}
18001800
}
18011801
return Utils.getSubdocumentValueCollectionAware(document, value);
1802-
} else if (expression instanceof Document) {
1803-
return evaluateDocumentExpression((Document) expression, document);
1802+
} else if (expression instanceof Document expressionDoc) {
1803+
return evaluateDocumentExpression(expressionDoc, document);
18041804
} else {
18051805
return expression;
18061806
}

core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/accumulator/AvgAccumulator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public AvgAccumulator(String field, Object expression) {
1313

1414
@Override
1515
public void aggregate(Object value) {
16-
if (value instanceof Number) {
17-
sum = NumericUtils.addNumbers(sum, (Number) value);
16+
if (value instanceof Number number) {
17+
sum = NumericUtils.addNumbers(sum, number);
1818
count++;
1919
}
2020
}

core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/accumulator/SumAccumulator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public SumAccumulator(String field, Object expression) {
1212

1313
@Override
1414
public void aggregate(Object value) {
15-
if (value instanceof Number) {
16-
sum = NumericUtils.addNumbers(sum, (Number) value);
15+
if (value instanceof Number number) {
16+
sum = NumericUtils.addNumbers(sum, number);
1717
}
1818
}
1919

core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/AbstractLookupStage.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ String readStringConfigurationProperty(Document configuration, String name) {
2222
if (value == null) {
2323
throw new FailedToParseException("missing '" + name + "' option to " + name() + " stage specification: " + configuration);
2424
}
25-
if (value instanceof String) {
26-
return (String) value;
25+
if (value instanceof String string) {
26+
return string;
2727
}
2828
throw new FailedToParseException("'" + name + "' option to " + name() + " must be a string, but was type " + Utils.describeType(value));
2929
}
@@ -33,8 +33,8 @@ Document readOptionalDocumentArgument(Document configuration, String name) {
3333
if (value == null) {
3434
return new Document();
3535
}
36-
if (value instanceof Document) {
37-
return (Document) value;
36+
if (value instanceof Document document) {
37+
return document;
3838
}
3939
throw new FailedToParseException(name() + " argument '" + name + ": " + Json.toJsonValue(value) + "' must be an object, is type " + Utils.describeType(value));
4040
}

core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/GraphLookupStage.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ Integer readOptionalIntegerConfigurationProperty(Document configuration, String
6565
if (value == null) {
6666
return null;
6767
}
68-
if (value instanceof Integer) {
69-
return (Integer) value;
68+
if (value instanceof Integer integer) {
69+
return integer;
7070
}
7171
throw new FailedToParseException("'" + name + "' option to \" + stageName + \" must be a integer, but was type " + Utils.describeType(value));
7272
}
@@ -76,8 +76,8 @@ String readOptionalStringConfigurationProperty(Document configuration, String na
7676
if (value == null) {
7777
return null;
7878
}
79-
if (value instanceof String) {
80-
return (String) value;
79+
if (value instanceof String string) {
80+
return string;
8181
}
8282
throw new FailedToParseException("'" + name + "' option to \" + stageName + \" must be a string, but was type " + Utils.describeType(value));
8383
}
@@ -113,8 +113,8 @@ private List<Document> findLinkedDocuments(long depth, final List<Document> link
113113
return linked;
114114
}
115115

116-
if (value instanceof List) {
117-
return ((List<?>) value).stream()
116+
if (value instanceof List<?> list) {
117+
return list.stream()
118118
.flatMap(item -> findLinkedDocuments(depth + 1, linked, item).stream())
119119
.collect(toList());
120120
}

core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/MergeStage.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,20 @@ private Map<String, Object> getLet(Document paramsDocument) {
9393
Object let = paramsDocument.get("let");
9494
if (let == null) {
9595
return new Document("$new", "$$ROOT");
96-
} else if (!(let instanceof Document)) {
96+
}
97+
98+
if (!(let instanceof Document)) {
9799
throw new TypeMismatchException("BSON field '$merge.let' is the wrong type '" + Utils.describeType(let) + "', expected type 'object'");
98-
} else {
99-
Map<String, Object> variables = new LinkedHashMap<>();
100-
for (Map.Entry<String, Object> entry : ((Document) let).entrySet()) {
101-
if (entry.getKey().equals("new") && !entry.getValue().equals("$$ROOT")) {
102-
throw new MongoServerError(51273, "'let' may not define a value for the reserved 'new' variable other than '$$ROOT'");
103-
}
104-
variables.put("$" + entry.getKey(), entry.getValue());
100+
}
101+
102+
Map<String, Object> variables = new LinkedHashMap<>();
103+
for (Map.Entry<String, Object> entry : ((Document) let).entrySet()) {
104+
if (entry.getKey().equals("new") && !entry.getValue().equals("$$ROOT")) {
105+
throw new MongoServerError(51273, "'let' may not define a value for the reserved 'new' variable other than '$$ROOT'");
105106
}
106-
return variables;
107+
variables.put("$" + entry.getKey(), entry.getValue());
107108
}
109+
return variables;
108110
}
109111

110112
private static Supplier<MongoCollection<?>> getTargetCollectionSupplier(DatabaseResolver databaseResolver,
@@ -146,9 +148,10 @@ private boolean matchesJoinFields(Index<?> index) {
146148

147149
private Set<String> getJoinFields(Document paramsDocument) {
148150
Object on = paramsDocument.getOrDefault("on", "_id");
149-
if (on instanceof String) {
150-
return Set.of((String) on);
151-
} else if (on instanceof Collection<?> collection) {
151+
if (on instanceof String string) {
152+
return Set.of(string);
153+
}
154+
if (on instanceof Collection<?> collection) {
152155
if (collection.isEmpty()) {
153156
throw new MongoServerError(51187, "If explicitly specifying $merge 'on', must include at least one field");
154157
}
@@ -170,9 +173,9 @@ private Set<String> getJoinFields(Document paramsDocument) {
170173

171174
private WhenMatched getWhenMatched(Document paramsDocument) {
172175
Object whenMatched = paramsDocument.getOrDefault("whenMatched", WhenMatched.merge.name());
173-
if (whenMatched instanceof String) {
176+
if (whenMatched instanceof String matched) {
174177
try {
175-
return WhenMatched.valueOf((String) whenMatched);
178+
return WhenMatched.valueOf(matched);
176179
} catch (IllegalArgumentException e) {
177180
throw new BadValueException("Enumeration value '" + whenMatched + "' for field 'whenMatched' is not a valid value.");
178181
}

core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/UnwindStage.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public UnwindStage(Object input) {
2424
}
2525

2626
final String fieldPath;
27-
if (input instanceof Document) {
28-
Document inputDocument = (Document) input;
27+
if (input instanceof Document inputDocument) {
2928
if (!inputDocument.containsKey("path")) {
3029
throw new MongoServerError(28812, "no path specified to $unwind stage");
3130
}

0 commit comments

Comments
 (0)