Skip to content

Commit

Permalink
MAINT: OperatorFactory to produce negate and numeric comparison operator
Browse files Browse the repository at this point in the history
Signed-off-by: Chen <[email protected]>
  • Loading branch information
chenqi0805 committed Mar 1, 2022
1 parent da97f79 commit 9e66532
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 95 deletions.
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);
}
}

This file was deleted.

This file was deleted.

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);
}
}
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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

class NotEqualOperatorTest {
final NotEqualOperator objectUnderTest = new NotEqualOperator();
final Operator<Boolean> objectUnderTest = new OperatorFactory().notEqualOperator(new EqualOperator());

@Test
void testGetSymbol() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

class NotInOperatorTest {
final NotInOperator objectUnderTest = new NotInOperator();
final Operator<Boolean> objectUnderTest = new OperatorFactory().notInOperator(new InOperator());

@Test
void testGetSymbol() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

class RegexNotEqualOperatorTest {
final RegexNotEqualOperator objectUnderTest = new RegexNotEqualOperator();
final Operator<Boolean> objectUnderTest = new OperatorFactory().regexNotEqualOperator(new RegexEqualOperator());

@Test
void testGetSymbol() {
Expand Down

0 comments on commit 9e66532

Please sign in to comment.