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 @@ -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"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,10 @@ public synchronized Iterable<P> 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));
Expand All @@ -215,8 +214,7 @@ public synchronized Iterable<P> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ static <T> List<List<T>> multiplyWithOtherElements(Collection<T> allValues, Coll
}

static <T> T getElementAtPosition(Iterable<T> iterable, int pos) {
if (iterable instanceof List) {
List<T> list = (List<T>) iterable;
if (iterable instanceof List<T> list) {
return list.get(pos);
} else {
Iterator<T> iterator = iterable.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -134,8 +133,7 @@ private boolean checkMatch(Object queryValue, List<String> 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);
Expand Down Expand Up @@ -171,10 +169,8 @@ private boolean checkMatch(Object queryValue, List<String> 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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ private void handlePush(String key, Object changeValue) {
Integer slice = null;
Comparator<Object> 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<String, Object> entry : pushDocument.entrySet()) {
String modifier = entry.getKey();
switch (modifier) {
Expand Down Expand Up @@ -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<Object> 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, "{ ", " }"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> newprojectedValues = (List<Object>) newDocument.computeIfAbsent(mainKey, k -> new ArrayList<>());

if ("$".equals(subKey) && !values.isEmpty()) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
}
Expand Down
30 changes: 10 additions & 20 deletions core/src/main/java/de/bwaldvogel/mongo/backend/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> result = new ArrayList<>();
for (Object o : values) {
if (o instanceof Document) {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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()) {
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -427,17 +419,15 @@ 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<String, Object> entry : document.entrySet()) {
String key = entry.getKey();
String nextPath = path != null ? path + "." + key : key;
if (key.startsWith("$") && !Constants.REFERENCE_KEYS.contains(key)) {
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 + ".");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Loading