mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-22 07:20:09 -08:00
Do not use null in exceptions and add messages to exceptions.
This commit is contained in:
parent
be94394a5c
commit
5b4773dee1
|
@ -3,7 +3,7 @@ package org.nwapw.abacus.exception;
|
|||
public class AbacusException extends RuntimeException {
|
||||
|
||||
public AbacusException(String baseMessage, String description){
|
||||
super(baseMessage + ((description == null) ? "." : (": " + description)));
|
||||
super(baseMessage + ((description.equals("")) ? "." : (": " + description)));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ public class ComputationInterruptedException extends AbacusException {
|
|||
* Creates a new exception of this type.
|
||||
*/
|
||||
public ComputationInterruptedException() {
|
||||
super("Computation interrupted", null);
|
||||
super("Computation interrupted", "");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ public class DomainException extends AbacusException {
|
|||
* Creates a new DomainException with a default message.
|
||||
*/
|
||||
public DomainException(){
|
||||
this(null);
|
||||
this("");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ public class EvaluationException extends AbacusException {
|
|||
* Creates a new EvaluationException with the default string.
|
||||
*/
|
||||
public EvaluationException() {
|
||||
this(null);
|
||||
this("");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -36,7 +36,8 @@ interface Applicable<in T : Any, out O : Any> {
|
|||
* @return the result of the operation, or null if parameters do not match.
|
||||
*/
|
||||
fun apply(context: MutableEvaluationContext, vararg params: T): O {
|
||||
if (!matchesParams(context, params)) throw DomainException()
|
||||
if (!matchesParams(context, params))
|
||||
throw DomainException("parameters do not match function requirements.")
|
||||
return applyInternal(context, params)
|
||||
}
|
||||
|
||||
|
|
|
@ -18,14 +18,14 @@ class NumberReducer(val abacus: Abacus, context: EvaluationContext) : Reducer<Nu
|
|||
return when(treeNode){
|
||||
is NumberNode -> {
|
||||
context.inheritedNumberImplementation?.instanceForString(treeNode.number)
|
||||
?: throw EvaluationException()
|
||||
?: throw EvaluationException("no number implementation selected.")
|
||||
}
|
||||
is VariableNode -> {
|
||||
val variable = context.getVariable(treeNode.variable)
|
||||
if(variable != null) return variable
|
||||
val definition = context.getDefinition(treeNode.variable)
|
||||
if(definition != null) return definition.reduce(this)
|
||||
throw EvaluationException()
|
||||
throw EvaluationException("variable is not defined.")
|
||||
}
|
||||
is NumberUnaryNode -> {
|
||||
val child = children[0] as NumberInterface
|
||||
|
@ -36,13 +36,15 @@ class NumberReducer(val abacus: Abacus, context: EvaluationContext) : Reducer<Nu
|
|||
is NumberBinaryNode -> {
|
||||
val left = children[0] as NumberInterface
|
||||
val right = children[1] as NumberInterface
|
||||
val promotionResult = promotionManager.promote(left, right) ?: throw EvaluationException()
|
||||
val promotionResult = promotionManager.promote(left, right) ?:
|
||||
throw EvaluationException("promotion failed.")
|
||||
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()
|
||||
.promote(*children.map { it as NumberInterface }.toTypedArray()) ?:
|
||||
throw EvaluationException("promotion failed.")
|
||||
context.numberImplementation = promotionResult.promotedTo
|
||||
abacus.pluginManager.functionFor(treeNode.callTo).apply(context, *promotionResult.items)
|
||||
}
|
||||
|
@ -58,7 +60,7 @@ class NumberReducer(val abacus: Abacus, context: EvaluationContext) : Reducer<Nu
|
|||
abacus.pluginManager.treeValueFunctionFor(treeNode.callTo)
|
||||
.apply(context, *treeNode.children.toTypedArray())
|
||||
}
|
||||
else -> throw EvaluationException()
|
||||
else -> throw EvaluationException("unrecognized tree node.")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user