diff --git a/core/src/main/java/org/nwapw/abacus/function/Applicable.java b/core/src/main/java/org/nwapw/abacus/function/Applicable.java new file mode 100644 index 0000000..ced0608 --- /dev/null +++ b/core/src/main/java/org/nwapw/abacus/function/Applicable.java @@ -0,0 +1,38 @@ +package org.nwapw.abacus.function; + +/** + * A class that represents something that can be applied to one or more + * arguments of the same type, and returns a single value from that application. + * @param the type of the parameters passed to this applicable. + * @param the return type of the applicable. + */ +public abstract class 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); + + /** + * Applies the applicable object to the given parameters, + * without checking for compatibility. + * @param params the parameters to apply to. + * @return the result of the application. + */ + protected abstract O applyInternal(T[] params); + + /** + * If the parameters can be used with this applicable, returns + * the result of the application of the applicable to the parameters. + * Otherwise, returns null. + * @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){ + if(!matchesParams(params)) return null; + return applyInternal(params); + } + +} diff --git a/core/src/main/java/org/nwapw/abacus/function/Function.java b/core/src/main/java/org/nwapw/abacus/function/Function.java index bd5af72..9ee9509 100755 --- a/core/src/main/java/org/nwapw/abacus/function/Function.java +++ b/core/src/main/java/org/nwapw/abacus/function/Function.java @@ -6,34 +6,6 @@ import org.nwapw.abacus.number.NumberInterface; * A function that operates on one or more * inputs and returns a single number. */ -public abstract class Function { - - /** - * Checks whether the given params will work for the given function. - * - * @param params the given params - * @return true if the params can be used with this function. - */ - protected abstract boolean matchesParams(NumberInterface[] params); - - /** - * Internal apply implementation, which already receives appropriately promoted - * parameters that have bee run through matchesParams - * - * @param params the promoted parameters. - * @return the return value of the function. - */ - protected abstract NumberInterface applyInternal(NumberInterface[] params); - - /** - * Function to check, promote arguments and run the function. - * - * @param params the raw input parameters. - * @return the return value of the function, or null if an error occurred. - */ - public NumberInterface apply(NumberInterface... params) { - if (!matchesParams(params)) return null; - return applyInternal(params); - } +public abstract class Function extends Applicable { }