Skip to content

Commit ba56b28

Browse files
authored
Merge pull request #242 from jmarkerink/chore/adopt-java17-features
chore: adopt java17 features
2 parents 55cfe72 + ec85489 commit ba56b28

File tree

15 files changed

+119
-148
lines changed

15 files changed

+119
-148
lines changed

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: 34 additions & 51 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 {
@@ -388,33 +388,16 @@ private BsonType getBsonType(Object to) {
388388

389389
private Object convert(Object inputValue, BsonType bsonType, Document document, Document convertDocument) {
390390
try {
391-
switch (bsonType) {
392-
case DOUBLE:
393-
return $toDouble.apply(inputValue, document);
394-
case STRING:
395-
return $toString.apply(inputValue, document);
396-
case OBJECT_ID:
397-
return $toObjectId.apply(inputValue, document);
398-
case BOOL:
399-
return $toBool.apply(inputValue, document);
400-
case DATE:
401-
return $toDate.apply(inputValue, document);
402-
case INT:
403-
return $toInt.apply(inputValue, document);
404-
case LONG:
405-
return $toLong.apply(inputValue, document);
406-
case OBJECT:
407-
case ARRAY:
408-
case BIN_DATA:
409-
case NULL:
410-
case REGEX:
411-
case TIMESTAMP:
412-
case DECIMAL128:
413-
case MIN_KEY:
414-
case MAX_KEY:
415-
default:
416-
throw new UnsupportedOperationException("Unsupported conversion to type " + bsonType);
417-
}
391+
return switch (bsonType) {
392+
case DOUBLE -> $toDouble.apply(inputValue, document);
393+
case STRING -> $toString.apply(inputValue, document);
394+
case OBJECT_ID -> $toObjectId.apply(inputValue, document);
395+
case BOOL -> $toBool.apply(inputValue, document);
396+
case DATE -> $toDate.apply(inputValue, document);
397+
case INT -> $toInt.apply(inputValue, document);
398+
case LONG -> $toLong.apply(inputValue, document);
399+
default -> throw new UnsupportedOperationException("Unsupported conversion to type " + bsonType);
400+
};
418401
} catch (MongoServerError e) {
419402
if (e.hasCode(ErrorCode.ConversionFailure)) {
420403
if (convertDocument.containsKey("onError")) {
@@ -1396,29 +1379,29 @@ Object apply(List<?> expressionValue, Document document) {
13961379
@Override
13971380
Object apply(List<?> expressionValue, Document document) {
13981381
TwoParameters parameters = requireTwoParameters(expressionValue);
1399-
Object one = parameters.getFirst();
1400-
Object other = parameters.getSecond();
1382+
Object first = parameters.getFirst();
1383+
Object second = parameters.getSecond();
14011384

1402-
if (isNullOrMissing(one) || isNullOrMissing(other)) {
1385+
if (isNullOrMissing(first) || isNullOrMissing(second)) {
14031386
return null;
14041387
}
14051388

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

1410-
if (one instanceof Instant) {
1393+
if (first instanceof Instant firstInstant) {
14111394
// subtract two instants (returns the difference in milliseconds)
1412-
if (other instanceof Instant) {
1413-
return ((Instant) one).toEpochMilli() - ((Instant) other).toEpochMilli();
1395+
if (second instanceof Instant secondInstant) {
1396+
return firstInstant.toEpochMilli() - secondInstant.toEpochMilli();
14141397
}
14151398
// subtract milliseconds from instant
1416-
if (other instanceof Number) {
1417-
return Instant.ofEpochMilli(((Instant) one).toEpochMilli() - ((Number) other).longValue());
1399+
if (second instanceof Number secondNumber) {
1400+
return Instant.ofEpochMilli(firstInstant.toEpochMilli() - secondNumber.longValue());
14181401
}
14191402
}
14201403

1421-
throw new MongoServerError(16556, "cant " + name() + " a " + describeType(one) + " from a " + describeType(other));
1404+
throw new MongoServerError(16556, "cant " + name() + " a " + describeType(first) + " from a " + describeType(second));
14221405
}
14231406
},
14241407

@@ -1433,8 +1416,8 @@ Object apply(List<?> expressionValue, Document document) {
14331416
}
14341417
Number sum = 0;
14351418
for (Object value : expressionValue) {
1436-
if (value instanceof Number) {
1437-
sum = NumericUtils.addNumbers(sum, (Number) value);
1419+
if (value instanceof Number number) {
1420+
sum = NumericUtils.addNumbers(sum, number);
14381421
}
14391422
}
14401423
return sum;
@@ -1761,11 +1744,11 @@ public static Object evaluateDocument(Object documentWithExpression, Document do
17611744
}
17621745

17631746
static Object evaluate(Object expression, Document document) {
1764-
if (expression instanceof String && ((String) expression).startsWith("$")) {
1747+
if (expression instanceof String expressionString && expressionString.startsWith("$")) {
17651748
if (KEYWORD_EXPRESSIONS.contains(expression)) {
17661749
return expression;
17671750
}
1768-
String value = ((String) expression).substring(1);
1751+
String value = expressionString.substring(1);
17691752
if (value.startsWith("$")) {
17701753
final String variableName;
17711754
if (value.contains(".")) {
@@ -1799,8 +1782,8 @@ static Object evaluate(Object expression, Document document) {
17991782
}
18001783
}
18011784
return Utils.getSubdocumentValueCollectionAware(document, value);
1802-
} else if (expression instanceof Document) {
1803-
return evaluateDocumentExpression((Document) expression, document);
1785+
} else if (expression instanceof Document expressionDoc) {
1786+
return evaluateDocumentExpression(expressionDoc, document);
18041787
} else {
18051788
return expression;
18061789
}

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,16 @@ public static Map<String, Supplier<Accumulator>> parse(Document configuration) {
3333
});
3434
String groupOperator = aggregation.getKey();
3535
Object expression = aggregation.getValue();
36-
if (groupOperator.equals("$sum")) {
37-
accumulators.put(field, () -> new SumAccumulator(field, expression));
38-
} else if (groupOperator.equals("$min")) {
39-
accumulators.put(field, () -> new MinAccumulator(field, expression));
40-
} else if (groupOperator.equals("$max")) {
41-
accumulators.put(field, () -> new MaxAccumulator(field, expression));
42-
} else if (groupOperator.equals("$avg")) {
43-
accumulators.put(field, () -> new AvgAccumulator(field, expression));
44-
} else if (groupOperator.equals("$addToSet")) {
45-
accumulators.put(field, () -> new AddToSetAccumulator(field, expression));
46-
} else if (groupOperator.equals("$push")) {
47-
accumulators.put(field, () -> new PushAccumulator(field, expression));
48-
} else if (groupOperator.equals("$first")) {
49-
accumulators.put(field, () -> new FirstAccumulator(field, expression));
50-
} else if (groupOperator.equals("$last")) {
51-
accumulators.put(field, () -> new LastAccumulator(field, expression));
52-
} else {
53-
throw new MongoServerError(15952, "unknown group operator '" + groupOperator + "'");
36+
switch (groupOperator) {
37+
case "$sum" -> accumulators.put(field, () -> new SumAccumulator(field, expression));
38+
case "$min" -> accumulators.put(field, () -> new MinAccumulator(field, expression));
39+
case "$max" -> accumulators.put(field, () -> new MaxAccumulator(field, expression));
40+
case "$avg" -> accumulators.put(field, () -> new AvgAccumulator(field, expression));
41+
case "$addToSet" -> accumulators.put(field, () -> new AddToSetAccumulator(field, expression));
42+
case "$push" -> accumulators.put(field, () -> new PushAccumulator(field, expression));
43+
case "$first" -> accumulators.put(field, () -> new FirstAccumulator(field, expression));
44+
case "$last" -> accumulators.put(field, () -> new LastAccumulator(field, expression));
45+
default -> throw new MongoServerError(15952, "unknown group operator '" + groupOperator + "'");
5446
}
5547
}
5648
return accumulators;

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
}

0 commit comments

Comments
 (0)