-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAINT: OperatorFactory to produce negate and numeric comparison operator
Signed-off-by: Chen <[email protected]>
- Loading branch information
1 parent
da97f79
commit 9e66532
Showing
9 changed files
with
185 additions
and
95 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
...repper-expression/src/main/java/org/opensearch/dataprepper/expression/NegateOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.expression; | ||
|
||
public class NegateOperator implements Operator<Boolean> { | ||
private final Integer symbol; | ||
private final Operator<Boolean> operationToNegate; | ||
|
||
public NegateOperator(final Integer symbol, Operator<Boolean> operationToNegate) { | ||
this.symbol = symbol; | ||
this.operationToNegate = operationToNegate; | ||
} | ||
|
||
@Override | ||
public Integer getSymbol() { | ||
return symbol; | ||
} | ||
|
||
@Override | ||
public Boolean evaluate(final Object... args) { | ||
return !operationToNegate.evaluate(args); | ||
} | ||
} |
30 changes: 0 additions & 30 deletions
30
...pper-expression/src/main/java/org/opensearch/dataprepper/expression/NotEqualOperator.java
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
...prepper-expression/src/main/java/org/opensearch/dataprepper/expression/NotInOperator.java
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
...xpression/src/main/java/org/opensearch/dataprepper/expression/NumericCompareOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.expression; | ||
|
||
import java.util.Map; | ||
import java.util.function.BiFunction; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
|
||
public class NumericCompareOperator implements Operator<Boolean> { | ||
private final Integer symbol; | ||
private final Map<Class<? extends Number>, Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>>> operandsToOperationMap; | ||
|
||
public NumericCompareOperator( | ||
final Integer symbol, | ||
final Map<Class<? extends Number>, Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>>> operandsToOperationMap) { | ||
this.symbol = symbol; | ||
this.operandsToOperationMap = operandsToOperationMap; | ||
} | ||
|
||
@Override | ||
public Integer getSymbol() { | ||
return symbol; | ||
} | ||
|
||
@Override | ||
public Boolean evaluate(final Object... args) { | ||
checkArgument(args.length == 2, "Operands length needs to be 2."); | ||
final Object leftValue = args[0]; | ||
final Object rightValue = args[1]; | ||
final Class<?> leftValueClass = leftValue.getClass(); | ||
final Class<?> rightValueClass = rightValue.getClass(); | ||
if (!operandsToOperationMap.containsKey(leftValueClass)) { | ||
throw new IllegalArgumentException(leftValue + " should be either Float or Integer"); | ||
} | ||
Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>> rightOperandToOperation = | ||
operandsToOperationMap.get(leftValueClass); | ||
if (!rightOperandToOperation.containsKey(rightValueClass)) { | ||
throw new IllegalArgumentException(rightValue + " should be either Float or Integer"); | ||
} | ||
final BiFunction<Object, Object, Boolean> operation = rightOperandToOperation.get(rightValueClass); | ||
return operation.apply(leftValue, rightValue); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
...epper-expression/src/main/java/org/opensearch/dataprepper/expression/OperatorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.expression; | ||
|
||
import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
import javax.inject.Named; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.BiFunction; | ||
|
||
@Named | ||
public class OperatorFactory { | ||
|
||
@Bean | ||
public Operator<Boolean> notEqualOperator(final EqualOperator equalOperator) { | ||
return new NegateOperator(DataPrepperExpressionParser.NOT_EQUAL, equalOperator); | ||
} | ||
|
||
@Bean | ||
public Operator<Boolean> notInOperator(final InOperator inOperator) { | ||
return new NegateOperator(DataPrepperExpressionParser.NOT_IN_SET, inOperator); | ||
} | ||
|
||
@Bean | ||
public Operator<Boolean> regexNotEqualOperator(final RegexEqualOperator regexEqualOperator) { | ||
return new NegateOperator(DataPrepperExpressionParser.NOT_MATCH_REGEX_PATTERN, regexEqualOperator); | ||
} | ||
|
||
@Bean | ||
public NumericCompareOperator greaterThanOperator() { | ||
final Map<Class<? extends Number>, Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>>> | ||
operandsToOperationMap = new HashMap<>(); | ||
final Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>> intOperations = | ||
new HashMap<Class<? extends Number>, BiFunction<Object, Object, Boolean>>() {{ | ||
put(Integer.class, (lhs, rhs) -> (Integer) lhs > (Integer) rhs); | ||
put(Float.class, (lhs, rhs) -> (Integer) lhs > (Float) rhs);}}; | ||
final Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>> floatOperations = | ||
new HashMap<Class<? extends Number>, BiFunction<Object, Object, Boolean>>() {{ | ||
put(Float.class, (lhs, rhs) -> (Float) lhs > (Integer) rhs); | ||
put(Integer.class, (lhs, rhs) -> (Float) lhs > (Integer) rhs);}}; | ||
|
||
operandsToOperationMap.put(Integer.class, intOperations); | ||
operandsToOperationMap.put(Float.class, floatOperations); | ||
|
||
return new NumericCompareOperator(DataPrepperExpressionParser.GT, operandsToOperationMap); | ||
} | ||
|
||
@Bean | ||
public NumericCompareOperator greaterThanOrEqualOperator() { | ||
final Map<Class<? extends Number>, Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>>> | ||
operandsToOperationMap = new HashMap<>(); | ||
final Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>> intOperations = | ||
new HashMap<Class<? extends Number>, BiFunction<Object, Object, Boolean>>() {{ | ||
put(Integer.class, (lhs, rhs) -> (Integer) lhs >= (Integer) rhs); | ||
put(Float.class, (lhs, rhs) -> (Integer) lhs >= (Float) rhs);}}; | ||
final Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>> floatOperations = | ||
new HashMap<Class<? extends Number>, BiFunction<Object, Object, Boolean>>() {{ | ||
put(Float.class, (lhs, rhs) -> (Float) lhs >= (Integer) rhs); | ||
put(Integer.class, (lhs, rhs) -> (Float) lhs >= (Integer) rhs);}}; | ||
|
||
operandsToOperationMap.put(Integer.class, intOperations); | ||
operandsToOperationMap.put(Float.class, floatOperations); | ||
|
||
return new NumericCompareOperator(DataPrepperExpressionParser.GTE, operandsToOperationMap); | ||
} | ||
|
||
@Bean | ||
public NumericCompareOperator lessThanOperator() { | ||
final Map<Class<? extends Number>, Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>>> | ||
operandsToOperationMap = new HashMap<>(); | ||
final Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>> intOperations = | ||
new HashMap<Class<? extends Number>, BiFunction<Object, Object, Boolean>>() {{ | ||
put(Integer.class, (lhs, rhs) -> (Integer) lhs < (Integer) rhs); | ||
put(Float.class, (lhs, rhs) -> (Integer) lhs < (Float) rhs);}}; | ||
final Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>> floatOperations = | ||
new HashMap<Class<? extends Number>, BiFunction<Object, Object, Boolean>>() {{ | ||
put(Float.class, (lhs, rhs) -> (Float) lhs < (Integer) rhs); | ||
put(Integer.class, (lhs, rhs) -> (Float) lhs < (Integer) rhs);}}; | ||
|
||
operandsToOperationMap.put(Integer.class, intOperations); | ||
operandsToOperationMap.put(Float.class, floatOperations); | ||
|
||
return new NumericCompareOperator(DataPrepperExpressionParser.LT, operandsToOperationMap); | ||
} | ||
|
||
@Bean | ||
public NumericCompareOperator lessThanOrEqualOperator() { | ||
final Map<Class<? extends Number>, Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>>> | ||
operandsToOperationMap = new HashMap<>(); | ||
final Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>> intOperations = | ||
new HashMap<Class<? extends Number>, BiFunction<Object, Object, Boolean>>() {{ | ||
put(Integer.class, (lhs, rhs) -> (Integer) lhs <= (Integer) rhs); | ||
put(Float.class, (lhs, rhs) -> (Integer) lhs <= (Float) rhs);}}; | ||
final Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>> floatOperations = | ||
new HashMap<Class<? extends Number>, BiFunction<Object, Object, Boolean>>() {{ | ||
put(Float.class, (lhs, rhs) -> (Float) lhs <= (Integer) rhs); | ||
put(Integer.class, (lhs, rhs) -> (Float) lhs <= (Integer) rhs);}}; | ||
|
||
operandsToOperationMap.put(Integer.class, intOperations); | ||
operandsToOperationMap.put(Float.class, floatOperations); | ||
|
||
return new NumericCompareOperator(DataPrepperExpressionParser.LTE, operandsToOperationMap); | ||
} | ||
} |
33 changes: 0 additions & 33 deletions
33
...expression/src/main/java/org/opensearch/dataprepper/expression/RegexNotEqualOperator.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters