From 3057f66e667414d653b5a8381f2a6ced81e8c10a Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 21 Sep 2017 23:05:48 -0700 Subject: [PATCH] Throw the exception instead of returning null. --- .../abacus/exception/ContextException.java | 24 +++++++++++++++++++ .../abacus/context/ChainSearchDelegate.kt | 7 +++--- .../nwapw/abacus/context/EvaluationContext.kt | 6 ++--- 3 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 core/src/main/java/org/nwapw/abacus/exception/ContextException.java diff --git a/core/src/main/java/org/nwapw/abacus/exception/ContextException.java b/core/src/main/java/org/nwapw/abacus/exception/ContextException.java new file mode 100644 index 0000000..dd2db0e --- /dev/null +++ b/core/src/main/java/org/nwapw/abacus/exception/ContextException.java @@ -0,0 +1,24 @@ +package org.nwapw.abacus.exception; + +/** + * Exception thrown by the Context in cases where lookup fails + * where it should not. + */ +public class ContextException extends AbacusException { + + /** + * Creates a new ContextException without an extra message. + */ + public ContextException() { + this(""); + } + + /** + * Creates a ContextException with the given message. + * @param message the message to use. + */ + public ContextException(String message){ + super("Context exception", message); + } + +} diff --git a/core/src/main/kotlin/org/nwapw/abacus/context/ChainSearchDelegate.kt b/core/src/main/kotlin/org/nwapw/abacus/context/ChainSearchDelegate.kt index b1245b9..64d933a 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/context/ChainSearchDelegate.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/context/ChainSearchDelegate.kt @@ -1,5 +1,6 @@ package org.nwapw.abacus.context +import org.nwapw.abacus.exception.ContextException import kotlin.reflect.KProperty /** @@ -16,14 +17,14 @@ import kotlin.reflect.KProperty */ class ChainSearchDelegate(private val valueGetter: EvaluationContext.() -> V?) { - operator fun getValue(selfRef: Any, property: KProperty<*>): V? { - var currentRef = selfRef as? EvaluationContext ?: return null + operator fun getValue(selfRef: Any, property: KProperty<*>): V { + var currentRef = selfRef as EvaluationContext var returnedValue = currentRef.valueGetter() while (returnedValue == null) { currentRef = currentRef.parent ?: break returnedValue = currentRef.valueGetter() } - return returnedValue + return returnedValue ?: throw ContextException() } } \ No newline at end of file diff --git a/core/src/main/kotlin/org/nwapw/abacus/context/EvaluationContext.kt b/core/src/main/kotlin/org/nwapw/abacus/context/EvaluationContext.kt index d4fdadd..293a5ff 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/context/EvaluationContext.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/context/EvaluationContext.kt @@ -43,13 +43,13 @@ open class EvaluationContext(val parent: EvaluationContext? = null, /** * The implementation inherited from this context's parent. */ - val inheritedNumberImplementation: NumberImplementation? - by ChainSearchDelegate { numberImplementation} + val inheritedNumberImplementation: NumberImplementation + by ChainSearchDelegate { numberImplementation } /** * The reducer inherited from this context's parent. */ - val inheritedReducer: Reducer? + val inheritedReducer: Reducer by ChainSearchDelegate { reducer } /**