diff --git a/core/src/main/java/org/nwapw/abacus/exception/PromotionException.java b/core/src/main/java/org/nwapw/abacus/exception/PromotionException.java new file mode 100644 index 0000000..057d6c8 --- /dev/null +++ b/core/src/main/java/org/nwapw/abacus/exception/PromotionException.java @@ -0,0 +1,24 @@ +package org.nwapw.abacus.exception; + +/** + * Exception thrown when a promotion fails. + */ +public class PromotionException extends AbacusException { + + /** + * Creates a new PromotionException with the default message + * and no additional information. + */ + public PromotionException() { + this(""); + } + + /** + * Creates a new PromotionException with the given additional message. + * @param message the additional message to include with the error. + */ + public PromotionException(String message) { + super("Failed to promote number instances.", message); + } + +} diff --git a/core/src/main/kotlin/org/nwapw/abacus/number/PromotionManager.kt b/core/src/main/kotlin/org/nwapw/abacus/number/PromotionManager.kt index a49a61c..e259446 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/number/PromotionManager.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/number/PromotionManager.kt @@ -1,6 +1,7 @@ package org.nwapw.abacus.number import org.nwapw.abacus.Abacus +import org.nwapw.abacus.exception.PromotionException import org.nwapw.abacus.plugin.NumberImplementation import org.nwapw.abacus.plugin.PluginListener import org.nwapw.abacus.plugin.PluginManager @@ -45,14 +46,14 @@ class PromotionManager(val abacus: Abacus) : PluginListener { * @param numbers the numbers to promote. * @return the resulting promotion result. */ - fun promote(vararg numbers: NumberInterface): PromotionResult? { + 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 PromotionResult(items = numbers.map { if(it.javaClass == highestPriority.implementation) it else computePaths[pluginManager.interfaceImplementationFor(it.javaClass) to highestPriority] - ?.promote(it) ?: return null + ?.promote(it) ?: throw PromotionException() }.toTypedArray(), promotedTo = highestPriority) } diff --git a/core/src/main/kotlin/org/nwapw/abacus/tree/NumberReducer.kt b/core/src/main/kotlin/org/nwapw/abacus/tree/NumberReducer.kt index 7a80ca7..7eff50a 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/tree/NumberReducer.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/NumberReducer.kt @@ -36,15 +36,13 @@ class NumberReducer(val abacus: Abacus, context: EvaluationContext) : Reducer { val left = children[0] as NumberInterface val right = children[1] as NumberInterface - val promotionResult = promotionManager.promote(left, right) ?: - throw EvaluationException("promotion failed.") + val promotionResult = promotionManager.promote(left, right) context.numberImplementation = promotionResult.promotedTo abacus.pluginManager.operatorFor(treeNode.operation).apply(context, *promotionResult.items) } is FunctionNode -> { val promotionResult = promotionManager - .promote(*children.map { it as NumberInterface }.toTypedArray()) ?: - throw EvaluationException("promotion failed.") + .promote(*children.map { it as NumberInterface }.toTypedArray()) context.numberImplementation = promotionResult.promotedTo abacus.pluginManager.functionFor(treeNode.callTo).apply(context, *promotionResult.items) }