From 8a3c61460247a25c86c55515a745713ca287ab44 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 25 Aug 2017 15:42:10 -0700 Subject: [PATCH] Make applicable into an interface. --- .../org/nwapw/abacus/function/Applicable.java | 8 +- .../abacus/function/ReducerApplicable.java | 10 +- .../nwapw/abacus/plugin/StandardPlugin.java | 104 +++++++++--------- .../org/nwapw/abacus/tree/NumberReducer.java | 5 +- .../nwapw/abacus/function/NumberFunction.kt | 2 +- .../nwapw/abacus/function/NumberOperator.kt | 3 +- .../org/nwapw/abacus/function/Operator.kt | 4 +- .../abacus/function/TreeValueFunction.kt | 2 +- 8 files changed, 70 insertions(+), 68 deletions(-) diff --git a/core/src/main/java/org/nwapw/abacus/function/Applicable.java b/core/src/main/java/org/nwapw/abacus/function/Applicable.java index ced0608..2133848 100644 --- a/core/src/main/java/org/nwapw/abacus/function/Applicable.java +++ b/core/src/main/java/org/nwapw/abacus/function/Applicable.java @@ -6,14 +6,14 @@ package org.nwapw.abacus.function; * @param the type of the parameters passed to this applicable. * @param the return type of the applicable. */ -public abstract class Applicable { +public interface Applicable { /** * Checks if the given applicable can be used with the given parameters. * @param params the parameter array to verify for compatibility. * @return whether the array can be used with applyInternal. */ - protected abstract boolean matchesParams(T[] params); + boolean matchesParams(T[] params); /** * Applies the applicable object to the given parameters, @@ -21,7 +21,7 @@ public abstract class Applicable { * @param params the parameters to apply to. * @return the result of the application. */ - protected abstract O applyInternal(T[] params); + O applyInternal(T[] params); /** * If the parameters can be used with this applicable, returns @@ -30,7 +30,7 @@ public abstract class Applicable { * @param params the parameters to apply to. * @return the result of the operation, or null if parameters do not match. */ - public O apply(T... params){ + default O apply(T... params){ if(!matchesParams(params)) return null; return applyInternal(params); } diff --git a/core/src/main/java/org/nwapw/abacus/function/ReducerApplicable.java b/core/src/main/java/org/nwapw/abacus/function/ReducerApplicable.java index 7713404..bff7934 100644 --- a/core/src/main/java/org/nwapw/abacus/function/ReducerApplicable.java +++ b/core/src/main/java/org/nwapw/abacus/function/ReducerApplicable.java @@ -9,15 +9,15 @@ import org.nwapw.abacus.tree.Reducer; * @param the return type of the application. * @param the required type of the reducer. */ -public abstract class ReducerApplicable extends Applicable { +public interface ReducerApplicable extends Applicable { @Override - protected final O applyInternal(T[] params) { + default O applyInternal(T[] params) { return null; } @Override - public final O apply(T... params) { + default O apply(T... params) { return null; } @@ -27,7 +27,7 @@ public abstract class ReducerApplicable extends Applicable { * @param args the arguments to apply to. * @return the result of the application. */ - public abstract O applyWithReducerInternal(Reducer reducer, T[] args); + O applyWithReducerInternal(Reducer reducer, T[] args); /** * Applies this applicable to the given arguments, and reducer, @@ -36,7 +36,7 @@ public abstract class ReducerApplicable extends Applicable { * @param args the arguments to apply to. * @return the result of the application, or null if the arguments are incompatible. */ - public O applyWithReducer(Reducer reducer, T...args) { + default O applyWithReducer(Reducer reducer, T...args) { if(!matchesParams(args) || reducer == null) return null; return applyWithReducerInternal(reducer, args); } diff --git a/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java b/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index c8cee64..1e0ca6f 100755 --- a/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -20,12 +20,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberOperator OP_ADD = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length >= 1; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { NumberInterface sum = params[0]; for (int i = 1; i < params.length; i++) { sum = sum.add(params[i]); @@ -38,12 +38,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberOperator OP_SUBTRACT = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 2; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { return params[0].subtract(params[1]); } @@ -53,12 +53,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberOperator OP_NEGATE = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.UNARY_PREFIX, 0) { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 1; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { return params[0].negate(); } }; @@ -67,12 +67,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberOperator OP_MULTIPLY = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1) { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length >= 1; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { NumberInterface product = params[0]; for (int i = 1; i < params.length; i++) { product = product.multiply(params[i]); @@ -85,13 +85,13 @@ public class StandardPlugin extends Plugin { */ public static final NumberOperator OP_NCR = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0) { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 2 && params[0].fractionalPart().signum() == 0 && params[1].fractionalPart().signum() == 0; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { return OP_NPR.apply(params).divide(OP_FACTORIAL.apply(params[1])); } }; @@ -154,12 +154,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberOperator OP_DIVIDE = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1) { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 2 && params[1].compareTo(fromInt(params[0].getClass(), 0)) != 0; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { return params[0].divide(params[1]); } }; @@ -169,14 +169,14 @@ public class StandardPlugin extends Plugin { public static final NumberOperator OP_FACTORIAL = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.UNARY_POSTFIX, 0) { //private HashMap, ArrayList> storedList = new HashMap, ArrayList>(); @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 1 && params[0].fractionalPart().compareTo(fromInt(params[0].getClass(), 0)) == 0 && params[0].signum() >= 0; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { if (params[0].signum() == 0) { return fromInt(params[0].getClass(), 1); } @@ -200,13 +200,13 @@ public class StandardPlugin extends Plugin { */ public static final NumberOperator OP_NPR = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0) { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 2 && params[0].fractionalPart().signum() == 0 && params[1].fractionalPart().signum() == 0; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { if (params[0].compareTo(params[1]) < 0 || params[0].signum() < 0 || (params[0].signum() == 0 && params[1].signum() != 0)) return fromInt(params[0].getClass(), 0); @@ -230,12 +230,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberFunction FUNCTION_ABS = new NumberFunction() { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 1; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { return params[0].multiply(fromInt(params[0].getClass(), params[0].signum())); } }; @@ -244,12 +244,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberFunction FUNCTION_LN = new NumberFunction() { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 1 && params[0].compareTo(fromInt(params[0].getClass(), 0)) > 0; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { NumberInterface param = params[0]; NumberInterface one = fromInt(param.getClass(), 1); int powersOf2 = 0; @@ -324,12 +324,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberFunction FUNCTION_RAND_INT = new NumberFunction() { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 1; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { return fromInt(params[0].getClass(), (int) Math.round(Math.random() * params[0].floor().intValue())); } }; @@ -338,7 +338,7 @@ public class StandardPlugin extends Plugin { */ public static final NumberOperator OP_CARET = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2) { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { NumberInterface zero = fromInt(params[0].getClass(), 0); return params.length == 2 && !(params[0].compareTo(zero) == 0 @@ -347,7 +347,7 @@ public class StandardPlugin extends Plugin { } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { NumberInterface zero = fromInt(params[0].getClass(), 0); if (params[0].compareTo(zero) == 0) return zero; @@ -368,12 +368,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberFunction FUNCTION_SQRT = new NumberFunction() { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 1; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { return OP_CARET.apply(params[0], ((new NaiveNumber(0.5)).promoteTo(params[0].getClass()))); } }; @@ -383,12 +383,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberFunction FUNCTION_EXP = new NumberFunction() { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 1; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { NumberInterface maxError = params[0].getMaxError(); int n = 0; if (params[0].signum() < 0) { @@ -420,12 +420,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionSin = new NumberFunction() { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 1; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { NumberInterface pi = piFor(params[0].getClass()); NumberInterface twoPi = pi.multiply(fromInt(pi.getClass(), 2)); NumberInterface theta = getSmallAngle(params[0], pi); @@ -444,12 +444,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionCos = new NumberFunction() { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 1; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { return functionSin.apply(piFor(params[0].getClass()).divide(fromInt(params[0].getClass(), 2)) .subtract(params[0])); } @@ -459,12 +459,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionTan = new NumberFunction() { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 1; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { return functionSin.apply(params[0]).divide(functionCos.apply(params[0])); } }; @@ -473,12 +473,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionSec = new NumberFunction() { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 1; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { return fromInt(params[0].getClass(), 1).divide(functionCos.apply(params[0])); } }; @@ -487,12 +487,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionCsc = new NumberFunction() { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 1; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { return fromInt(params[0].getClass(), 1).divide(functionSin.apply(params[0])); } }; @@ -501,12 +501,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionCot = new NumberFunction() { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 1; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { return functionCos.apply(params[0]).divide(functionSin.apply(params[0])); } }; @@ -516,13 +516,13 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionArcsin = new NumberFunction() { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 1 && FUNCTION_ABS.apply(params[0]).compareTo(fromInt(params[0].getClass(), 1)) <= 0; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { if (FUNCTION_ABS.apply(params[0]).compareTo(new NaiveNumber(0.8).promoteTo(params[0].getClass())) >= 0) { NumberInterface[] newParams = {FUNCTION_SQRT.apply(fromInt(params[0].getClass(), 1).subtract(params[0].multiply(params[0])))}; return piFor(params[0].getClass()).divide(fromInt(params[0].getClass(), 2)) @@ -549,12 +549,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionArccos = new NumberFunction() { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 1 && FUNCTION_ABS.apply(params[0]).compareTo(fromInt(params[0].getClass(), 1)) <= 0; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { return piFor(params[0].getClass()).divide(fromInt(params[0].getClass(), 2)) .subtract(functionArcsin.apply(params)); } @@ -565,12 +565,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionArccsc = new NumberFunction() { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 1 && FUNCTION_ABS.apply(params[0]).compareTo(fromInt(params[0].getClass(), 1)) >= 0; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { NumberInterface[] reciprocalParamArr = {fromInt(params[0].getClass(), 1).divide(params[0])}; return functionArcsin.apply(reciprocalParamArr); } @@ -581,12 +581,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionArcsec = new NumberFunction() { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 1 && FUNCTION_ABS.apply(params[0]).compareTo(fromInt(params[0].getClass(), 1)) >= 0; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { NumberInterface[] reciprocalParamArr = {fromInt(params[0].getClass(), 1).divide(params[0])}; return functionArccos.apply(reciprocalParamArr); } @@ -597,12 +597,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionArctan = new NumberFunction() { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 1; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { if (params[0].signum() == -1) { NumberInterface[] negatedParams = {params[0].negate()}; return applyInternal(negatedParams).negate(); @@ -638,12 +638,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionArccot = new NumberFunction() { @Override - protected boolean matchesParams(NumberInterface[] params) { + public boolean matchesParams(NumberInterface[] params) { return params.length == 1; } @Override - protected NumberInterface applyInternal(NumberInterface[] params) { + public NumberInterface applyInternal(NumberInterface[] params) { return piFor(params[0].getClass()).divide(fromInt(params[0].getClass(), 2)) .subtract(functionArctan.apply(params)); } diff --git a/core/src/main/java/org/nwapw/abacus/tree/NumberReducer.java b/core/src/main/java/org/nwapw/abacus/tree/NumberReducer.java index ec3b6f3..5160a3b 100644 --- a/core/src/main/java/org/nwapw/abacus/tree/NumberReducer.java +++ b/core/src/main/java/org/nwapw/abacus/tree/NumberReducer.java @@ -2,6 +2,7 @@ package org.nwapw.abacus.tree; import org.nwapw.abacus.Abacus; import org.nwapw.abacus.function.NumberFunction; +import org.nwapw.abacus.function.NumberOperator; import org.nwapw.abacus.function.Operator; import org.nwapw.abacus.function.TreeValueFunction; import org.nwapw.abacus.number.NumberInterface; @@ -35,11 +36,11 @@ public class NumberReducer implements Reducer { } else if (node instanceof BinaryNode) { NumberInterface left = (NumberInterface) children[0]; NumberInterface right = (NumberInterface) children[1]; - Operator operator = abacus.getPluginManager().operatorFor(((BinaryNode) node).getOperation()); + NumberOperator operator = abacus.getPluginManager().operatorFor(((BinaryNode) node).getOperation()); return operator.apply(left, right); } else if (node instanceof UnaryNode) { NumberInterface child = (NumberInterface) children[0]; - Operator operator = abacus.getPluginManager().operatorFor(((UnaryNode) node).getOperation()); + NumberOperator operator = abacus.getPluginManager().operatorFor(((UnaryNode) node).getOperation()); return operator.apply(child); } else if (node instanceof FunctionNode) { NumberInterface[] convertedChildren = new NumberInterface[children.length]; diff --git a/core/src/main/kotlin/org/nwapw/abacus/function/NumberFunction.kt b/core/src/main/kotlin/org/nwapw/abacus/function/NumberFunction.kt index a706274..d0946e6 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/function/NumberFunction.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/function/NumberFunction.kt @@ -8,4 +8,4 @@ import org.nwapw.abacus.number.NumberInterface * This function takes some number of input NumberInterfaces and returns * another NumberInterface as a result. */ -abstract class NumberFunction : Applicable() \ No newline at end of file +abstract class NumberFunction : Applicable \ No newline at end of file diff --git a/core/src/main/kotlin/org/nwapw/abacus/function/NumberOperator.kt b/core/src/main/kotlin/org/nwapw/abacus/function/NumberOperator.kt index cf7979d..9b1f2f1 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/function/NumberOperator.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/function/NumberOperator.kt @@ -12,4 +12,5 @@ import org.nwapw.abacus.number.NumberInterface */ abstract class NumberOperator(associativity: OperatorAssociativity, type: OperatorType, precedence: Int) : - Operator(associativity, type, precedence) \ No newline at end of file + Operator(associativity, type, precedence), + Applicable \ No newline at end of file diff --git a/core/src/main/kotlin/org/nwapw/abacus/function/Operator.kt b/core/src/main/kotlin/org/nwapw/abacus/function/Operator.kt index 0a70b68..31f1028 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/function/Operator.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/function/Operator.kt @@ -9,5 +9,5 @@ package org.nwapw.abacus.function * @param type the type of this operator, used for parsing (infix / prefix / postfix and binary / unary) * @param precedence the precedence of this operator, used for order of operations. */ -abstract class Operator(val associativity: OperatorAssociativity, val type: OperatorType, - val precedence: Int) : Applicable() \ No newline at end of file +open class Operator(val associativity: OperatorAssociativity, val type: OperatorType, + val precedence: Int) \ No newline at end of file diff --git a/core/src/main/kotlin/org/nwapw/abacus/function/TreeValueFunction.kt b/core/src/main/kotlin/org/nwapw/abacus/function/TreeValueFunction.kt index 2038e6b..42568ea 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/function/TreeValueFunction.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/function/TreeValueFunction.kt @@ -9,4 +9,4 @@ import org.nwapw.abacus.tree.TreeNode * A function that operates on parse tree nodes instead of on already simplified numbers. * Despite this, it returns a number, not a tree. */ -abstract class TreeValueFunction : ReducerApplicable() +abstract class TreeValueFunction : ReducerApplicable