mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-22 07:20:09 -08:00
Convert Applicable interfaces into Kotlin.
This commit is contained in:
parent
ca2681cc21
commit
5b1a48c02e
|
@ -1,5 +1,6 @@
|
|||
package org.nwapw.abacus.function
|
||||
|
||||
import org.nwapw.abacus.function.applicable.Applicable
|
||||
import org.nwapw.abacus.number.NumberInterface
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package org.nwapw.abacus.function
|
||||
|
||||
import org.nwapw.abacus.function.applicable.Applicable
|
||||
import org.nwapw.abacus.number.NumberInterface
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package org.nwapw.abacus.function
|
||||
|
||||
import org.nwapw.abacus.function.applicable.ReducerApplicable
|
||||
import org.nwapw.abacus.number.NumberInterface
|
||||
import org.nwapw.abacus.tree.TreeNode
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package org.nwapw.abacus.function
|
||||
|
||||
import org.nwapw.abacus.function.applicable.ReducerApplicable
|
||||
import org.nwapw.abacus.number.NumberInterface
|
||||
import javax.swing.tree.TreeNode
|
||||
|
||||
|
|
|
@ -1,27 +1,28 @@
|
|||
package org.nwapw.abacus.function;
|
||||
package org.nwapw.abacus.function.applicable
|
||||
|
||||
/**
|
||||
* A class that represents something that can be applied to one or more
|
||||
* A class that can be applied to arguments.
|
||||
*
|
||||
* Applicable is 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 interface Applicable<T, O> {
|
||||
interface Applicable<in T: Any, out O: Any> {
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
boolean matchesParams(T[] params);
|
||||
|
||||
fun matchesParams(params: Array<out T>): Boolean
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
O applyInternal(T[] params);
|
||||
fun applyInternal(params: Array<out T>): O?
|
||||
|
||||
/**
|
||||
* If the parameters can be used with this applicable, returns
|
||||
|
@ -30,9 +31,9 @@ public interface Applicable<T, O> {
|
|||
* @param params the parameters to apply to.
|
||||
* @return the result of the operation, or null if parameters do not match.
|
||||
*/
|
||||
default O apply(T... params){
|
||||
if(!matchesParams(params)) return null;
|
||||
return applyInternal(params);
|
||||
fun apply(vararg params: T): O? {
|
||||
if(!matchesParams(params)) return null
|
||||
return applyInternal(params)
|
||||
}
|
||||
|
||||
}
|
|
@ -1,24 +1,24 @@
|
|||
package org.nwapw.abacus.function;
|
||||
package org.nwapw.abacus.function.applicable
|
||||
|
||||
import org.nwapw.abacus.tree.Reducer;
|
||||
import org.nwapw.abacus.tree.Reducer
|
||||
|
||||
/**
|
||||
* A slightly more specific Applicable that requires a reducer
|
||||
* Applicable that requires a reducer.
|
||||
*
|
||||
* ReducerApplicable 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 interface ReducerApplicable<T, O, R> extends Applicable<T, O> {
|
||||
interface ReducerApplicable<in T: Any, out O: Any, in R: Any> : Applicable<T, O> {
|
||||
|
||||
@Override
|
||||
default O applyInternal(T[] params) {
|
||||
return null;
|
||||
override fun applyInternal(params: Array<out T>): O? {
|
||||
return null
|
||||
}
|
||||
|
||||
@Override
|
||||
default O apply(T... params) {
|
||||
return null;
|
||||
override fun apply(vararg params: T): O? {
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -27,8 +27,7 @@ public interface ReducerApplicable<T, O, R> extends Applicable<T, O> {
|
|||
* @param args the arguments to apply to.
|
||||
* @return the result of the application.
|
||||
*/
|
||||
O applyWithReducerInternal(Reducer<R> reducer, T[] args);
|
||||
|
||||
fun applyWithReducerInternal(reducer: Reducer<R>, params: Array<out T>): O?
|
||||
/**
|
||||
* Applies this applicable to the given arguments, and reducer,
|
||||
* if the arguments and reducer are compatible with this applicable.
|
||||
|
@ -36,9 +35,9 @@ public interface ReducerApplicable<T, O, R> extends Applicable<T, O> {
|
|||
* @param args the arguments to apply to.
|
||||
* @return the result of the application, or null if the arguments are incompatible.
|
||||
*/
|
||||
default O applyWithReducer(Reducer<R> reducer, T...args) {
|
||||
if(!matchesParams(args) || reducer == null) return null;
|
||||
return applyWithReducerInternal(reducer, args);
|
||||
fun applyWithReducer(reducer: Reducer<R>, vararg params: T): O? {
|
||||
if(!matchesParams(params)) return null
|
||||
return applyWithReducerInternal(reducer, params)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user