Skip to content

Commit 3a75078

Browse files
authored
Merge pull request #240 from jmarkerink/feat/adopt-pattern-matching
feat: adopt pattern matching
2 parents ad32036 + de2531e commit 3a75078

File tree

17 files changed

+69
-139
lines changed

17 files changed

+69
-139
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,8 +691,7 @@ private void convertSelectorToDocument(Document selector, Document document) {
691691
continue;
692692
} else if (key.startsWith("$")) {
693693
continue;
694-
} else if (value instanceof Document) {
695-
Document documentValue = (Document) value;
694+
} else if (value instanceof Document documentValue) {
696695
if (documentValue.keySet().equals(Set.of("$eq"))) {
697696
changeSubdocumentValueOrThrow(document, key, documentValue.get("$eq"));
698697
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -865,8 +865,7 @@ private Document toWriteError(int index, MongoServerException e) {
865865
Document error = new Document();
866866
error.put("index", index);
867867
error.put("errmsg", e.getMessageWithoutErrorCode());
868-
if (e instanceof MongoServerError) {
869-
MongoServerError err = (MongoServerError) e;
868+
if (e instanceof MongoServerError err) {
870869
error.put("code", Integer.valueOf(err.getCode()));
871870
error.putIfNotNull("codeName", err.getCodeName());
872871
}
@@ -876,8 +875,7 @@ private Document toWriteError(int index, MongoServerException e) {
876875
private Document toError(Channel channel, MongoServerException ex) {
877876
Document error = new Document();
878877
error.put("err", ex.getMessageWithoutErrorCode());
879-
if (ex instanceof MongoServerError) {
880-
MongoServerError err = (MongoServerError) ex;
878+
if (ex instanceof MongoServerError err) {
881879
error.put("code", Integer.valueOf(err.getCode()));
882880
error.putIfNotNull("codeName", err.getCodeName());
883881
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,10 @@ public synchronized Iterable<P> getPositions(Document query) {
202202
}
203203
}
204204
return positions;
205-
} else if (queriedValue instanceof Document) {
205+
} else if (queriedValue instanceof Document keyObj) {
206206
if (isCompoundIndex()) {
207207
throw new UnsupportedOperationException("Not yet implemented");
208208
}
209-
Document keyObj = (Document) queriedValue;
210209
if (Utils.containsQueryExpression(keyObj)) {
211210
String expression = CollectionUtils.getSingleElement(keyObj.keySet(),
212211
() -> new UnsupportedOperationException("illegal query key: " + queriedKeyValues));
@@ -215,8 +214,7 @@ public synchronized Iterable<P> getPositions(Document query) {
215214
return getPositionsForExpression(keyObj, expression);
216215
}
217216
}
218-
} else if (queriedValue instanceof Collection) {
219-
Collection<?> values = (Collection<?>) queriedValue;
217+
} else if (queriedValue instanceof Collection<?> values) {
220218
return values.stream()
221219
.map(KeyValue::new)
222220
.map(this::getPosition)

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ static <T> List<List<T>> multiplyWithOtherElements(Collection<T> allValues, Coll
6868
}
6969

7070
static <T> T getElementAtPosition(Iterable<T> iterable, int pos) {
71-
if (iterable instanceof List) {
72-
List<T> list = (List<T>) iterable;
71+
if (iterable instanceof List<T> list) {
7372
return list.get(pos);
7473
} else {
7574
Iterator<T> iterator = iterable.iterator();

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

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ private void validateQueryValue(Object queryValue, String key) {
5959
QueryOperator queryOperator = QueryOperator.fromValue(operator);
6060
if (queryOperator == QueryOperator.TYPE) {
6161
Object value = queryObject.get(operator);
62-
if (value instanceof Collection) {
63-
Collection<?> values = (Collection<?>) value;
62+
if (value instanceof Collection<?> values) {
6463
if (values.isEmpty()) {
6564
throw new FailedToParseException(key + " must match at least one type");
6665
}
@@ -134,8 +133,7 @@ private boolean checkMatch(Object queryValue, List<String> keys, Object value) {
134133
return checkMatchesValue(queryValue, value);
135134
}
136135

137-
if (queryValue instanceof Document) {
138-
Document query = (Document) queryValue;
136+
if (queryValue instanceof Document query) {
139137
if (query.containsKey(QueryOperator.ALL.getValue())) {
140138
Object allQuery = query.get(QueryOperator.ALL.getValue());
141139
return checkMatchesAllDocuments(allQuery, keys, value);
@@ -171,10 +169,8 @@ private boolean checkMatch(Object queryValue, List<String> keys, Object value) {
171169
return checkMatchesValue(queryValue, Missing.getInstance());
172170
}
173171

174-
if (documentValue instanceof Collection<?>) {
175-
Collection<?> documentValues = (Collection<?>) documentValue;
176-
if (queryValue instanceof Document) {
177-
Document queryDocument = (Document) queryValue;
172+
if (documentValue instanceof Collection<?> documentValues) {
173+
if (queryValue instanceof Document queryDocument) {
178174
boolean matches = checkMatchesAnyValue(queryDocument, keys, document, documentValues);
179175
if (matches) {
180176
return true;
@@ -338,9 +334,7 @@ private boolean checkMatchesValue(Object queryValue, Object value, boolean requi
338334
}
339335
}
340336

341-
if (queryValue instanceof Document) {
342-
Document queryObject = (Document) queryValue;
343-
337+
if (queryValue instanceof Document queryObject) {
344338
if (queryObject.keySet().equals(Constants.REFERENCE_KEYS)) {
345339
if (value instanceof Document) {
346340
return matches((Document) value, queryObject);
@@ -412,8 +406,7 @@ private boolean checkMatchesElemValues(Object queryValue, Object values) {
412406
}
413407

414408
private boolean checkMatchesAnyValue(Object queryValue, Collection<?> values) {
415-
if (queryValue instanceof Document) {
416-
Document queryDocument = (Document) queryValue;
409+
if (queryValue instanceof Document queryDocument) {
417410
if (queryDocument.keySet().equals(Set.of(QueryOperator.ELEM_MATCH.getValue()))) {
418411
queryValue = queryDocument.get(QueryOperator.ELEM_MATCH.getValue());
419412
}
@@ -445,13 +438,11 @@ private boolean checkExpressionMatch(Object value, Object expressionValue, Strin
445438
case IN:
446439
Collection<?> queriedObjects = (Collection<?>) expressionValue;
447440
for (Object o : queriedObjects) {
448-
if (o instanceof BsonRegularExpression && value instanceof String) {
449-
BsonRegularExpression pattern = (BsonRegularExpression) o;
450-
if (pattern.matcher((String) value).find()) {
441+
if (o instanceof BsonRegularExpression pattern && value instanceof String strValue) {
442+
if (pattern.matcher(strValue).find()) {
451443
return true;
452444
}
453-
} else if (value instanceof Collection && !(o instanceof Collection)) {
454-
Collection<?> values = (Collection<?>) value;
445+
} else if (value instanceof Collection<?> values && !(o instanceof Collection)) {
455446
return values.stream().anyMatch(v -> Utils.nullAwareEquals(o, v));
456447
} else if (Utils.nullAwareEquals(o, value)) {
457448
return true;
@@ -533,8 +524,7 @@ static boolean matchTypes(Object value, Object expressionValue) {
533524
return matchTypes(value, BsonType.forString((String) expressionValue));
534525
} else if (expressionValue instanceof Number) {
535526
return matchTypes(value, BsonType.forNumber((Number) expressionValue));
536-
} else if (expressionValue instanceof Collection) {
537-
Collection<?> values = (Collection<?>) expressionValue;
527+
} else if (expressionValue instanceof Collection<?> values) {
538528
for (Object type : values) {
539529
if (matchTypes(value, type)) {
540530
return true;

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ private void handlePush(String key, Object changeValue) {
140140
Integer slice = null;
141141
Comparator<Object> comparator = null;
142142
int position = existingValue.size();
143-
if (changeValue instanceof Document && ((Document) changeValue).containsKey("$each")) {
144-
Document pushDocument = (Document) changeValue;
143+
if (changeValue instanceof Document pushDocument && pushDocument.containsKey("$each")) {
145144
for (Entry<String, Object> entry : pushDocument.entrySet()) {
146145
String modifier = entry.getKey();
147146
switch (modifier) {
@@ -228,8 +227,7 @@ private void handleAddToSet(String key, Object changeValue) {
228227
"Cannot apply $addToSet to non-array field. Field named '" + key + "' has non-array type " + describeType(value)));
229228

230229
Collection<Object> pushValues = new ArrayList<>();
231-
if (changeValue instanceof Document && ((Document) changeValue).keySet().iterator().next().equals("$each")) {
232-
Document addToSetDocument = (Document) changeValue;
230+
if (changeValue instanceof Document addToSetDocument && addToSetDocument.keySet().iterator().next().equals("$each")) {
233231
for (String modifier : addToSetDocument.keySet()) {
234232
if (!modifier.equals("$each")) {
235233
throw new BadValueException("Found unexpected fields after $each in $addToSet: " + addToSetDocument.toString(true, "{ ", " }"));

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ private static void projectField(Document document, Document newDocument, String
113113
if (object instanceof Document) {
114114
Document newsubDocument = (Document) newDocument.computeIfAbsent(mainKey, k -> new Document());
115115
projectField((Document) object, newsubDocument, subKey, projectionValue);
116-
} else if (object instanceof List) {
117-
List<?> values = (List<?>) object;
116+
} else if (object instanceof List<?> values) {
118117
List<Object> newprojectedValues = (List<Object>) newDocument.computeIfAbsent(mainKey, k -> new ArrayList<>());
119118

120119
if ("$".equals(subKey) && !values.isEmpty()) {
@@ -153,8 +152,7 @@ else if (!Utils.isTrue(projectionValue)) {
153152
} else {
154153
Object value = document.getOrMissing(key);
155154

156-
if (projectionValue instanceof Document) {
157-
Document projectionDocument = (Document) projectionValue;
155+
if (projectionValue instanceof Document projectionDocument) {
158156
if (projectionDocument.keySet().equals(Set.of(QueryOperator.ELEM_MATCH.getValue()))) {
159157
Document elemMatch = (Document) projectionDocument.get(QueryOperator.ELEM_MATCH.getValue());
160158
projectElemMatch(newDocument, elemMatch, key, value);
@@ -202,8 +200,7 @@ private static void projectSlice(Document newDocument, Object slice, String key,
202200
} else {
203201
toIndex = num;
204202
}
205-
} else if (slice instanceof List) {
206-
List<?> sliceParams = (List<?>) slice;
203+
} else if (slice instanceof List<?> sliceParams) {
207204
if (sliceParams.size() != 2) {
208205
throw new MongoServerError(28724, "First argument to $slice must be an array, but is of type: int");
209206
}

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

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ private static Object getSubdocumentValue(Document document, String key, boolean
6060
Object subObject = getFieldValueListSafe(document, mainKey);
6161
if (subObject instanceof Document) {
6262
return getSubdocumentValue((Document) subObject, subKey, handleCollections);
63-
} else if (handleCollections && subObject instanceof Collection) {
64-
Collection<?> values = (Collection<?>) subObject;
63+
} else if (handleCollections && subObject instanceof Collection<?> values) {
6564
List<Object> result = new ArrayList<>();
6665
for (Object o : values) {
6766
if (o instanceof Document) {
@@ -125,8 +124,7 @@ static Object normalizeValue(Object value) {
125124
result.put(entry.getKey(), normalizeValue(entry.getValue()));
126125
}
127126
return result;
128-
} else if (value instanceof Collection<?>) {
129-
Collection<?> collection = (Collection<?>) value;
127+
} else if (value instanceof Collection<?> collection) {
130128
return collection.stream()
131129
.map(Utils::normalizeValue)
132130
.collect(Collectors.toList());
@@ -212,8 +210,7 @@ static Object getFieldValueListSafe(Object value, String field) throws IllegalAr
212210
throw new IllegalArgumentException("illegal field: " + field);
213211
}
214212

215-
if (value instanceof List<?>) {
216-
List<?> list = (List<?>) value;
213+
if (value instanceof List<?> list) {
217214
if (isNumeric(field)) {
218215
int pos = Integer.parseInt(field);
219216
if (pos >= 0 && pos < list.size()) {
@@ -236,8 +233,7 @@ static Object getFieldValueListSafe(Object value, String field) throws IllegalAr
236233
}
237234
return values;
238235
}
239-
} else if (value instanceof Document) {
240-
Document document = (Document) value;
236+
} else if (value instanceof Document document) {
241237
return document.getOrMissing(field);
242238
} else {
243239
return Missing.getInstance();
@@ -305,10 +301,9 @@ static boolean hasFieldValueListSafe(Object document, String field) throws Illeg
305301
throw new IllegalArgumentException("illegal field: " + field);
306302
}
307303

308-
if (document instanceof List<?>) {
304+
if (document instanceof List<?> list) {
309305
if (isNumeric(field)) {
310306
int pos = Integer.parseInt(field);
311-
List<?> list = (List<?>) document;
312307
return (pos >= 0 && pos < list.size());
313308
} else {
314309
return false;
@@ -344,14 +339,12 @@ private static Object setListSafe(Object document, String key, String previousKe
344339
}
345340

346341
private static Object removeListSafe(Object value, String key) {
347-
if (value instanceof Document) {
348-
Document document = (Document) value;
342+
if (value instanceof Document document) {
349343
if (document.containsKey(key)) {
350344
return document.remove(key);
351345
}
352346
return Missing.getInstance();
353-
} else if (value instanceof List<?>) {
354-
List<?> values = ((List<?>) value);
347+
} else if (value instanceof List<?> values) {
355348
if (isNumeric(key)) {
356349
int pos = Integer.parseInt(key);
357350
if (values.size() > pos) {
@@ -367,8 +360,7 @@ private static Object removeListSafe(Object value, String key) {
367360
if (!(removedValue instanceof Missing)) {
368361
removedValues.add(removedValue);
369362
}
370-
} else if (subValue instanceof List) {
371-
List<?> subValueList = (List<?>) subValue;
363+
} else if (subValue instanceof List<?> subValueList) {
372364
for (Object subValueListValue : subValueList) {
373365
Object removedValue = removeListSafe(subValueListValue, key);
374366
if (!(removedValue instanceof Missing)) {
@@ -427,17 +419,15 @@ public static void validateFieldNames(Document document) {
427419
}
428420

429421
private static void validateFieldNames(Object value, String path) {
430-
if (value instanceof Document) {
431-
Document document = (Document) value;
422+
if (value instanceof Document document) {
432423
for (Entry<String, Object> entry : document.entrySet()) {
433424
String key = entry.getKey();
434425
String nextPath = path != null ? path + "." + key : key;
435426
if (key.startsWith("$") && !Constants.REFERENCE_KEYS.contains(key)) {
436427
throw new DollarPrefixedFieldNameException("The dollar ($) prefixed field '" + key + "' in '" + nextPath + "' is not allowed in the context of an update's replacement document. Consider using an aggregation pipeline with $replaceWith.");
437428
}
438429
}
439-
} else if (value instanceof Collection<?>) {
440-
Collection<?> values = (Collection<?>) value;
430+
} else if (value instanceof Collection<?> values) {
441431
for (Object object : values) {
442432
validateFieldNames(object, path + ".");
443433
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,7 @@ private int doCompare(Object value1, Object value2) {
134134
return ((ObjectId) value1).compareTo((ObjectId) value2);
135135
}
136136

137-
if (value1 instanceof Decimal128 && value2 instanceof Decimal128) {
138-
Decimal128 decimal1 = (Decimal128) value1;
139-
Decimal128 decimal2 = (Decimal128) value2;
137+
if (value1 instanceof Decimal128 decimal1 && value2 instanceof Decimal128 decimal2) {
140138
return decimal1.compareTo(decimal2);
141139
}
142140

0 commit comments

Comments
 (0)