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 cdf1d413d..a40483895 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/DefaultQueryMatcher.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/DefaultQueryMatcher.java @@ -336,8 +336,8 @@ private boolean checkMatchesValue(Object queryValue, Object value, boolean requi if (queryValue instanceof Document queryObject) { if (queryObject.keySet().equals(Constants.REFERENCE_KEYS)) { - if (value instanceof Document) { - return matches((Document) value, queryObject); + if (value instanceof Document documentValue) { + return matches(documentValue, queryObject); } else { return false; } @@ -520,10 +520,10 @@ static boolean matchTypes(Object value, Object expressionValue) { .map(BsonType::getAlias) .collect(Collectors.toList()); return matchTypes(value, types); - } else if (expressionValue instanceof String) { - return matchTypes(value, BsonType.forString((String) expressionValue)); - } else if (expressionValue instanceof Number) { - return matchTypes(value, BsonType.forNumber((Number) expressionValue)); + } else if (expressionValue instanceof String stringValue) { + return matchTypes(value, BsonType.forString(stringValue)); + } else if (expressionValue instanceof Number numberValue) { + return matchTypes(value, BsonType.forNumber(numberValue)); } else if (expressionValue instanceof Collection values) { for (Object type : values) { if (matchTypes(value, type)) { 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 bb0b4b3ba..21980f6a8 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/FieldUpdates.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/FieldUpdates.java @@ -158,19 +158,19 @@ private void handlePush(String key, Object changeValue) { break; case "$sort": Object sortValue = Utils.normalizeValue(entry.getValue()); - if (sortValue instanceof Number) { - Number sortOrder = Utils.normalizeNumber((Number) sortValue); + if (sortValue instanceof Number sortNumber) { + Number sortOrder = Utils.normalizeNumber(sortNumber); if (sortOrder.equals(1)) { comparator = ValueComparator.asc(); } else if (sortOrder.equals(-1)) { comparator = ValueComparator.desc(); } - } else if (sortValue instanceof Document) { + } else if (sortValue instanceof Document sortDocument) { ValueComparator valueComparator = ValueComparator.asc(); - DocumentComparator documentComparator = new DocumentComparator((Document) sortValue); + DocumentComparator documentComparator = new DocumentComparator(sortDocument); comparator = (o1, o2) -> { - if (o1 instanceof Document && o2 instanceof Document) { - return documentComparator.compare((Document) o1, (Document) o2); + if (o1 instanceof Document doc1 && o2 instanceof Document doc2) { + return documentComparator.compare(doc1, doc2); } else if (o1 instanceof Document || o2 instanceof Document) { return valueComparator.compare(o1, o2); } else { 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 177f8a9ae..b464c7dc3 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/Utils.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/Utils.java @@ -58,13 +58,13 @@ private static Object getSubdocumentValue(Document document, String key, boolean String subKey = joinTail(pathFragments); Assert.doesNotStartWith(subKey, "$."); Object subObject = getFieldValueListSafe(document, mainKey); - if (subObject instanceof Document) { - return getSubdocumentValue((Document) subObject, subKey, handleCollections); + if (subObject instanceof Document subDocument) { + return getSubdocumentValue(subDocument, subKey, handleCollections); } else if (handleCollections && subObject instanceof Collection values) { List result = new ArrayList<>(); for (Object o : values) { - if (o instanceof Document) { - Object subdocumentValue = getSubdocumentValue((Document) o, subKey, handleCollections); + if (o instanceof Document doc) { + Object subdocumentValue = getSubdocumentValue(doc, subKey, handleCollections); if (subdocumentValue instanceof Collection) { result.addAll((Collection) subdocumentValue); } else { @@ -93,12 +93,12 @@ public static boolean isTrue(Object value) { return false; } - if (value instanceof Boolean) { - return ((Boolean) value).booleanValue(); + if (value instanceof Boolean bool) { + return bool.booleanValue(); } - if (value instanceof Number) { - return ((Number) value).doubleValue() != 0.0; + if (value instanceof Number number) { + return number.doubleValue() != 0.0; } return true; @@ -110,8 +110,8 @@ static Object normalizeValue(Object value) { } if (value instanceof Long && cannotBeRepresentedAsDouble((Long) value)) { return value; - } else if (value instanceof Number) { - double doubleValue = ((Number) value).doubleValue(); + } else if (value instanceof Number number) { + double doubleValue = number.doubleValue(); if (doubleValue == -0.0) { doubleValue = 0.0; } @@ -221,8 +221,8 @@ static Object getFieldValueListSafe(Object value, String field) throws IllegalAr } else { List values = new ArrayList<>(); for (Object subValue : list) { - if (subValue instanceof Document) { - Object subDocumentValue = ((Document) subValue).getOrMissing(field); + if (subValue instanceof Document subDocument) { + Object subDocumentValue = subDocument.getOrMissing(field); if (!(subDocumentValue instanceof Missing)) { values.add(subDocumentValue); } @@ -308,8 +308,8 @@ static boolean hasFieldValueListSafe(Object document, String field) throws Illeg } else { return false; } - } else if (document instanceof Document) { - return ((Document) document).containsKey(field); + } else if (document instanceof Document doc) { + return doc.containsKey(field); } throw new IllegalArgumentException("illegal document: " + document); 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 d5b813990..1bef87fa9 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 @@ -284,10 +284,10 @@ Object apply(List expressionValue, Document document) { Object apply(Object expressionValue, Document document) { // document values need to be evaluated lazily List values = new ArrayList<>(); - if (!(expressionValue instanceof Collection)) { - values.add(expressionValue); + if (expressionValue instanceof Collection collectionValue) { + values.addAll(collectionValue); } else { - values.addAll(((Collection) expressionValue)); + values.add(expressionValue); } return apply(values, document); } @@ -370,10 +370,10 @@ Object apply(List expression, Document document) { private BsonType getBsonType(Object to) { try { - if (to instanceof String) { - return BsonType.forString((String) to); - } else if (to instanceof Integer) { - return BsonType.forNumber((Integer) to); + if (to instanceof String toString) { + return BsonType.forString(toString); + } else if (to instanceof Integer toNumber) { + return BsonType.forNumber(toNumber); } else if (to instanceof Number) { throw new IllegalArgumentException("In $convert, numeric 'to' argument is not an integer"); } else { @@ -388,33 +388,16 @@ private BsonType getBsonType(Object to) { private Object convert(Object inputValue, BsonType bsonType, Document document, Document convertDocument) { try { - switch (bsonType) { - case DOUBLE: - return $toDouble.apply(inputValue, document); - case STRING: - return $toString.apply(inputValue, document); - case OBJECT_ID: - return $toObjectId.apply(inputValue, document); - case BOOL: - return $toBool.apply(inputValue, document); - case DATE: - return $toDate.apply(inputValue, document); - case INT: - return $toInt.apply(inputValue, document); - case LONG: - return $toLong.apply(inputValue, document); - case OBJECT: - case ARRAY: - case BIN_DATA: - case NULL: - case REGEX: - case TIMESTAMP: - case DECIMAL128: - case MIN_KEY: - case MAX_KEY: - default: - throw new UnsupportedOperationException("Unsupported conversion to type " + bsonType); - } + return switch (bsonType) { + case DOUBLE -> $toDouble.apply(inputValue, document); + case STRING -> $toString.apply(inputValue, document); + case OBJECT_ID -> $toObjectId.apply(inputValue, document); + case BOOL -> $toBool.apply(inputValue, document); + case DATE -> $toDate.apply(inputValue, document); + case INT -> $toInt.apply(inputValue, document); + case LONG -> $toLong.apply(inputValue, document); + default -> throw new UnsupportedOperationException("Unsupported conversion to type " + bsonType); + }; } catch (MongoServerError e) { if (e.hasCode(ErrorCode.ConversionFailure)) { if (convertDocument.containsKey("onError")) { @@ -1396,29 +1379,29 @@ Object apply(List expressionValue, Document document) { @Override Object apply(List expressionValue, Document document) { TwoParameters parameters = requireTwoParameters(expressionValue); - Object one = parameters.getFirst(); - Object other = parameters.getSecond(); + Object first = parameters.getFirst(); + Object second = parameters.getSecond(); - if (isNullOrMissing(one) || isNullOrMissing(other)) { + if (isNullOrMissing(first) || isNullOrMissing(second)) { return null; } - if (one instanceof Number && other instanceof Number) { - return NumericUtils.subtractNumbers((Number) one, (Number) other); + if (first instanceof Number firstNumber && second instanceof Number secondNumber) { + return NumericUtils.subtractNumbers(firstNumber, secondNumber); } - if (one instanceof Instant) { + if (first instanceof Instant firstInstant) { // subtract two instants (returns the difference in milliseconds) - if (other instanceof Instant) { - return ((Instant) one).toEpochMilli() - ((Instant) other).toEpochMilli(); + if (second instanceof Instant secondInstant) { + return firstInstant.toEpochMilli() - secondInstant.toEpochMilli(); } // subtract milliseconds from instant - if (other instanceof Number) { - return Instant.ofEpochMilli(((Instant) one).toEpochMilli() - ((Number) other).longValue()); + if (second instanceof Number secondNumber) { + return Instant.ofEpochMilli(firstInstant.toEpochMilli() - secondNumber.longValue()); } } - throw new MongoServerError(16556, "cant " + name() + " a " + describeType(one) + " from a " + describeType(other)); + throw new MongoServerError(16556, "cant " + name() + " a " + describeType(first) + " from a " + describeType(second)); } }, @@ -1433,8 +1416,8 @@ Object apply(List expressionValue, Document document) { } Number sum = 0; for (Object value : expressionValue) { - if (value instanceof Number) { - sum = NumericUtils.addNumbers(sum, (Number) value); + if (value instanceof Number number) { + sum = NumericUtils.addNumbers(sum, number); } } return sum; @@ -1761,11 +1744,11 @@ public static Object evaluateDocument(Object documentWithExpression, Document do } static Object evaluate(Object expression, Document document) { - if (expression instanceof String && ((String) expression).startsWith("$")) { + if (expression instanceof String expressionString && expressionString.startsWith("$")) { if (KEYWORD_EXPRESSIONS.contains(expression)) { return expression; } - String value = ((String) expression).substring(1); + String value = expressionString.substring(1); if (value.startsWith("$")) { final String variableName; if (value.contains(".")) { @@ -1799,8 +1782,8 @@ static Object evaluate(Object expression, Document document) { } } return Utils.getSubdocumentValueCollectionAware(document, value); - } else if (expression instanceof Document) { - return evaluateDocumentExpression((Document) expression, document); + } else if (expression instanceof Document expressionDoc) { + return evaluateDocumentExpression(expressionDoc, document); } else { return expression; } diff --git a/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/accumulator/Accumulator.java b/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/accumulator/Accumulator.java index a3ac34c61..95c6acfa9 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/accumulator/Accumulator.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/accumulator/Accumulator.java @@ -33,24 +33,16 @@ public static Map> parse(Document configuration) { }); String groupOperator = aggregation.getKey(); Object expression = aggregation.getValue(); - if (groupOperator.equals("$sum")) { - accumulators.put(field, () -> new SumAccumulator(field, expression)); - } else if (groupOperator.equals("$min")) { - accumulators.put(field, () -> new MinAccumulator(field, expression)); - } else if (groupOperator.equals("$max")) { - accumulators.put(field, () -> new MaxAccumulator(field, expression)); - } else if (groupOperator.equals("$avg")) { - accumulators.put(field, () -> new AvgAccumulator(field, expression)); - } else if (groupOperator.equals("$addToSet")) { - accumulators.put(field, () -> new AddToSetAccumulator(field, expression)); - } else if (groupOperator.equals("$push")) { - accumulators.put(field, () -> new PushAccumulator(field, expression)); - } else if (groupOperator.equals("$first")) { - accumulators.put(field, () -> new FirstAccumulator(field, expression)); - } else if (groupOperator.equals("$last")) { - accumulators.put(field, () -> new LastAccumulator(field, expression)); - } else { - throw new MongoServerError(15952, "unknown group operator '" + groupOperator + "'"); + switch (groupOperator) { + case "$sum" -> accumulators.put(field, () -> new SumAccumulator(field, expression)); + case "$min" -> accumulators.put(field, () -> new MinAccumulator(field, expression)); + case "$max" -> accumulators.put(field, () -> new MaxAccumulator(field, expression)); + case "$avg" -> accumulators.put(field, () -> new AvgAccumulator(field, expression)); + case "$addToSet" -> accumulators.put(field, () -> new AddToSetAccumulator(field, expression)); + case "$push" -> accumulators.put(field, () -> new PushAccumulator(field, expression)); + case "$first" -> accumulators.put(field, () -> new FirstAccumulator(field, expression)); + case "$last" -> accumulators.put(field, () -> new LastAccumulator(field, expression)); + default -> throw new MongoServerError(15952, "unknown group operator '" + groupOperator + "'"); } } return accumulators; diff --git a/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/accumulator/AvgAccumulator.java b/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/accumulator/AvgAccumulator.java index 5797510f7..f22f9886e 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/accumulator/AvgAccumulator.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/accumulator/AvgAccumulator.java @@ -13,8 +13,8 @@ public AvgAccumulator(String field, Object expression) { @Override public void aggregate(Object value) { - if (value instanceof Number) { - sum = NumericUtils.addNumbers(sum, (Number) value); + if (value instanceof Number number) { + sum = NumericUtils.addNumbers(sum, number); count++; } } diff --git a/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/accumulator/SumAccumulator.java b/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/accumulator/SumAccumulator.java index 6db300ad8..a0df6883d 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/accumulator/SumAccumulator.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/accumulator/SumAccumulator.java @@ -12,8 +12,8 @@ public SumAccumulator(String field, Object expression) { @Override public void aggregate(Object value) { - if (value instanceof Number) { - sum = NumericUtils.addNumbers(sum, (Number) value); + if (value instanceof Number number) { + sum = NumericUtils.addNumbers(sum, number); } } diff --git a/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/AbstractLookupStage.java b/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/AbstractLookupStage.java index 7a875ae0c..2dea52109 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/AbstractLookupStage.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/AbstractLookupStage.java @@ -22,8 +22,8 @@ String readStringConfigurationProperty(Document configuration, String name) { if (value == null) { throw new FailedToParseException("missing '" + name + "' option to " + name() + " stage specification: " + configuration); } - if (value instanceof String) { - return (String) value; + if (value instanceof String string) { + return string; } throw new FailedToParseException("'" + name + "' option to " + name() + " must be a string, but was type " + Utils.describeType(value)); } @@ -33,8 +33,8 @@ Document readOptionalDocumentArgument(Document configuration, String name) { if (value == null) { return new Document(); } - if (value instanceof Document) { - return (Document) value; + if (value instanceof Document document) { + return document; } throw new FailedToParseException(name() + " argument '" + name + ": " + Json.toJsonValue(value) + "' must be an object, is type " + Utils.describeType(value)); } diff --git a/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/GraphLookupStage.java b/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/GraphLookupStage.java index 178739ea3..40c88441f 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/GraphLookupStage.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/GraphLookupStage.java @@ -65,8 +65,8 @@ Integer readOptionalIntegerConfigurationProperty(Document configuration, String if (value == null) { return null; } - if (value instanceof Integer) { - return (Integer) value; + if (value instanceof Integer integer) { + return integer; } throw new FailedToParseException("'" + name + "' option to \" + stageName + \" must be a integer, but was type " + Utils.describeType(value)); } @@ -76,8 +76,8 @@ String readOptionalStringConfigurationProperty(Document configuration, String na if (value == null) { return null; } - if (value instanceof String) { - return (String) value; + if (value instanceof String string) { + return string; } throw new FailedToParseException("'" + name + "' option to \" + stageName + \" must be a string, but was type " + Utils.describeType(value)); } @@ -113,8 +113,8 @@ private List findLinkedDocuments(long depth, final List link return linked; } - if (value instanceof List) { - return ((List) value).stream() + if (value instanceof List list) { + return list.stream() .flatMap(item -> findLinkedDocuments(depth + 1, linked, item).stream()) .collect(toList()); } 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 6d891db00..eec4aa5b6 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 @@ -93,18 +93,20 @@ private Map getLet(Document paramsDocument) { Object let = paramsDocument.get("let"); if (let == null) { return new Document("$new", "$$ROOT"); - } else if (!(let instanceof Document)) { + } + + if (!(let instanceof Document)) { throw new TypeMismatchException("BSON field '$merge.let' is the wrong type '" + Utils.describeType(let) + "', expected type 'object'"); - } else { - Map variables = new LinkedHashMap<>(); - for (Map.Entry entry : ((Document) let).entrySet()) { - if (entry.getKey().equals("new") && !entry.getValue().equals("$$ROOT")) { - throw new MongoServerError(51273, "'let' may not define a value for the reserved 'new' variable other than '$$ROOT'"); - } - variables.put("$" + entry.getKey(), entry.getValue()); + } + + Map variables = new LinkedHashMap<>(); + for (Map.Entry entry : ((Document) let).entrySet()) { + if (entry.getKey().equals("new") && !entry.getValue().equals("$$ROOT")) { + throw new MongoServerError(51273, "'let' may not define a value for the reserved 'new' variable other than '$$ROOT'"); } - return variables; + variables.put("$" + entry.getKey(), entry.getValue()); } + return variables; } private static Supplier> getTargetCollectionSupplier(DatabaseResolver databaseResolver, @@ -146,9 +148,10 @@ private boolean matchesJoinFields(Index index) { 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) { + if (on instanceof String string) { + return Set.of(string); + } + if (on instanceof Collection collection) { if (collection.isEmpty()) { throw new MongoServerError(51187, "If explicitly specifying $merge 'on', must include at least one field"); } @@ -170,9 +173,9 @@ private Set getJoinFields(Document paramsDocument) { private WhenMatched getWhenMatched(Document paramsDocument) { Object whenMatched = paramsDocument.getOrDefault("whenMatched", WhenMatched.merge.name()); - if (whenMatched instanceof String) { + if (whenMatched instanceof String matched) { try { - return WhenMatched.valueOf((String) whenMatched); + return WhenMatched.valueOf(matched); } catch (IllegalArgumentException e) { throw new BadValueException("Enumeration value '" + whenMatched + "' for field 'whenMatched' is not a valid value."); } 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 1d059621f..b84fc5ecd 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 @@ -24,8 +24,7 @@ public UnwindStage(Object input) { } final String fieldPath; - if (input instanceof Document) { - Document inputDocument = (Document) input; + if (input instanceof Document inputDocument) { if (!inputDocument.containsKey("path")) { throw new MongoServerError(28812, "no path specified to $unwind stage"); } diff --git a/core/src/main/java/de/bwaldvogel/mongo/bson/BsonRegularExpression.java b/core/src/main/java/de/bwaldvogel/mongo/bson/BsonRegularExpression.java index b90f43f2b..09797d8fa 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/bson/BsonRegularExpression.java +++ b/core/src/main/java/de/bwaldvogel/mongo/bson/BsonRegularExpression.java @@ -46,8 +46,8 @@ private static BsonRegularExpression fromDocument(Document queryObject) { } public static boolean isRegularExpression(Object object) { - if (object instanceof Document) { - return ((Document) object).containsKey(REGEX); + if (object instanceof Document document) { + return document.containsKey(REGEX); } else { return object instanceof BsonRegularExpression; } 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 e65e9eda3..0be9f48d5 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/bson/Json.java +++ b/core/src/main/java/de/bwaldvogel/mongo/bson/Json.java @@ -31,8 +31,8 @@ public static String toJsonValue(Object value, boolean compactKey, String jsonPr if (value instanceof Boolean) { return value.toString(); } - if (value instanceof String) { - return "\"" + escapeJson((String) value) + "\""; + if (value instanceof String string) { + return "\"" + escapeJson(string) + "\""; } if (value instanceof Document document) { return document.toString(compactKey, jsonPrefix, jsonSuffix); @@ -54,8 +54,8 @@ public static String toJsonValue(Object value, boolean compactKey, String jsonPr if (value instanceof BinData binData) { return "BinData(0, " + toHex(binData.getData()) + ")"; } - if (value instanceof LegacyUUID) { - UUID uuid = ((LegacyUUID) value).getUuid(); + if (value instanceof LegacyUUID legacyUUID) { + UUID uuid = legacyUUID.getUuid(); return "BinData(3, " + toHex(uuid) + ")"; } if (value instanceof UUID uuid) { diff --git a/core/src/main/java/de/bwaldvogel/mongo/oplog/OperationType.java b/core/src/main/java/de/bwaldvogel/mongo/oplog/OperationType.java index 983f086d3..97660a7d4 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/oplog/OperationType.java +++ b/core/src/main/java/de/bwaldvogel/mongo/oplog/OperationType.java @@ -40,18 +40,12 @@ static OperationType fromCode(String code) { } String getDescription() { - switch (this) { - case DELETE: - return "delete"; - case INSERT: - return "insert"; - case UPDATE: - return "update"; - case COMMAND: - return "command"; - case INVALIDATE: - return "invalidate"; - } - return null; + return switch (this) { + case DELETE -> "delete"; + case INSERT -> "insert"; + case UPDATE -> "update"; + case COMMAND -> "command"; + case INVALIDATE -> "invalidate"; + }; } } 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 1c08443c4..227814e05 100644 --- a/core/src/main/java/de/bwaldvogel/mongo/wire/MongoDatabaseHandler.java +++ b/core/src/main/java/de/bwaldvogel/mongo/wire/MongoDatabaseHandler.java @@ -55,11 +55,11 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { @Override protected void channelRead0(ChannelHandlerContext ctx, ClientRequest object) { - if (object instanceof MongoQuery) { - MongoReply mongoReply = handleQuery((MongoQuery) object); + if (object instanceof MongoQuery mongoQuery) { + MongoReply mongoReply = handleQuery(mongoQuery); ctx.channel().writeAndFlush(mongoReply); - } else if (object instanceof MongoMessage) { - MongoMessage response = handleMessage((MongoMessage) object); + } else if (object instanceof MongoMessage mongoMessage) { + MongoMessage response = handleMessage(mongoMessage); ctx.channel().writeAndFlush(response); } else { throw new MongoServerException("unknown message: " + object);