mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-22 15:30:09 -08:00
Add a DomainException that avoids using null in functions.
This commit is contained in:
parent
9ddfeb02cf
commit
9c77fa8aeb
|
@ -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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user