Throw the exception instead of returning null.

This commit is contained in:
Danila Fedorin 2017-09-21 23:05:48 -07:00
parent f385a48aa2
commit 3057f66e66
3 changed files with 31 additions and 6 deletions

View File

@ -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);
}
}

View File

@ -1,5 +1,6 @@
package org.nwapw.abacus.context package org.nwapw.abacus.context
import org.nwapw.abacus.exception.ContextException
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
/** /**
@ -16,14 +17,14 @@ import kotlin.reflect.KProperty
*/ */
class ChainSearchDelegate<out V>(private val valueGetter: EvaluationContext.() -> V?) { class ChainSearchDelegate<out V>(private val valueGetter: EvaluationContext.() -> V?) {
operator fun getValue(selfRef: Any, property: KProperty<*>): V? { operator fun getValue(selfRef: Any, property: KProperty<*>): V {
var currentRef = selfRef as? EvaluationContext ?: return null var currentRef = selfRef as EvaluationContext
var returnedValue = currentRef.valueGetter() var returnedValue = currentRef.valueGetter()
while (returnedValue == null) { while (returnedValue == null) {
currentRef = currentRef.parent ?: break currentRef = currentRef.parent ?: break
returnedValue = currentRef.valueGetter() returnedValue = currentRef.valueGetter()
} }
return returnedValue return returnedValue ?: throw ContextException()
} }
} }

View File

@ -43,13 +43,13 @@ open class EvaluationContext(val parent: EvaluationContext? = null,
/** /**
* The implementation inherited from this context's parent. * The implementation inherited from this context's parent.
*/ */
val inheritedNumberImplementation: NumberImplementation? val inheritedNumberImplementation: NumberImplementation
by ChainSearchDelegate { numberImplementation} by ChainSearchDelegate { numberImplementation }
/** /**
* The reducer inherited from this context's parent. * The reducer inherited from this context's parent.
*/ */
val inheritedReducer: Reducer<NumberInterface>? val inheritedReducer: Reducer<NumberInterface>
by ChainSearchDelegate { reducer } by ChainSearchDelegate { reducer }
/** /**