Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Replace Traditional Switch Statements with Switch Expressions #1671

Closed
Closed
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 @@ -80,30 +80,19 @@ protected void doEndValueRange(Filter.Value listValue, StringBuilder context) {
}

private String getOperationSymbol(Expression exp) {
switch (exp.type()) {
case AND:
return " and ";
case OR:
return " or ";
case EQ:
return " eq ";
case NE:
return " ne ";
case LT:
return " lt ";
case LTE:
return " le ";
case GT:
return " gt ";
case GTE:
return " ge ";
case IN:
return " search.in";
case NIN:
return " not search.in";
default:
throw new RuntimeException("Not supported expression type: " + exp.type());
}
return switch (exp.type()) {
case AND -> " and ";
case OR -> " or ";
case EQ -> " eq ";
case NE -> " ne ";
case LT -> " lt ";
case LTE -> " le ";
case GT -> " gt ";
case GTE -> " ge ";
case IN -> " search.in";
case NIN -> " not search.in";
default -> throw new RuntimeException("Not supported expression type: " + exp.type());
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,19 @@ protected void doExpression(Expression exp, StringBuilder context) {
}

private String getOperationSymbol(Expression exp) {
switch (exp.type()) {
case AND:
return " && ";
case OR:
return " || ";
case EQ:
return " == ";
case NE:
return " != ";
case LT:
return " < ";
case LTE:
return " <= ";
case GT:
return " > ";
case GTE:
return " >= ";
case IN:
return " in ";
case NIN:
return " nin ";
default:
throw new RuntimeException("Not supported expression type:" + exp.type());
}
return switch (exp.type()) {
case AND -> " && ";
case OR -> " || ";
case EQ -> " == ";
case NE -> " != ";
case LT -> " < ";
case LTE -> " <= ";
case GT -> " > ";
case GTE -> " >= ";
case IN -> " in ";
case NIN -> " nin ";
default -> throw new RuntimeException("Not supported expression type:" + exp.type());
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,33 +140,24 @@ private Object stringValue(Expression expression, Value value) {
}

private String tagValueDelimiter(Expression expression) {
switch (expression.type()) {
case IN:
return " | ";
case EQ:
return " ";
default:
throw new UnsupportedOperationException(
MessageFormat.format("Tag operand {0} not supported", expression.type()));
}
return switch (expression.type()) {
case IN -> " | ";
case EQ -> " ";
default -> throw new UnsupportedOperationException(
MessageFormat.format("Tag operand {0} not supported", expression.type()));
};
}

private Numeric numeric(Expression expression, Value value) {
switch (expression.type()) {
case EQ:
return new Numeric(inclusive(value), inclusive(value));
case GT:
return new Numeric(exclusive(value), POSITIVE_INFINITY);
case GTE:
return new Numeric(inclusive(value), POSITIVE_INFINITY);
case LT:
return new Numeric(NEGATIVE_INFINITY, exclusive(value));
case LTE:
return new Numeric(NEGATIVE_INFINITY, inclusive(value));
default:
throw new UnsupportedOperationException(MessageFormat
.format("Expression type {0} not supported for numeric fields", expression.type()));
}
return switch (expression.type()) {
case EQ -> new Numeric(inclusive(value), inclusive(value));
case GT -> new Numeric(exclusive(value), POSITIVE_INFINITY);
case GTE -> new Numeric(inclusive(value), POSITIVE_INFINITY);
case LT -> new Numeric(NEGATIVE_INFINITY, exclusive(value));
case LTE -> new Numeric(NEGATIVE_INFINITY, inclusive(value));
default -> throw new UnsupportedOperationException(
MessageFormat.format("Expression type {0} not supported for numeric fields", expression.type()));
};
}

private NumericBoundary inclusive(Value value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,13 @@ private Iterable<SchemaField> schemaFields() {

private SchemaField schemaField(MetadataField field) {
String fieldName = jsonPath(field.name);
switch (field.fieldType) {
case NUMERIC:
return NumericField.of(fieldName).as(field.name);
case TAG:
return TagField.of(fieldName).as(field.name);
case TEXT:
return TextField.of(fieldName).as(field.name);
default:
throw new IllegalArgumentException(
MessageFormat.format("Field {0} has unsupported type {1}", field.name, field.fieldType));
}
return switch (field.fieldType) {
case NUMERIC -> NumericField.of(fieldName).as(field.name);
case TAG -> TagField.of(fieldName).as(field.name);
case TEXT -> TextField.of(fieldName).as(field.name);
default -> throw new IllegalArgumentException(
MessageFormat.format("Field {0} has unsupported type {1}", field.name, field.fieldType));
};
}

private VectorAlgorithm vectorAlgorithm() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,20 @@ protected void doExpression(Filter.Expression exp, StringBuilder context) {
}

private String getOperationSymbol(Filter.Expression exp) {
switch (exp.type()) {
case AND:
return " && ";
case OR:
return " || ";
case EQ:
return " "; // in typesense "EQ" operator looks like -> country:USA
case NE:
return " != ";
case LT:
return " < ";
case LTE:
return " <= ";
case GT:
return " > ";
case GTE:
return " >= ";
case IN:
return " "; // in typesense "IN" operator looks like -> country: [USA, UK]
case NIN:
return " != "; // in typesense "NIN" operator looks like -> country:
return switch (exp.type()) {
case AND -> " && ";
case OR -> " || ";
case EQ -> " "; // in typesense "EQ" operator looks like -> country:USA
case NE -> " != ";
case LT -> " < ";
case LTE -> " <= ";
case GT -> " > ";
case GTE -> " >= ";
case IN -> " "; // in typesense "IN" operator looks like -> country: [USA, UK]
case NIN -> " != "; // in typesense "NIN" operator looks like -> country:
// !=[USA, UK]
default:
throw new RuntimeException("Not supported expression type:" + exp.type());
}
default -> throw new RuntimeException("Not supported expression type:" + exp.type());
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,32 +83,21 @@ else if (exp.type() == ExpressionType.AND || exp.type() == ExpressionType.OR) {
}

private String getOperationSymbol(Expression exp) {
switch (exp.type()) {
case AND:
return "operator:And \n";
case OR:
return "operator:Or \n";
case EQ:
return "operator:Equal \n";
case NE:
return "operator:NotEqual \n";
case LT:
return "operator:LessThan \n";
case LTE:
return "operator:LessThanEqual \n";
case GT:
return "operator:GreaterThan \n";
case GTE:
return "operator:GreaterThanEqual \n";
case IN:
throw new IllegalStateException(
"The 'IN' operator should have been transformed into chain of OR/EQ expressions.");
case NIN:
throw new IllegalStateException(
"The 'NIN' operator should have been transformed into chain of AND/NEQ expressions.");
default:
throw new UnsupportedOperationException("Not supported expression type:" + exp.type());
}
return switch (exp.type()) {
case AND -> "operator:And \n";
case OR -> "operator:Or \n";
case EQ -> "operator:Equal \n";
case NE -> "operator:NotEqual \n";
case LT -> "operator:LessThan \n";
case LTE -> "operator:LessThanEqual \n";
case GT -> "operator:GreaterThan \n";
case GTE -> "operator:GreaterThanEqual \n";
case IN -> throw new IllegalStateException(
"The 'IN' operator should have been transformed into chain of OR/EQ expressions.");
case NIN -> throw new IllegalStateException(
"The 'NIN' operator should have been transformed into chain of AND/NEQ expressions.");
default -> throw new UnsupportedOperationException("Not supported expression type:" + exp.type());
};
}

@Override
Expand Down