1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-22 07:20:09 -08:00

Abstract some of the Function functionality further.

This commit is contained in:
Danila Fedorin 2017-08-25 00:43:36 -07:00
parent 553c7354c1
commit 01f80bbb53
2 changed files with 39 additions and 29 deletions

View File

@ -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 <T> the type of the parameters passed to this applicable.
* @param <O> the return type of the applicable.
*/
public abstract class Applicable<T, O> {
/**
* 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);
}
}

View File

@ -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<NumberInterface, NumberInterface> {
}