-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from sidhant92/array_math_functions
Add Support for Arithmetic Functions
- Loading branch information
Showing
28 changed files
with
1,487 additions
and
398 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -30,3 +30,6 @@ out/ | |
` | ||
|
||
gradle.properties | ||
|
||
*.DS_Store | ||
**/.DS_Store |
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
28 changes: 28 additions & 0 deletions
28
src/main/java/com/github/sidhant92/boolparser/constant/FunctionType.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,28 @@ | ||
package com.github.sidhant92.boolparser.constant; | ||
|
||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
/** | ||
* @author sidhant.aggarwal | ||
* @since 21/05/2024 | ||
*/ | ||
public enum FunctionType { | ||
MIN, | ||
MAX, | ||
AVG, | ||
SUM, | ||
MEAN, | ||
MODE, | ||
MEDIAN, | ||
INT, | ||
LEN; | ||
|
||
public static Optional<FunctionType> getArrayFunctionFromSymbol(final String symbol) { | ||
final String symbolUpperCase = symbol.toUpperCase(); | ||
return Arrays | ||
.stream(FunctionType.values()) | ||
.filter(function -> function.name().equals(symbolUpperCase)) | ||
.findFirst(); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -14,5 +14,6 @@ public enum NodeType { | |
ARITHMETIC, | ||
ARITHMETIC_LEAF, | ||
ARITHMETIC_UNARY, | ||
ARITHMETIC_FUNCTION, | ||
STRING | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/github/sidhant92/boolparser/domain/arithmetic/ArithmeticFunctionNode.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,30 @@ | ||
package com.github.sidhant92.boolparser.domain.arithmetic; | ||
|
||
import java.util.List; | ||
import com.github.sidhant92.boolparser.constant.FunctionType; | ||
import com.github.sidhant92.boolparser.constant.NodeType; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
/** | ||
* @author sidhant.aggarwal | ||
* @since 21/05/2024 | ||
*/ | ||
@AllArgsConstructor | ||
@Getter | ||
@Setter | ||
@Builder | ||
public class ArithmeticFunctionNode extends ArithmeticBaseNode { | ||
private FunctionType functionType; | ||
|
||
|
||
private final List<ArithmeticLeafNode> items; | ||
|
||
|
||
@Override | ||
public NodeType getTokenType() { | ||
return NodeType.ARITHMETIC_FUNCTION; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/github/sidhant92/boolparser/function/FunctionEvaluatorService.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,50 @@ | ||
package com.github.sidhant92.boolparser.function; | ||
|
||
import java.util.List; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import com.github.sidhant92.boolparser.constant.ContainerDataType; | ||
import com.github.sidhant92.boolparser.constant.DataType; | ||
import com.github.sidhant92.boolparser.constant.FunctionType; | ||
import com.github.sidhant92.boolparser.exception.InvalidContainerTypeException; | ||
import com.github.sidhant92.boolparser.exception.InvalidDataType; | ||
import com.github.sidhant92.boolparser.exception.InvalidExpressionException; | ||
import com.github.sidhant92.boolparser.function.arithmetic.AbstractFunction; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* @author sidhant.aggarwal | ||
* @since 21/05/2024 | ||
*/ | ||
@Slf4j | ||
public class FunctionEvaluatorService { | ||
public FunctionEvaluatorService() { | ||
FunctionFactory.initialize(); | ||
} | ||
|
||
public Object evaluateArithmeticFunction(final FunctionType functionType, final List<Pair<Object, DataType>> items) { | ||
final AbstractFunction abstractFunction = FunctionFactory.getArithmeticFunction(functionType); | ||
if (items.isEmpty()) { | ||
log.error("Empty items not allowed"); | ||
throw new InvalidExpressionException(); | ||
} | ||
final ContainerDataType containerDataType = items.size() > 1 ? ContainerDataType.LIST : ContainerDataType.PRIMITIVE; | ||
if (!abstractFunction.getAllowedContainerTypes().contains(containerDataType)) { | ||
log.error("Invalid container type {} for function {}", containerDataType, functionType.name()); | ||
throw new InvalidContainerTypeException(); | ||
} | ||
final boolean validDataType = items | ||
.stream().allMatch(item -> abstractFunction.getAllowedDataTypes().contains(item.getValue())); | ||
if (!validDataType) { | ||
log.error("Invalid data type {} for function {}", items, functionType.name()); | ||
throw new InvalidDataType(); | ||
} | ||
|
||
items.forEach(item -> { | ||
if (!ContainerDataType.PRIMITIVE.isValid(item.getValue(), item.getKey())) { | ||
log.error("Validation failed for the function {} for the operand {}", functionType.name(), item.getKey()); | ||
throw new InvalidDataType(); | ||
} | ||
}); | ||
return abstractFunction.evaluate(items); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/github/sidhant92/boolparser/function/FunctionFactory.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,43 @@ | ||
package com.github.sidhant92.boolparser.function; | ||
|
||
import java.util.EnumMap; | ||
import java.util.Map; | ||
import com.github.sidhant92.boolparser.constant.FunctionType; | ||
import com.github.sidhant92.boolparser.function.arithmetic.AbstractFunction; | ||
import com.github.sidhant92.boolparser.function.arithmetic.AvgFunction; | ||
import com.github.sidhant92.boolparser.function.arithmetic.IntFunction; | ||
import com.github.sidhant92.boolparser.function.arithmetic.LenFunction; | ||
import com.github.sidhant92.boolparser.function.arithmetic.MaxFunction; | ||
import com.github.sidhant92.boolparser.function.arithmetic.MeanFunction; | ||
import com.github.sidhant92.boolparser.function.arithmetic.MedianFunction; | ||
import com.github.sidhant92.boolparser.function.arithmetic.MinFunction; | ||
import com.github.sidhant92.boolparser.function.arithmetic.ModeFunction; | ||
import com.github.sidhant92.boolparser.function.arithmetic.SumFunction; | ||
|
||
/** | ||
* @author sidhant.aggarwal | ||
* @since 21/05/2024 | ||
*/ | ||
public class FunctionFactory { | ||
private static final Map<FunctionType, AbstractFunction> arithmeticFunctionrMap = new EnumMap<>(FunctionType.class); | ||
|
||
private FunctionFactory() { | ||
super(); | ||
} | ||
|
||
public static void initialize() { | ||
arithmeticFunctionrMap.put(FunctionType.MIN, new MinFunction()); | ||
arithmeticFunctionrMap.put(FunctionType.MAX, new MaxFunction()); | ||
arithmeticFunctionrMap.put(FunctionType.AVG, new AvgFunction()); | ||
arithmeticFunctionrMap.put(FunctionType.SUM, new SumFunction()); | ||
arithmeticFunctionrMap.put(FunctionType.MEAN, new MeanFunction()); | ||
arithmeticFunctionrMap.put(FunctionType.MEDIAN, new MedianFunction()); | ||
arithmeticFunctionrMap.put(FunctionType.MODE, new ModeFunction()); | ||
arithmeticFunctionrMap.put(FunctionType.INT, new IntFunction()); | ||
arithmeticFunctionrMap.put(FunctionType.LEN, new LenFunction()); | ||
} | ||
|
||
public static AbstractFunction getArithmeticFunction(final FunctionType functionType) { | ||
return arithmeticFunctionrMap.get(functionType); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/github/sidhant92/boolparser/function/arithmetic/AbstractFunction.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,22 @@ | ||
package com.github.sidhant92.boolparser.function.arithmetic; | ||
|
||
import java.util.List; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import com.github.sidhant92.boolparser.constant.ContainerDataType; | ||
import com.github.sidhant92.boolparser.constant.DataType; | ||
import com.github.sidhant92.boolparser.constant.FunctionType; | ||
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticFunctionNode; | ||
|
||
/** | ||
* @author sidhant.aggarwal | ||
* @since 21/05/2024 | ||
*/ | ||
public abstract class AbstractFunction { | ||
public abstract Object evaluate(final List<Pair<Object, DataType>> items); | ||
|
||
public abstract FunctionType getFunctionType(); | ||
|
||
public abstract List<ContainerDataType> getAllowedContainerTypes(); | ||
|
||
public abstract List<DataType> getAllowedDataTypes(); | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/github/sidhant92/boolparser/function/arithmetic/AvgFunction.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 @@ | ||
package com.github.sidhant92.boolparser.function.arithmetic; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import com.github.sidhant92.boolparser.constant.ContainerDataType; | ||
import com.github.sidhant92.boolparser.constant.DataType; | ||
import com.github.sidhant92.boolparser.constant.FunctionType; | ||
import com.github.sidhant92.boolparser.util.ValueUtils; | ||
|
||
/** | ||
* @author sidhant.aggarwal | ||
* @since 21/05/2024 | ||
*/ | ||
public class AvgFunction extends AbstractFunction { | ||
@Override | ||
public Object evaluate(final List<Pair<Object, DataType>> items) { | ||
if (items | ||
.stream().anyMatch(a -> a.getValue().equals(DataType.DECIMAL))) { | ||
return ValueUtils.caseDouble(items | ||
.stream().mapToDouble(a -> Double.parseDouble(a.getKey().toString())).average().getAsDouble()); | ||
} | ||
if (items | ||
.stream().anyMatch(a -> a.getValue().equals(DataType.LONG))) { | ||
return ValueUtils.caseDouble(items | ||
.stream().mapToLong(a -> Long.parseLong(a.getKey().toString())).average().getAsDouble()); | ||
} | ||
return ValueUtils.caseDouble(items | ||
.stream().mapToInt(a -> Integer.parseInt(a.getKey().toString())).average().getAsDouble()); | ||
} | ||
|
||
@Override | ||
public FunctionType getFunctionType() { | ||
return FunctionType.AVG; | ||
} | ||
|
||
@Override | ||
public List<ContainerDataType> getAllowedContainerTypes() { | ||
return Collections.singletonList(ContainerDataType.LIST); | ||
} | ||
|
||
@Override | ||
public List<DataType> getAllowedDataTypes() { | ||
return Arrays.asList(DataType.INTEGER, DataType.LONG, DataType.DECIMAL); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/github/sidhant92/boolparser/function/arithmetic/IntFunction.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,42 @@ | ||
package com.github.sidhant92.boolparser.function.arithmetic; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import com.github.sidhant92.boolparser.constant.ContainerDataType; | ||
import com.github.sidhant92.boolparser.constant.DataType; | ||
import com.github.sidhant92.boolparser.constant.FunctionType; | ||
|
||
/** | ||
* @author sidhant.aggarwal | ||
* @since 21/05/2024 | ||
*/ | ||
public class IntFunction extends AbstractFunction { | ||
@Override | ||
public Object evaluate(final List<Pair<Object, DataType>> items) { | ||
final Pair<Object, DataType> item = items.get(0); | ||
if (item.getValue() == DataType.DECIMAL) { | ||
return ((Double) item.getKey()).intValue(); | ||
} | ||
if (item.getValue() == DataType.LONG) { | ||
return ((Long) item.getKey()).intValue(); | ||
} | ||
return item.getKey(); | ||
} | ||
|
||
@Override | ||
public FunctionType getFunctionType() { | ||
return FunctionType.INT; | ||
} | ||
|
||
@Override | ||
public List<ContainerDataType> getAllowedContainerTypes() { | ||
return Collections.singletonList(ContainerDataType.PRIMITIVE); | ||
} | ||
|
||
@Override | ||
public List<DataType> getAllowedDataTypes() { | ||
return Arrays.asList(DataType.INTEGER, DataType.LONG, DataType.DECIMAL); | ||
} | ||
} |
Oops, something went wrong.