diff --git a/core/src/main/java/de/bwaldvogel/mongo/backend/AbstractMongoCollection.java b/core/src/main/java/de/bwaldvogel/mongo/backend/AbstractMongoCollection.java index c9f8709d..bee4890d 100755 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/AbstractMongoCollection.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/AbstractMongoCollection.java @@ -691,8 +691,7 @@ private void convertSelectorToDocument(Document selector, Document document) { continue; } else if (key.startsWith("$")) { continue; - } else if (value instanceof Document) { - Document documentValue = (Document) value; + } else if (value instanceof Document documentValue) { if (documentValue.keySet().equals(Set.of("$eq"))) { changeSubdocumentValueOrThrow(document, key, documentValue.get("$eq")); } diff --git a/core/src/main/java/de/bwaldvogel/mongo/backend/AbstractMongoDatabase.java b/core/src/main/java/de/bwaldvogel/mongo/backend/AbstractMongoDatabase.java index 3f492fe5..5d91a64d 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/AbstractMongoDatabase.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/AbstractMongoDatabase.java @@ -865,8 +865,7 @@ private Document toWriteError(int index, MongoServerException e) { Document error = new Document(); error.put("index", index); error.put("errmsg", e.getMessageWithoutErrorCode()); - if (e instanceof MongoServerError) { - MongoServerError err = (MongoServerError) e; + if (e instanceof MongoServerError err) { error.put("code", Integer.valueOf(err.getCode())); error.putIfNotNull("codeName", err.getCodeName()); } @@ -876,8 +875,7 @@ private Document toWriteError(int index, MongoServerException e) { private Document toError(Channel channel, MongoServerException ex) { Document error = new Document(); error.put("err", ex.getMessageWithoutErrorCode()); - if (ex instanceof MongoServerError) { - MongoServerError err = (MongoServerError) ex; + if (ex instanceof MongoServerError err) { error.put("code", Integer.valueOf(err.getCode())); error.putIfNotNull("codeName", err.getCodeName()); } diff --git a/core/src/main/java/de/bwaldvogel/mongo/backend/AbstractUniqueIndex.java b/core/src/main/java/de/bwaldvogel/mongo/backend/AbstractUniqueIndex.java index 909f326d..9050b0e2 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/AbstractUniqueIndex.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/AbstractUniqueIndex.java @@ -202,11 +202,10 @@ public synchronized Iterable

getPositions(Document query) { } } return positions; - } else if (queriedValue instanceof Document) { + } else if (queriedValue instanceof Document keyObj) { if (isCompoundIndex()) { throw new UnsupportedOperationException("Not yet implemented"); } - Document keyObj = (Document) queriedValue; if (Utils.containsQueryExpression(keyObj)) { String expression = CollectionUtils.getSingleElement(keyObj.keySet(), () -> new UnsupportedOperationException("illegal query key: " + queriedKeyValues)); @@ -215,8 +214,7 @@ public synchronized Iterable

getPositions(Document query) { return getPositionsForExpression(keyObj, expression); } } - } else if (queriedValue instanceof Collection) { - Collection values = (Collection) queriedValue; + } else if (queriedValue instanceof Collection values) { return values.stream() .map(KeyValue::new) .map(this::getPosition) diff --git a/core/src/main/java/de/bwaldvogel/mongo/backend/CollectionUtils.java b/core/src/main/java/de/bwaldvogel/mongo/backend/CollectionUtils.java index 0fe25747..8925ba29 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/CollectionUtils.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/CollectionUtils.java @@ -68,8 +68,7 @@ static List> multiplyWithOtherElements(Collection allValues, Coll } static T getElementAtPosition(Iterable iterable, int pos) { - if (iterable instanceof List) { - List list = (List) iterable; + if (iterable instanceof List list) { return list.get(pos); } else { Iterator iterator = iterable.iterator(); diff --git a/core/src/main/java/de/bwaldvogel/mongo/backend/DefaultQueryMatcher.java b/core/src/main/java/de/bwaldvogel/mongo/backend/DefaultQueryMatcher.java index 510dc0b9..cdf1d413 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/DefaultQueryMatcher.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/DefaultQueryMatcher.java @@ -59,8 +59,7 @@ private void validateQueryValue(Object queryValue, String key) { QueryOperator queryOperator = QueryOperator.fromValue(operator); if (queryOperator == QueryOperator.TYPE) { Object value = queryObject.get(operator); - if (value instanceof Collection) { - Collection values = (Collection) value; + if (value instanceof Collection values) { if (values.isEmpty()) { throw new FailedToParseException(key + " must match at least one type"); } @@ -134,8 +133,7 @@ private boolean checkMatch(Object queryValue, List keys, Object value) { return checkMatchesValue(queryValue, value); } - if (queryValue instanceof Document) { - Document query = (Document) queryValue; + if (queryValue instanceof Document query) { if (query.containsKey(QueryOperator.ALL.getValue())) { Object allQuery = query.get(QueryOperator.ALL.getValue()); return checkMatchesAllDocuments(allQuery, keys, value); @@ -171,10 +169,8 @@ private boolean checkMatch(Object queryValue, List keys, Object value) { return checkMatchesValue(queryValue, Missing.getInstance()); } - if (documentValue instanceof Collection) { - Collection documentValues = (Collection) documentValue; - if (queryValue instanceof Document) { - Document queryDocument = (Document) queryValue; + if (documentValue instanceof Collection documentValues) { + if (queryValue instanceof Document queryDocument) { boolean matches = checkMatchesAnyValue(queryDocument, keys, document, documentValues); if (matches) { return true; @@ -338,9 +334,7 @@ private boolean checkMatchesValue(Object queryValue, Object value, boolean requi } } - if (queryValue instanceof Document) { - Document queryObject = (Document) queryValue; - + if (queryValue instanceof Document queryObject) { if (queryObject.keySet().equals(Constants.REFERENCE_KEYS)) { if (value instanceof Document) { return matches((Document) value, queryObject); @@ -412,8 +406,7 @@ private boolean checkMatchesElemValues(Object queryValue, Object values) { } private boolean checkMatchesAnyValue(Object queryValue, Collection values) { - if (queryValue instanceof Document) { - Document queryDocument = (Document) queryValue; + if (queryValue instanceof Document queryDocument) { if (queryDocument.keySet().equals(Set.of(QueryOperator.ELEM_MATCH.getValue()))) { queryValue = queryDocument.get(QueryOperator.ELEM_MATCH.getValue()); } @@ -445,13 +438,11 @@ private boolean checkExpressionMatch(Object value, Object expressionValue, Strin case IN: Collection queriedObjects = (Collection) expressionValue; for (Object o : queriedObjects) { - if (o instanceof BsonRegularExpression && value instanceof String) { - BsonRegularExpression pattern = (BsonRegularExpression) o; - if (pattern.matcher((String) value).find()) { + if (o instanceof BsonRegularExpression pattern && value instanceof String strValue) { + if (pattern.matcher(strValue).find()) { return true; } - } else if (value instanceof Collection && !(o instanceof Collection)) { - Collection values = (Collection) value; + } else if (value instanceof Collection values && !(o instanceof Collection)) { return values.stream().anyMatch(v -> Utils.nullAwareEquals(o, v)); } else if (Utils.nullAwareEquals(o, value)) { return true; @@ -533,8 +524,7 @@ static boolean matchTypes(Object value, Object expressionValue) { return matchTypes(value, BsonType.forString((String) expressionValue)); } else if (expressionValue instanceof Number) { return matchTypes(value, BsonType.forNumber((Number) expressionValue)); - } else if (expressionValue instanceof Collection) { - Collection values = (Collection) expressionValue; + } else if (expressionValue instanceof Collection values) { for (Object type : values) { if (matchTypes(value, type)) { return true; diff --git a/core/src/main/java/de/bwaldvogel/mongo/backend/FieldUpdates.java b/core/src/main/java/de/bwaldvogel/mongo/backend/FieldUpdates.java index 0fcf83e7..bb0b4b3b 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/FieldUpdates.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/FieldUpdates.java @@ -140,8 +140,7 @@ private void handlePush(String key, Object changeValue) { Integer slice = null; Comparator comparator = null; int position = existingValue.size(); - if (changeValue instanceof Document && ((Document) changeValue).containsKey("$each")) { - Document pushDocument = (Document) changeValue; + if (changeValue instanceof Document pushDocument && pushDocument.containsKey("$each")) { for (Entry entry : pushDocument.entrySet()) { String modifier = entry.getKey(); switch (modifier) { @@ -228,8 +227,7 @@ private void handleAddToSet(String key, Object changeValue) { "Cannot apply $addToSet to non-array field. Field named '" + key + "' has non-array type " + describeType(value))); Collection pushValues = new ArrayList<>(); - if (changeValue instanceof Document && ((Document) changeValue).keySet().iterator().next().equals("$each")) { - Document addToSetDocument = (Document) changeValue; + if (changeValue instanceof Document addToSetDocument && addToSetDocument.keySet().iterator().next().equals("$each")) { for (String modifier : addToSetDocument.keySet()) { if (!modifier.equals("$each")) { throw new BadValueException("Found unexpected fields after $each in $addToSet: " + addToSetDocument.toString(true, "{ ", " }")); diff --git a/core/src/main/java/de/bwaldvogel/mongo/backend/Projection.java b/core/src/main/java/de/bwaldvogel/mongo/backend/Projection.java index 4c6f5a2c..f1ea9db0 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/Projection.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/Projection.java @@ -113,8 +113,7 @@ private static void projectField(Document document, Document newDocument, String if (object instanceof Document) { Document newsubDocument = (Document) newDocument.computeIfAbsent(mainKey, k -> new Document()); projectField((Document) object, newsubDocument, subKey, projectionValue); - } else if (object instanceof List) { - List values = (List) object; + } else if (object instanceof List values) { List newprojectedValues = (List) newDocument.computeIfAbsent(mainKey, k -> new ArrayList<>()); if ("$".equals(subKey) && !values.isEmpty()) { @@ -153,8 +152,7 @@ else if (!Utils.isTrue(projectionValue)) { } else { Object value = document.getOrMissing(key); - if (projectionValue instanceof Document) { - Document projectionDocument = (Document) projectionValue; + if (projectionValue instanceof Document projectionDocument) { if (projectionDocument.keySet().equals(Set.of(QueryOperator.ELEM_MATCH.getValue()))) { Document elemMatch = (Document) projectionDocument.get(QueryOperator.ELEM_MATCH.getValue()); projectElemMatch(newDocument, elemMatch, key, value); @@ -202,8 +200,7 @@ private static void projectSlice(Document newDocument, Object slice, String key, } else { toIndex = num; } - } else if (slice instanceof List) { - List sliceParams = (List) slice; + } else if (slice instanceof List sliceParams) { if (sliceParams.size() != 2) { throw new MongoServerError(28724, "First argument to $slice must be an array, but is of type: int"); } diff --git a/core/src/main/java/de/bwaldvogel/mongo/backend/Utils.java b/core/src/main/java/de/bwaldvogel/mongo/backend/Utils.java index 84552663..177f8a9a 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/Utils.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/Utils.java @@ -60,8 +60,7 @@ private static Object getSubdocumentValue(Document document, String key, boolean Object subObject = getFieldValueListSafe(document, mainKey); if (subObject instanceof Document) { return getSubdocumentValue((Document) subObject, subKey, handleCollections); - } else if (handleCollections && subObject instanceof Collection) { - Collection values = (Collection) subObject; + } else if (handleCollections && subObject instanceof Collection values) { List result = new ArrayList<>(); for (Object o : values) { if (o instanceof Document) { @@ -125,8 +124,7 @@ static Object normalizeValue(Object value) { result.put(entry.getKey(), normalizeValue(entry.getValue())); } return result; - } else if (value instanceof Collection) { - Collection collection = (Collection) value; + } else if (value instanceof Collection collection) { return collection.stream() .map(Utils::normalizeValue) .collect(Collectors.toList()); @@ -212,8 +210,7 @@ static Object getFieldValueListSafe(Object value, String field) throws IllegalAr throw new IllegalArgumentException("illegal field: " + field); } - if (value instanceof List) { - List list = (List) value; + if (value instanceof List list) { if (isNumeric(field)) { int pos = Integer.parseInt(field); if (pos >= 0 && pos < list.size()) { @@ -236,8 +233,7 @@ static Object getFieldValueListSafe(Object value, String field) throws IllegalAr } return values; } - } else if (value instanceof Document) { - Document document = (Document) value; + } else if (value instanceof Document document) { return document.getOrMissing(field); } else { return Missing.getInstance(); @@ -305,10 +301,9 @@ static boolean hasFieldValueListSafe(Object document, String field) throws Illeg throw new IllegalArgumentException("illegal field: " + field); } - if (document instanceof List) { + if (document instanceof List list) { if (isNumeric(field)) { int pos = Integer.parseInt(field); - List list = (List) document; return (pos >= 0 && pos < list.size()); } else { return false; @@ -344,14 +339,12 @@ private static Object setListSafe(Object document, String key, String previousKe } private static Object removeListSafe(Object value, String key) { - if (value instanceof Document) { - Document document = (Document) value; + if (value instanceof Document document) { if (document.containsKey(key)) { return document.remove(key); } return Missing.getInstance(); - } else if (value instanceof List) { - List values = ((List) value); + } else if (value instanceof List values) { if (isNumeric(key)) { int pos = Integer.parseInt(key); if (values.size() > pos) { @@ -367,8 +360,7 @@ private static Object removeListSafe(Object value, String key) { if (!(removedValue instanceof Missing)) { removedValues.add(removedValue); } - } else if (subValue instanceof List) { - List subValueList = (List) subValue; + } else if (subValue instanceof List subValueList) { for (Object subValueListValue : subValueList) { Object removedValue = removeListSafe(subValueListValue, key); if (!(removedValue instanceof Missing)) { @@ -427,8 +419,7 @@ public static void validateFieldNames(Document document) { } private static void validateFieldNames(Object value, String path) { - if (value instanceof Document) { - Document document = (Document) value; + if (value instanceof Document document) { for (Entry entry : document.entrySet()) { String key = entry.getKey(); String nextPath = path != null ? path + "." + key : key; @@ -436,8 +427,7 @@ private static void validateFieldNames(Object value, String path) { 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."); } } - } else if (value instanceof Collection) { - Collection values = (Collection) value; + } else if (value instanceof Collection values) { for (Object object : values) { validateFieldNames(object, path + "."); } diff --git a/core/src/main/java/de/bwaldvogel/mongo/backend/ValueComparator.java b/core/src/main/java/de/bwaldvogel/mongo/backend/ValueComparator.java index d707bc3a..c25a5cff 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/ValueComparator.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/ValueComparator.java @@ -134,9 +134,7 @@ private int doCompare(Object value1, Object value2) { return ((ObjectId) value1).compareTo((ObjectId) value2); } - if (value1 instanceof Decimal128 && value2 instanceof Decimal128) { - Decimal128 decimal1 = (Decimal128) value1; - Decimal128 decimal2 = (Decimal128) value2; + if (value1 instanceof Decimal128 decimal1 && value2 instanceof Decimal128 decimal2) { return decimal1.compareTo(decimal2); } diff --git a/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/Expression.java b/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/Expression.java index 47a116ed..404c5f07 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/Expression.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/Expression.java @@ -76,8 +76,7 @@ Object apply(List expressionValue, Document document) { throw new MongoServerError(16554, name() + " only supports numeric or date types, not " + describeType(number)); } - if (number instanceof Instant) { - Instant instant = (Instant) number; + if (number instanceof Instant instant) { number = instant.toEpochMilli(); returnDate = true; } @@ -179,8 +178,7 @@ Object apply(List expressionValues, Document document) { } Document result = new Document(); for (Object keyValueObject : (Collection) values) { - if (keyValueObject instanceof List) { - List keyValue = (List) keyValueObject; + if (keyValueObject instanceof List keyValue) { if (keyValue.size() != 2) { throw new FailedToOptimizePipelineError(40397, name() + " requires an array of size 2 arrays,found array of size: " + keyValue.size()); } @@ -191,8 +189,7 @@ Object apply(List expressionValues, Document document) { String key = (String) keyObject; Object value = keyValue.get(1); result.put(key, value); - } else if (keyValueObject instanceof Document) { - Document keyValue = (Document) keyValueObject; + } else if (keyValueObject instanceof Document keyValue) { if (keyValue.size() != 2) { throw new FailedToOptimizePipelineError(40392, name() + " requires an object keys of 'k' and 'v'. Found incorrect number of keys:" + keyValue.size()); } @@ -300,8 +297,7 @@ Object apply(List expressionValue, Document document) { final Object thenExpression; final Object elseExpression; - if (expressionValue.size() == 1 && CollectionUtils.getSingleElement(expressionValue) instanceof Document) { - Document condDocument = (Document) CollectionUtils.getSingleElement(expressionValue); + if (expressionValue.size() == 1 && CollectionUtils.getSingleElement(expressionValue) instanceof Document condDocument) { List requiredKeys = asList("if", "then", "else"); for (String requiredKey : requiredKeys) { if (!condDocument.containsKey(requiredKey)) { @@ -1528,8 +1524,7 @@ Boolean apply(List expressionValue, Document document) { Object value = requireSingleValue(expressionValue); if (Missing.isNullOrMissing(value)) { return null; - } else if (value instanceof Number) { - Number number = (Number) value; + } else if (value instanceof Number number) { return number.doubleValue() != 0.0; } else if (value instanceof Boolean) { return (Boolean) value; @@ -1553,8 +1548,7 @@ Instant apply(List expressionValue, Document document) { return Instant.ofEpochMilli(number.longValue()); } else if (value instanceof Instant) { return (Instant) value; - } else if (value instanceof String) { - String dateString = (String) value; + } else if (value instanceof String dateString) { try { return Instant.parse(dateString); } catch (DateTimeParseException e1) { @@ -1587,17 +1581,13 @@ Double apply(List expressionValue, Document document) { Object value = requireSingleValue(expressionValue); if (Missing.isNullOrMissing(value)) { return null; - } else if (value instanceof Number) { - Number number = (Number) value; + } else if (value instanceof Number number) { return number.doubleValue(); - } else if (value instanceof Boolean) { - Boolean booleanValue = (Boolean) value; + } else if (value instanceof Boolean booleanValue) { return booleanValue.booleanValue() ? 1.0 : 0.0; - } else if (value instanceof Instant) { - Instant instant = (Instant) value; + } else if (value instanceof Instant instant) { return (double) instant.toEpochMilli(); - } else if (value instanceof String) { - String string = (String) value; + } else if (value instanceof String string) { try { return Double.valueOf(string); } catch (NumberFormatException e) { @@ -1616,14 +1606,11 @@ Integer apply(List expressionValue, Document document) { Object value = requireSingleValue(expressionValue); if (Missing.isNullOrMissing(value)) { return null; - } else if (value instanceof Number) { - Number number = (Number) value; + } else if (value instanceof Number number) { return number.intValue(); - } else if (value instanceof Boolean) { - Boolean booleanValue = (Boolean) value; + } else if (value instanceof Boolean booleanValue) { return booleanValue.booleanValue() ? 1 : 0; - } else if (value instanceof String) { - String string = (String) value; + } else if (value instanceof String string) { try { return Integer.valueOf(string); } catch (NumberFormatException e) { @@ -1642,17 +1629,13 @@ Long apply(List expressionValue, Document document) { Object value = requireSingleValue(expressionValue); if (Missing.isNullOrMissing(value)) { return null; - } else if (value instanceof Number) { - Number number = (Number) value; + } else if (value instanceof Number number) { return number.longValue(); - } else if (value instanceof Boolean) { - Boolean booleanValue = (Boolean) value; + } else if (value instanceof Boolean booleanValue) { return booleanValue.booleanValue() ? 1L : 0L; - } else if (value instanceof Instant) { - Instant instant = (Instant) value; + } else if (value instanceof Instant instant) { return instant.toEpochMilli(); - } else if (value instanceof String) { - String string = (String) value; + } else if (value instanceof String string) { try { return Long.valueOf(string); } catch (NumberFormatException e) { @@ -1678,8 +1661,7 @@ ObjectId apply(List expressionValue, Document document) { Object value = requireSingleValue(expressionValue); if (Missing.isNullOrMissing(value)) { return null; - } else if (value instanceof String) { - String string = (String) value; + } else if (value instanceof String string) { try { return new ObjectId(string); } catch (RuntimeException e) { @@ -1750,8 +1732,7 @@ Object apply(Object expressionValue, Document document) { public static Object evaluateDocument(Object documentWithExpression, Document document) { Object evaluatedValue = evaluate(documentWithExpression, document); - if (evaluatedValue instanceof Document) { - Document projectedDocument = (Document) evaluatedValue; + if (evaluatedValue instanceof Document projectedDocument) { Document result = new Document(); for (Entry entry : projectedDocument.entrySet()) { String field = entry.getKey(); @@ -1792,8 +1773,7 @@ static Object evaluate(Object expression, Document document) { } } - if (variableValue instanceof String) { - String variableValueString = (String) variableValue; + if (variableValue instanceof String variableValueString) { if (variableValueString.startsWith("$")) { variableValue = evaluate(variableValue, document); } diff --git a/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/ExpressionTraits.java b/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/ExpressionTraits.java index 5f3e7db3..8ba1d0fc 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/ExpressionTraits.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/ExpressionTraits.java @@ -112,8 +112,7 @@ default TwoNumericParameters requireTwoNumericParameters(List value, int erro default ZonedDateTime getZonedDateTime(Object value, Document document) { ZoneId timezone = ZoneId.systemDefault(); - if (value instanceof Document) { - Document valueAsDocument = (Document) value; + if (value instanceof Document valueAsDocument) { if (!valueAsDocument.containsKey("date")) { throw new MongoServerError(40539, "missing 'date' argument to " + name() + ", provided: " + value); } diff --git a/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/MergeStage.java b/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/MergeStage.java index 8a198f75..6d891db0 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/MergeStage.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/MergeStage.java @@ -111,11 +111,9 @@ private static Supplier> getTargetCollectionSupplier(Database MongoDatabase database, Document paramsDocument) { Object into = paramsDocument.get("into"); - if (into instanceof String) { - String collectionName = (String) into; + if (into instanceof String collectionName) { return () -> resolveOrCreateCollection(database, collectionName); - } else if (into instanceof Document) { - Document intoDocument = (Document) into; + } else if (into instanceof Document intoDocument) { for (String intoKey : intoDocument.keySet()) { if (!intoKey.equals("db") && !intoKey.equals("coll")) { throw new MongoServerError(40415, "BSON field 'into." + intoKey + "' is an unknown field."); @@ -150,8 +148,7 @@ private Set getJoinFields(Document paramsDocument) { Object on = paramsDocument.getOrDefault("on", "_id"); if (on instanceof String) { return Set.of((String) on); - } else if (on instanceof Collection) { - Collection collection = (Collection) on; + } else if (on instanceof Collection collection) { if (collection.isEmpty()) { throw new MongoServerError(51187, "If explicitly specifying $merge 'on', must include at least one field"); } @@ -179,8 +176,7 @@ private WhenMatched getWhenMatched(Document paramsDocument) { } catch (IllegalArgumentException e) { throw new BadValueException("Enumeration value '" + whenMatched + "' for field 'whenMatched' is not a valid value."); } - } else if (whenMatched instanceof Collection) { - Collection pipeline = (Collection) whenMatched; + } else if (whenMatched instanceof Collection pipeline) { for (Object pipelineElement : pipeline) { if (!(pipelineElement instanceof Document)) { throw new TypeMismatchException("Each element of the 'pipeline' array must be an object"); diff --git a/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/UnwindStage.java b/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/UnwindStage.java index 94010ace..1d059621 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/UnwindStage.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/UnwindStage.java @@ -62,8 +62,7 @@ public Stream apply(Stream stream) { } else { return streamWithoutIndex(); } - } else if (values instanceof Collection) { - Collection collection = (Collection) values; + } else if (values instanceof Collection collection) { if (collection.isEmpty() && preserveNullAndEmptyArrays) { Document documentClone = document.cloneDeeply(); Utils.removeSubdocumentValue(documentClone, path); diff --git a/core/src/main/java/de/bwaldvogel/mongo/bson/Document.java b/core/src/main/java/de/bwaldvogel/mongo/bson/Document.java index 6454806b..c92c945a 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/bson/Document.java +++ b/core/src/main/java/de/bwaldvogel/mongo/bson/Document.java @@ -44,21 +44,18 @@ public Document cloneDeeply() { private static T cloneDeeply(T object) { if (object == null) { return null; - } else if (object instanceof Document) { - Document document = (Document) object; + } else if (object instanceof Document document) { Document clone = document.clone(); for (String key : document.keySet()) { clone.put(key, cloneDeeply(clone.get(key))); } return (T) clone; - } else if (object instanceof List) { - List list = (List) object; + } else if (object instanceof List list) { List result = list.stream() .map(Document::cloneDeeply) .collect(Collectors.toList()); return (T) result; - } else if (object instanceof Set) { - Set set = (Set) object; + } else if (object instanceof Set set) { Set result = set.stream() .map(Document::cloneDeeply) .collect(Collectors.toCollection(LinkedHashSet::new)); diff --git a/core/src/main/java/de/bwaldvogel/mongo/bson/Json.java b/core/src/main/java/de/bwaldvogel/mongo/bson/Json.java index 12e7c8b7..e65e9eda 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/bson/Json.java +++ b/core/src/main/java/de/bwaldvogel/mongo/bson/Json.java @@ -34,16 +34,13 @@ public static String toJsonValue(Object value, boolean compactKey, String jsonPr if (value instanceof String) { return "\"" + escapeJson((String) value) + "\""; } - if (value instanceof Document) { - Document document = (Document) value; + if (value instanceof Document document) { return document.toString(compactKey, jsonPrefix, jsonSuffix); } - if (value instanceof Instant) { - Instant instant = (Instant) value; + if (value instanceof Instant instant) { return toJsonValue(instant.toString()); } - if (value instanceof Collection) { - Collection collection = (Collection) value; + if (value instanceof Collection collection) { if (collection.isEmpty()) { return "[]"; } @@ -51,20 +48,17 @@ public static String toJsonValue(Object value, boolean compactKey, String jsonPr .map(v -> toJsonValue(v, compactKey, "{ ", " }")) .collect(Collectors.joining(", ", "[ ", " ]")); } - if (value instanceof ObjectId) { - ObjectId objectId = (ObjectId) value; + if (value instanceof ObjectId objectId) { return objectId.getHexData(); } - if (value instanceof BinData) { - BinData binData = (BinData) value; + if (value instanceof BinData binData) { return "BinData(0, " + toHex(binData.getData()) + ")"; } if (value instanceof LegacyUUID) { UUID uuid = ((LegacyUUID) value).getUuid(); return "BinData(3, " + toHex(uuid) + ")"; } - if (value instanceof UUID) { - UUID uuid = (UUID) value; + if (value instanceof UUID uuid) { return "UUID(\"" + uuid + "\")"; } return toJsonValue(value.toString()); diff --git a/core/src/main/java/de/bwaldvogel/mongo/wire/MongoDatabaseHandler.java b/core/src/main/java/de/bwaldvogel/mongo/wire/MongoDatabaseHandler.java index 579ec588..1c08443c 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/wire/MongoDatabaseHandler.java +++ b/core/src/main/java/de/bwaldvogel/mongo/wire/MongoDatabaseHandler.java @@ -108,8 +108,7 @@ private Document errorResponse(MongoServerException exception, Map ad Document obj = new Document(); obj.put("$err", exception.getMessageWithoutErrorCode()); obj.put("errmsg", exception.getMessageWithoutErrorCode()); - if (exception instanceof MongoServerError) { - MongoServerError error = (MongoServerError) exception; + if (exception instanceof MongoServerError error) { obj.put("code", error.getCode()); obj.putIfNotNull("codeName", error.getCodeName()); } diff --git a/core/src/test/java/de/bwaldvogel/mongo/TestUtils.java b/core/src/test/java/de/bwaldvogel/mongo/TestUtils.java index 39c0f945..138af046 100644 --- a/core/src/test/java/de/bwaldvogel/mongo/TestUtils.java +++ b/core/src/test/java/de/bwaldvogel/mongo/TestUtils.java @@ -34,8 +34,7 @@ private static Object convertValue(Object value) { Map mapValue = (Map) value; return toDocument(mapValue); } - if (value instanceof Collection) { - Collection collection = (Collection) value; + if (value instanceof Collection collection) { return collection.stream() .map(TestUtils::convertValue) .collect(Collectors.toList());