Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)) {
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/java/de/bwaldvogel/mongo/backend/FieldUpdates.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 14 additions & 14 deletions core/src/main/java/de/bwaldvogel/mongo/backend/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> 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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -221,8 +221,8 @@ static Object getFieldValueListSafe(Object value, String field) throws IllegalAr
} else {
List<Object> 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);
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,10 @@ Object apply(List<?> expressionValue, Document document) {
Object apply(Object expressionValue, Document document) {
// document values need to be evaluated lazily
List<Object> 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);
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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")) {
Expand Down Expand Up @@ -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));
}
},

Expand All @@ -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;
Expand Down Expand Up @@ -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(".")) {
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,16 @@ public static Map<String, Supplier<Accumulator>> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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));
}
Expand Down Expand Up @@ -113,8 +113,8 @@ private List<Document> findLinkedDocuments(long depth, final List<Document> 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());
}
Expand Down
Loading