Move the code for applicables that require a Reducer.

This commit is contained in:
Danila Fedorin 2017-08-25 15:25:46 -07:00
parent 225a926f86
commit da1c78945e
3 changed files with 56 additions and 43 deletions

View File

@ -0,0 +1,44 @@
package org.nwapw.abacus.function;
import org.nwapw.abacus.tree.Reducer;
/**
* A slightly more specific Applicable that requires a reducer
* to be passed to it along with the parameters.
* @param <T> the type of the input arguments.
* @param <O> the return type of the application.
* @param <R> the required type of the reducer.
*/
public abstract class ReducerApplicable<T, O, R> extends Applicable<T, O> {
@Override
protected final O applyInternal(T[] params) {
return null;
}
@Override
public final O apply(T... params) {
return null;
}
/**
* Applies this applicable to the given arguments, and reducer.
* @param reducer the reducer to use in the application.
* @param args the arguments to apply to.
* @return the result of the application.
*/
public abstract O applyWithReducerInternal(Reducer<R> reducer, T[] args);
/**
* Applies this applicable to the given arguments, and reducer,
* if the arguments and reducer are compatible with this applicable.
* @param reducer the reducer to use in the application.
* @param args the arguments to apply to.
* @return the result of the application, or null if the arguments are incompatible.
*/
public O applyWithReducer(Reducer<R> reducer, T...args) {
if(!matchesParams(args) || reducer == null) return null;
return applyWithReducerInternal(reducer, args);
}
}

View File

@ -1,43 +0,0 @@
package org.nwapw.abacus.function;
import org.nwapw.abacus.number.NumberInterface;
import org.nwapw.abacus.tree.Reducer;
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.
*/
public abstract class TreeValueFunction extends Applicable<TreeNode, NumberInterface> {
@Override
protected NumberInterface applyInternal(TreeNode[] params) {
return null;
}
@Override
public NumberInterface apply(TreeNode... params) {
return null;
}
/**
* Applies the tree value functions to the given tree nodes,
* using the given reducer.
* @param reducer the reducer to use.
* @param params the parameters to apply to.
* @return the result of the application.
*/
public abstract NumberInterface applyWithReducerInternal(Reducer<NumberInterface> reducer, TreeNode[] params);
/**
* Checks if the given parameters and reducer can be used
* with this function, and if so, calls the function on them.
* @param reducer the reducer to use.
* @param params the parameters to apply to.
* @return the result of the application, or null if the parameters are incompatible.
*/
public NumberInterface applyWithReducer(Reducer<NumberInterface> reducer, TreeNode... params) {
if(!matchesParams(params) || reducer == null) return null;
return applyWithReducerInternal(reducer, params);
}
}

View File

@ -0,0 +1,12 @@
package org.nwapw.abacus.function
import org.nwapw.abacus.number.NumberInterface
import org.nwapw.abacus.tree.TreeNode
/**
* A function that operates on trees.
*
* 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<TreeNode, NumberInterface, NumberInterface>()