1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-11-17 08:03:09 -08:00

Do not use null in exceptions and add messages to exceptions.

This commit is contained in:
Danila Fedorin 2017-09-07 13:21:18 -07:00
parent be94394a5c
commit 5b4773dee1
6 changed files with 13 additions and 10 deletions

View File

@ -3,7 +3,7 @@ package org.nwapw.abacus.exception;
public class AbacusException extends RuntimeException { public class AbacusException extends RuntimeException {
public AbacusException(String baseMessage, String description){ public AbacusException(String baseMessage, String description){
super(baseMessage + ((description == null) ? "." : (": " + description))); super(baseMessage + ((description.equals("")) ? "." : (": " + description)));
} }
} }

View File

@ -10,7 +10,7 @@ public class ComputationInterruptedException extends AbacusException {
* Creates a new exception of this type. * Creates a new exception of this type.
*/ */
public ComputationInterruptedException() { public ComputationInterruptedException() {
super("Computation interrupted", null); super("Computation interrupted", "");
} }
} }

View File

@ -18,7 +18,7 @@ public class DomainException extends AbacusException {
* Creates a new DomainException with a default message. * Creates a new DomainException with a default message.
*/ */
public DomainException(){ public DomainException(){
this(null); this("");
} }
} }

View File

@ -11,7 +11,7 @@ public class EvaluationException extends AbacusException {
* Creates a new EvaluationException with the default string. * Creates a new EvaluationException with the default string.
*/ */
public EvaluationException() { public EvaluationException() {
this(null); this("");
} }
/** /**

View File

@ -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. * @return the result of the operation, or null if parameters do not match.
*/ */
fun apply(context: MutableEvaluationContext, vararg params: T): O { 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) return applyInternal(context, params)
} }

View File

@ -18,14 +18,14 @@ class NumberReducer(val abacus: Abacus, context: EvaluationContext) : Reducer<Nu
return when(treeNode){ return when(treeNode){
is NumberNode -> { is NumberNode -> {
context.inheritedNumberImplementation?.instanceForString(treeNode.number) context.inheritedNumberImplementation?.instanceForString(treeNode.number)
?: throw EvaluationException() ?: throw EvaluationException("no number implementation selected.")
} }
is VariableNode -> { is VariableNode -> {
val variable = context.getVariable(treeNode.variable) val variable = context.getVariable(treeNode.variable)
if(variable != null) return variable if(variable != null) return variable
val definition = context.getDefinition(treeNode.variable) val definition = context.getDefinition(treeNode.variable)
if(definition != null) return definition.reduce(this) if(definition != null) return definition.reduce(this)
throw EvaluationException() throw EvaluationException("variable is not defined.")
} }
is NumberUnaryNode -> { is NumberUnaryNode -> {
val child = children[0] as NumberInterface val child = children[0] as NumberInterface
@ -36,13 +36,15 @@ class NumberReducer(val abacus: Abacus, context: EvaluationContext) : Reducer<Nu
is NumberBinaryNode -> { is NumberBinaryNode -> {
val left = children[0] as NumberInterface val left = children[0] as NumberInterface
val right = children[1] 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 context.numberImplementation = promotionResult.promotedTo
abacus.pluginManager.operatorFor(treeNode.operation).apply(context, *promotionResult.items) abacus.pluginManager.operatorFor(treeNode.operation).apply(context, *promotionResult.items)
} }
is FunctionNode -> { is FunctionNode -> {
val promotionResult = promotionManager 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 context.numberImplementation = promotionResult.promotedTo
abacus.pluginManager.functionFor(treeNode.callTo).apply(context, *promotionResult.items) 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) abacus.pluginManager.treeValueFunctionFor(treeNode.callTo)
.apply(context, *treeNode.children.toTypedArray()) .apply(context, *treeNode.children.toTypedArray())
} }
else -> throw EvaluationException() else -> throw EvaluationException("unrecognized tree node.")
} }
} }