From 5b4773dee1a69838042aa98bbb0591754a2bf677 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 7 Sep 2017 13:21:18 -0700 Subject: [PATCH] Do not use null in exceptions and add messages to exceptions. --- .../org/nwapw/abacus/exception/AbacusException.java | 2 +- .../exception/ComputationInterruptedException.java | 2 +- .../org/nwapw/abacus/exception/DomainException.java | 2 +- .../nwapw/abacus/exception/EvaluationException.java | 2 +- .../nwapw/abacus/function/applicable/Applicable.kt | 3 ++- .../kotlin/org/nwapw/abacus/tree/NumberReducer.kt | 12 +++++++----- 6 files changed, 13 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/org/nwapw/abacus/exception/AbacusException.java b/core/src/main/java/org/nwapw/abacus/exception/AbacusException.java index 894e959..f71cfe1 100644 --- a/core/src/main/java/org/nwapw/abacus/exception/AbacusException.java +++ b/core/src/main/java/org/nwapw/abacus/exception/AbacusException.java @@ -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))); } } diff --git a/core/src/main/java/org/nwapw/abacus/exception/ComputationInterruptedException.java b/core/src/main/java/org/nwapw/abacus/exception/ComputationInterruptedException.java index bd77649..5674a27 100644 --- a/core/src/main/java/org/nwapw/abacus/exception/ComputationInterruptedException.java +++ b/core/src/main/java/org/nwapw/abacus/exception/ComputationInterruptedException.java @@ -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", ""); } } diff --git a/core/src/main/java/org/nwapw/abacus/exception/DomainException.java b/core/src/main/java/org/nwapw/abacus/exception/DomainException.java index 4b7fe17..e1cf139 100644 --- a/core/src/main/java/org/nwapw/abacus/exception/DomainException.java +++ b/core/src/main/java/org/nwapw/abacus/exception/DomainException.java @@ -18,7 +18,7 @@ public class DomainException extends AbacusException { * Creates a new DomainException with a default message. */ public DomainException(){ - this(null); + this(""); } } diff --git a/core/src/main/java/org/nwapw/abacus/exception/EvaluationException.java b/core/src/main/java/org/nwapw/abacus/exception/EvaluationException.java index 4763aaf..5eed066 100644 --- a/core/src/main/java/org/nwapw/abacus/exception/EvaluationException.java +++ b/core/src/main/java/org/nwapw/abacus/exception/EvaluationException.java @@ -11,7 +11,7 @@ public class EvaluationException extends AbacusException { * Creates a new EvaluationException with the default string. */ public EvaluationException() { - this(null); + this(""); } /** diff --git a/core/src/main/kotlin/org/nwapw/abacus/function/applicable/Applicable.kt b/core/src/main/kotlin/org/nwapw/abacus/function/applicable/Applicable.kt index 5103d8b..0d64ba6 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/function/applicable/Applicable.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/function/applicable/Applicable.kt @@ -36,7 +36,8 @@ interface Applicable { * @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) } 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 e94ab46..7a80ca7 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/tree/NumberReducer.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/NumberReducer.kt @@ -18,14 +18,14 @@ class NumberReducer(val abacus: Abacus, context: EvaluationContext) : Reducer { 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 { 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 throw EvaluationException() + else -> throw EvaluationException("unrecognized tree node.") } }