Add a DomainException that avoids using null in functions.

This commit is contained in:
Danila Fedorin 2017-09-01 18:23:44 -07:00
parent 9ddfeb02cf
commit 9c77fa8aeb
4 changed files with 34 additions and 6 deletions

View File

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

View File

@ -1,5 +1,6 @@
package org.nwapw.abacus.function.applicable package org.nwapw.abacus.function.applicable
import org.nwapw.abacus.number.DomainException
import org.nwapw.abacus.plugin.NumberImplementation import org.nwapw.abacus.plugin.NumberImplementation
/** /**
@ -25,7 +26,7 @@ interface Applicable<in T : Any, out O : Any> {
* @param params the parameters to apply to. * @param params the parameters to apply to.
* @return the result of the application. * @return the result of the application.
*/ */
fun applyInternal(implementation: NumberImplementation, params: Array<out T>): O? fun applyInternal(implementation: NumberImplementation, params: Array<out T>): O
/** /**
* If the parameters can be used with this applicable, returns * If the parameters can be used with this applicable, returns
@ -34,8 +35,8 @@ interface Applicable<in T : Any, out O : Any> {
* @param params the parameters to apply to. * @param params the parameters to apply to.
* @return the result of the operation, or null if parameters do not match. * @return the result of the operation, or null if parameters do not match.
*/ */
fun apply(implementation: NumberImplementation, vararg params: T): O? { fun apply(implementation: NumberImplementation, vararg params: T): O {
if (!matchesParams(implementation, params)) return null if (!matchesParams(implementation, params)) throw DomainException()
return applyInternal(implementation, params) return applyInternal(implementation, params)
} }

View File

@ -1,5 +1,6 @@
package org.nwapw.abacus.function.applicable package org.nwapw.abacus.function.applicable
import org.nwapw.abacus.number.DomainException
import org.nwapw.abacus.plugin.NumberImplementation import org.nwapw.abacus.plugin.NumberImplementation
import org.nwapw.abacus.tree.Reducer import org.nwapw.abacus.tree.Reducer
@ -27,7 +28,7 @@ interface ReducerApplicable<in T : Any, out O : Any, in R : Any> {
* @param params the arguments to apply to. * @param params the arguments to apply to.
* @return the result of the application. * @return the result of the application.
*/ */
fun applyWithReducerInternal(implementation: NumberImplementation, reducer: Reducer<R>, params: Array<out T>): O? fun applyWithReducerInternal(implementation: NumberImplementation, reducer: Reducer<R>, params: Array<out T>): O
/** /**
* Applies this applicable to the given arguments, and reducer, * Applies this applicable to the given arguments, and reducer,
@ -36,8 +37,8 @@ interface ReducerApplicable<in T : Any, out O : Any, in R : Any> {
* @param params the arguments to apply to. * @param params the arguments to apply to.
* @return the result of the application, or null if the arguments are incompatible. * @return the result of the application, or null if the arguments are incompatible.
*/ */
fun applyWithReducer(implementation: NumberImplementation, reducer: Reducer<R>, vararg params: T): O? { fun applyWithReducer(implementation: NumberImplementation, reducer: Reducer<R>, vararg params: T): O {
if (!matchesParams(implementation, params)) return null if (!matchesParams(implementation, params)) throw DomainException()
return applyWithReducerInternal(implementation, reducer, params) return applyWithReducerInternal(implementation, reducer, params)
} }

View File

@ -152,6 +152,8 @@ public class AbacusController implements PluginListener {
return resultingString; return resultingString;
} catch (ComputationInterruptedException exception) { } catch (ComputationInterruptedException exception) {
return ERR_STOP; return ERR_STOP;
} catch (DomainException exception) {
return exception.getMessage();
} catch (RuntimeException exception) { } catch (RuntimeException exception) {
exception.printStackTrace(); exception.printStackTrace();
return ERR_EXCEPTION; return ERR_EXCEPTION;