From 9c77fa8aeb10420a9997e63c11377327cd93ce57 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 1 Sep 2017 18:23:44 -0700 Subject: [PATCH] Add a DomainException that avoids using null in functions. --- .../nwapw/abacus/number/DomainException.java | 24 +++++++++++++++++++ .../abacus/function/applicable/Applicable.kt | 7 +++--- .../function/applicable/ReducerApplicable.kt | 7 +++--- .../org/nwapw/abacus/fx/AbacusController.java | 2 ++ 4 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 core/src/main/java/org/nwapw/abacus/number/DomainException.java diff --git a/core/src/main/java/org/nwapw/abacus/number/DomainException.java b/core/src/main/java/org/nwapw/abacus/number/DomainException.java new file mode 100644 index 0000000..51a1769 --- /dev/null +++ b/core/src/main/java/org/nwapw/abacus/number/DomainException.java @@ -0,0 +1,24 @@ +package org.nwapw.abacus.number; + +/** + * Exception thrown if the function parameters do not match + * requirements. + */ +public class DomainException extends RuntimeException { + + /** + * Creates a new DomainException. + * @param reason the reason for which the exception is thrown. + */ + public DomainException(String reason) { + super(reason); + } + + /** + * Creates a new DomainException with a default message. + */ + public DomainException(){ + this("Domain Error"); + } + +} 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 69f0afc..dce1129 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 @@ -1,5 +1,6 @@ package org.nwapw.abacus.function.applicable +import org.nwapw.abacus.number.DomainException import org.nwapw.abacus.plugin.NumberImplementation /** @@ -25,7 +26,7 @@ interface Applicable { * @param params the parameters to apply to. * @return the result of the application. */ - fun applyInternal(implementation: NumberImplementation, params: Array): O? + fun applyInternal(implementation: NumberImplementation, params: Array): O /** * If the parameters can be used with this applicable, returns @@ -34,8 +35,8 @@ interface Applicable { * @param params the parameters to apply to. * @return the result of the operation, or null if parameters do not match. */ - fun apply(implementation: NumberImplementation, vararg params: T): O? { - if (!matchesParams(implementation, params)) return null + fun apply(implementation: NumberImplementation, vararg params: T): O { + if (!matchesParams(implementation, params)) throw DomainException() return applyInternal(implementation, params) } diff --git a/core/src/main/kotlin/org/nwapw/abacus/function/applicable/ReducerApplicable.kt b/core/src/main/kotlin/org/nwapw/abacus/function/applicable/ReducerApplicable.kt index bdb3276..9a80edc 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/function/applicable/ReducerApplicable.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/function/applicable/ReducerApplicable.kt @@ -1,5 +1,6 @@ package org.nwapw.abacus.function.applicable +import org.nwapw.abacus.number.DomainException import org.nwapw.abacus.plugin.NumberImplementation import org.nwapw.abacus.tree.Reducer @@ -27,7 +28,7 @@ interface ReducerApplicable { * @param params the arguments to apply to. * @return the result of the application. */ - fun applyWithReducerInternal(implementation: NumberImplementation, reducer: Reducer, params: Array): O? + fun applyWithReducerInternal(implementation: NumberImplementation, reducer: Reducer, params: Array): O /** * Applies this applicable to the given arguments, and reducer, @@ -36,8 +37,8 @@ interface ReducerApplicable { * @param params the arguments to apply to. * @return the result of the application, or null if the arguments are incompatible. */ - fun applyWithReducer(implementation: NumberImplementation, reducer: Reducer, vararg params: T): O? { - if (!matchesParams(implementation, params)) return null + fun applyWithReducer(implementation: NumberImplementation, reducer: Reducer, vararg params: T): O { + if (!matchesParams(implementation, params)) throw DomainException() return applyWithReducerInternal(implementation, reducer, params) } diff --git a/fx/src/main/java/org/nwapw/abacus/fx/AbacusController.java b/fx/src/main/java/org/nwapw/abacus/fx/AbacusController.java index 4f62247..45cdf76 100644 --- a/fx/src/main/java/org/nwapw/abacus/fx/AbacusController.java +++ b/fx/src/main/java/org/nwapw/abacus/fx/AbacusController.java @@ -152,6 +152,8 @@ public class AbacusController implements PluginListener { return resultingString; } catch (ComputationInterruptedException exception) { return ERR_STOP; + } catch (DomainException exception) { + return exception.getMessage(); } catch (RuntimeException exception) { exception.printStackTrace(); return ERR_EXCEPTION;