mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-22 07:20:09 -08:00
Throw the exception instead of returning null.
This commit is contained in:
parent
f385a48aa2
commit
3057f66e66
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -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 }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue
Block a user