/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ package org.opensearch.dataprepper.expression; import org.antlr.v4.runtime.RuleContext; import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; import java.util.Map; import java.util.function.BiFunction; import static com.google.common.base.Preconditions.checkArgument; class ArithmeticBinaryOperator implements Operator { private final int symbol; private final String displayName; private final Map, Map, BiFunction>> operandsToOperationMap; public ArithmeticBinaryOperator(final int symbol, final Map, Map, BiFunction>> operandsToOperationMap) { this.symbol = symbol; displayName = DataPrepperExpressionParser.VOCABULARY.getDisplayName(symbol); this.operandsToOperationMap = operandsToOperationMap; } @Override public boolean shouldEvaluate(final RuleContext ctx) { return ctx.getRuleIndex() == DataPrepperExpressionParser.RULE_multiplicativeExpression; } @Override public int getSymbol() { return symbol; } @Override public Number evaluate(final Object ... args) { checkArgument(args.length == 2, displayName + " requires 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(displayName + " requires left operand to be either Float or Integer."); } Map, BiFunction> rightOperandToOperation = operandsToOperationMap.get(leftValueClass); if (!rightOperandToOperation.containsKey(rightValueClass)) { throw new IllegalArgumentException(displayName + " requires right operand to be either Float or Integer."); } final BiFunction operation = rightOperandToOperation.get(rightValueClass); return operation.apply(leftValue, rightValue); } }