Null MathOperator for better null safety.
@@ -21,7 +21,7 @@ public class JsonMath {
for(Entry<String, JsonElement> entry : mathObject.entrySet()) {
rootOp = processElement(entry.getKey(), entry.getValue());
- if(rootOp != null) {
+ if (rootOp != NULL_OPERATOR) {
return;
}
}
@@ -39,7 +39,7 @@ public class JsonMath {
return new Radius();
}
- return null;
+ return NULL_OPERATOR;
}
private MathOperator processElement(String key, JsonElement value) {
@@ -47,7 +47,7 @@ public class JsonMath {
MathFunction op = MathFunction.getFunction(key);
if (op == null)
- return null;
+ return NULL_OPERATOR;
ArrayList<MathOperator> paramList = new ArrayList<>();
Species speciesArg = Species.NULL_SPECIES;
@@ -55,7 +55,7 @@ public class JsonMath {
//If the value is an array then these are the parameters for this operation
if(value.isJsonArray()) {
for(JsonElement parameter : value.getAsJsonArray()) {
- MathOperator m = null;
+ MathOperator m = NULL_OPERATOR;
if(parameter.isJsonObject()) {
Entry<String, JsonElement> entry = parameter.getAsJsonObject().entrySet().iterator().next();
m = processElement(entry.getKey(), entry.getValue());
@@ -67,7 +67,7 @@ public class JsonMath {
if(parameter.getAsJsonPrimitive().isString()) {
String name = parameter.getAsString();
MathOperator var = getVariable(name);
- if(var != null) {
+ if(var != NULL_OPERATOR) {
m = var;
}
else if(TreeRegistry.findSpeciesSloppy(name) != Species.NULL_SPECIES) {
@@ -76,7 +76,7 @@ public class JsonMath {
}
}
- if(m != null) {
+ if(m != NULL_OPERATOR) {
paramList.add(m);
}
@@ -98,7 +98,7 @@ public class JsonMath {
case IFGT: return new IfGreaterThan(paramArray);
case SPECIES: return speciesArg != Species.NULL_SPECIES ? new IfSpecies(speciesArg, paramArray) : null;
case DEBUG: return new Debug(paramArray);
- default: return null;
+ default: return NULL_OPERATOR;
}
}
@@ -428,6 +428,15 @@ public class JsonMath {
}
+ public static final MathOperator NULL_OPERATOR = new Null();
+
+ private static class Null implements MathOperator {
+ @Override
+ public float apply(MathContext mc) {
+ return 0f;
+ }
+ }
+
public enum MathFunction {
CONST,
NOISE,
