Skip to content

Commit ec85489

Browse files
author
jmarkerink
committed
feat: adopt switch expression
1 parent 7c5dc5c commit ec85489

File tree

3 files changed

+27
-58
lines changed

3 files changed

+27
-58
lines changed

core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/Expression.java

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -388,33 +388,16 @@ private BsonType getBsonType(Object to) {
388388

389389
private Object convert(Object inputValue, BsonType bsonType, Document document, Document convertDocument) {
390390
try {
391-
switch (bsonType) {
392-
case DOUBLE:
393-
return $toDouble.apply(inputValue, document);
394-
case STRING:
395-
return $toString.apply(inputValue, document);
396-
case OBJECT_ID:
397-
return $toObjectId.apply(inputValue, document);
398-
case BOOL:
399-
return $toBool.apply(inputValue, document);
400-
case DATE:
401-
return $toDate.apply(inputValue, document);
402-
case INT:
403-
return $toInt.apply(inputValue, document);
404-
case LONG:
405-
return $toLong.apply(inputValue, document);
406-
case OBJECT:
407-
case ARRAY:
408-
case BIN_DATA:
409-
case NULL:
410-
case REGEX:
411-
case TIMESTAMP:
412-
case DECIMAL128:
413-
case MIN_KEY:
414-
case MAX_KEY:
415-
default:
416-
throw new UnsupportedOperationException("Unsupported conversion to type " + bsonType);
417-
}
391+
return switch (bsonType) {
392+
case DOUBLE -> $toDouble.apply(inputValue, document);
393+
case STRING -> $toString.apply(inputValue, document);
394+
case OBJECT_ID -> $toObjectId.apply(inputValue, document);
395+
case BOOL -> $toBool.apply(inputValue, document);
396+
case DATE -> $toDate.apply(inputValue, document);
397+
case INT -> $toInt.apply(inputValue, document);
398+
case LONG -> $toLong.apply(inputValue, document);
399+
default -> throw new UnsupportedOperationException("Unsupported conversion to type " + bsonType);
400+
};
418401
} catch (MongoServerError e) {
419402
if (e.hasCode(ErrorCode.ConversionFailure)) {
420403
if (convertDocument.containsKey("onError")) {

core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/accumulator/Accumulator.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,16 @@ public static Map<String, Supplier<Accumulator>> parse(Document configuration) {
3333
});
3434
String groupOperator = aggregation.getKey();
3535
Object expression = aggregation.getValue();
36-
if (groupOperator.equals("$sum")) {
37-
accumulators.put(field, () -> new SumAccumulator(field, expression));
38-
} else if (groupOperator.equals("$min")) {
39-
accumulators.put(field, () -> new MinAccumulator(field, expression));
40-
} else if (groupOperator.equals("$max")) {
41-
accumulators.put(field, () -> new MaxAccumulator(field, expression));
42-
} else if (groupOperator.equals("$avg")) {
43-
accumulators.put(field, () -> new AvgAccumulator(field, expression));
44-
} else if (groupOperator.equals("$addToSet")) {
45-
accumulators.put(field, () -> new AddToSetAccumulator(field, expression));
46-
} else if (groupOperator.equals("$push")) {
47-
accumulators.put(field, () -> new PushAccumulator(field, expression));
48-
} else if (groupOperator.equals("$first")) {
49-
accumulators.put(field, () -> new FirstAccumulator(field, expression));
50-
} else if (groupOperator.equals("$last")) {
51-
accumulators.put(field, () -> new LastAccumulator(field, expression));
52-
} else {
53-
throw new MongoServerError(15952, "unknown group operator '" + groupOperator + "'");
36+
switch (groupOperator) {
37+
case "$sum" -> accumulators.put(field, () -> new SumAccumulator(field, expression));
38+
case "$min" -> accumulators.put(field, () -> new MinAccumulator(field, expression));
39+
case "$max" -> accumulators.put(field, () -> new MaxAccumulator(field, expression));
40+
case "$avg" -> accumulators.put(field, () -> new AvgAccumulator(field, expression));
41+
case "$addToSet" -> accumulators.put(field, () -> new AddToSetAccumulator(field, expression));
42+
case "$push" -> accumulators.put(field, () -> new PushAccumulator(field, expression));
43+
case "$first" -> accumulators.put(field, () -> new FirstAccumulator(field, expression));
44+
case "$last" -> accumulators.put(field, () -> new LastAccumulator(field, expression));
45+
default -> throw new MongoServerError(15952, "unknown group operator '" + groupOperator + "'");
5446
}
5547
}
5648
return accumulators;

core/src/main/java/de/bwaldvogel/mongo/oplog/OperationType.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,12 @@ static OperationType fromCode(String code) {
4040
}
4141

4242
String getDescription() {
43-
switch (this) {
44-
case DELETE:
45-
return "delete";
46-
case INSERT:
47-
return "insert";
48-
case UPDATE:
49-
return "update";
50-
case COMMAND:
51-
return "command";
52-
case INVALIDATE:
53-
return "invalidate";
54-
}
55-
return null;
43+
return switch (this) {
44+
case DELETE -> "delete";
45+
case INSERT -> "insert";
46+
case UPDATE -> "update";
47+
case COMMAND -> "command";
48+
case INVALIDATE -> "invalidate";
49+
};
5650
}
5751
}

0 commit comments

Comments
 (0)