mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-31 19:15:20 +00:00
Require applicable interfaces to be passed an implementation they use.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package org.nwapw.abacus.function.applicable
|
||||
|
||||
import org.nwapw.abacus.plugin.NumberImplementation
|
||||
|
||||
/**
|
||||
* A class that can be applied to arguments.
|
||||
*
|
||||
@@ -15,7 +17,7 @@ interface Applicable<in T : Any, out O : Any> {
|
||||
* @param params the parameter array to verify for compatibility.
|
||||
* @return whether the array can be used with applyInternal.
|
||||
*/
|
||||
fun matchesParams(params: Array<out T>): Boolean
|
||||
fun matchesParams(implementation: NumberImplementation, params: Array<out T>): Boolean
|
||||
|
||||
/**
|
||||
* Applies the applicable object to the given parameters,
|
||||
@@ -23,7 +25,7 @@ interface Applicable<in T : Any, out O : Any> {
|
||||
* @param params the parameters to apply to.
|
||||
* @return the result of the application.
|
||||
*/
|
||||
fun applyInternal(params: Array<out T>): O?
|
||||
fun applyInternal(implementation: NumberImplementation, params: Array<out T>): O?
|
||||
|
||||
/**
|
||||
* If the parameters can be used with this applicable, returns
|
||||
@@ -32,9 +34,9 @@ interface Applicable<in T : Any, out O : Any> {
|
||||
* @param params the parameters to apply to.
|
||||
* @return the result of the operation, or null if parameters do not match.
|
||||
*/
|
||||
fun apply(vararg params: T): O? {
|
||||
if (!matchesParams(params)) return null
|
||||
return applyInternal(params)
|
||||
fun apply(implementation: NumberImplementation, vararg params: T): O? {
|
||||
if (!matchesParams(implementation, params)) return null
|
||||
return applyInternal(implementation, params)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.nwapw.abacus.function.applicable
|
||||
|
||||
import org.nwapw.abacus.plugin.NumberImplementation
|
||||
import org.nwapw.abacus.tree.Reducer
|
||||
|
||||
/**
|
||||
@@ -18,7 +19,7 @@ interface ReducerApplicable<in T : Any, out O : Any, in R : Any> {
|
||||
* given parameters.
|
||||
* @param params the parameters to check.
|
||||
*/
|
||||
fun matchesParams(params: Array<out T>): Boolean
|
||||
fun matchesParams(implementation: NumberImplementation, params: Array<out T>): Boolean
|
||||
|
||||
/**
|
||||
* Applies this applicable to the given arguments, and reducer.
|
||||
@@ -26,7 +27,7 @@ interface ReducerApplicable<in T : Any, out O : Any, in R : Any> {
|
||||
* @param params the arguments to apply to.
|
||||
* @return the result of the application.
|
||||
*/
|
||||
fun applyWithReducerInternal(reducer: Reducer<R>, params: Array<out T>): O?
|
||||
fun applyWithReducerInternal(implementation: NumberImplementation, reducer: Reducer<R>, params: Array<out T>): O?
|
||||
|
||||
/**
|
||||
* Applies this applicable to the given arguments, and reducer,
|
||||
@@ -35,9 +36,9 @@ interface ReducerApplicable<in T : Any, out O : Any, in R : Any> {
|
||||
* @param params the arguments to apply to.
|
||||
* @return the result of the application, or null if the arguments are incompatible.
|
||||
*/
|
||||
fun applyWithReducer(reducer: Reducer<R>, vararg params: T): O? {
|
||||
if (!matchesParams(params)) return null
|
||||
return applyWithReducerInternal(reducer, params)
|
||||
fun applyWithReducer(implementation: NumberImplementation, reducer: Reducer<R>, vararg params: T): O? {
|
||||
if (!matchesParams(implementation, params)) return null
|
||||
return applyWithReducerInternal(implementation, reducer, params)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,15 +25,15 @@ class PromotionManager(val abacus: Abacus) {
|
||||
})
|
||||
}
|
||||
|
||||
fun promote(vararg numbers: NumberInterface): Array<NumberInterface>? {
|
||||
fun promote(vararg numbers: NumberInterface): PromotionResult? {
|
||||
val pluginManager = abacus.pluginManager
|
||||
val implementations = numbers.map { pluginManager.interfaceImplementationFor(it.javaClass) }
|
||||
val highestPriority = implementations.sortedBy { it.priority }.last()
|
||||
return numbers.map {
|
||||
return PromotionResult(items = numbers.map {
|
||||
if(it.javaClass == highestPriority.implementation) it
|
||||
else getPathBetween(pluginManager.interfaceImplementationFor(it.javaClass), highestPriority)
|
||||
?.promote(it) ?: return null
|
||||
}.toTypedArray()
|
||||
}.toTypedArray(), promotedTo = highestPriority)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.nwapw.abacus.number
|
||||
|
||||
import org.nwapw.abacus.plugin.NumberImplementation
|
||||
|
||||
data class PromotionResult(val promotedTo: NumberImplementation, val items: Array<NumberInterface>)
|
||||
Reference in New Issue
Block a user