WarpPI/core/src/main/java/it/cavallium/warppi/math/FunctionSingle.java

133 lines
3.0 KiB
Java
Raw Normal View History

package it.cavallium.warppi.math;
import it.cavallium.warppi.math.rules.Rule;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.util.Objects;
public abstract class FunctionSingle implements Function {
2018-10-12 23:20:12 +02:00
private boolean simplified;
/**
* Create a new instance of FunctionSingle. The Math Context will be the
* same of <strong>value</strong>'s.
2018-09-22 11:17:30 +02:00
*
* @throws NullPointerException
* when value is null.
* @param value
* The parameter of this function.
*/
2018-09-22 11:17:30 +02:00
public FunctionSingle(final Function value) throws NullPointerException {
mathContext = value.getMathContext();
parameter = value;
}
2017-06-09 22:47:30 +02:00
/**
* Create a new instance of FunctionSingle.
2018-09-22 11:17:30 +02:00
*
2017-06-09 22:47:30 +02:00
* @param mathContext
* Math Context
*/
2018-09-22 11:17:30 +02:00
public FunctionSingle(final MathContext mathContext) {
2017-06-09 22:47:30 +02:00
this.mathContext = mathContext;
parameter = null;
}
/**
* Create a new instance of FunctionSingle.
2018-09-22 11:17:30 +02:00
*
* @param mathContext
* Math Context
* @param value
* The parameter of this function.
*/
2018-09-22 11:17:30 +02:00
public FunctionSingle(final MathContext mathContext, final Function value) {
this.mathContext = mathContext;
parameter = value;
}
protected final MathContext mathContext;
/**
* Function parameter.<br>
* <u>MUST NOT BE MODIFIED IF ALREADY SET UP.</u>
*/
protected Function parameter;
/**
2018-09-22 11:17:30 +02:00
*
* @return Parameter.
*/
public Function getParameter() {
return parameter;
}
/**
2018-09-22 11:17:30 +02:00
*
* @param value
* Parameter.
* @return A new instance of this function.
*/
2018-09-22 11:17:30 +02:00
public FunctionSingle setParameter(final Function value) {
final FunctionSingle s = clone();
s.parameter = value;
return s;
}
@Override
2018-09-22 11:17:30 +02:00
public FunctionSingle setParameter(final int index, final Function var) throws IndexOutOfBoundsException {
2018-09-28 11:39:28 +02:00
if (index == 0) {
return this.setParameter(var);
2018-09-28 11:39:28 +02:00
} else {
throw new IndexOutOfBoundsException();
2018-09-28 11:39:28 +02:00
}
}
@Override
2018-09-22 11:17:30 +02:00
public Function getParameter(final int index) throws IndexOutOfBoundsException {
2018-09-28 11:39:28 +02:00
if (index == 0) {
return this.getParameter();
2018-09-28 11:39:28 +02:00
} else {
throw new IndexOutOfBoundsException();
2018-09-28 11:39:28 +02:00
}
}
@Override
public MathContext getMathContext() {
return mathContext;
}
@Override
2018-09-22 11:17:30 +02:00
public final ObjectArrayList<Function> simplify(final Rule rule) throws Error, InterruptedException {
2018-05-12 21:18:29 +02:00
final ObjectArrayList<Function> simplifiedParam = parameter.simplify(rule);
2018-09-28 11:39:28 +02:00
if (simplifiedParam == null) {
2018-05-12 21:18:29 +02:00
return rule.execute(this);
2018-09-28 11:39:28 +02:00
}
2018-05-12 21:18:29 +02:00
final ObjectArrayList<Function> result = new ObjectArrayList<>();
2018-09-28 11:39:28 +02:00
for (final Function f : simplifiedParam) {
2017-12-22 22:39:58 +01:00
result.add(this.setParameter(f));
2018-09-28 11:39:28 +02:00
}
return result;
}
@Override
public abstract FunctionSingle clone();
@Override
public int hashCode() {
return Objects.hash(parameter);
}
@Override
public abstract boolean equals(Object o);
@Override
public String toString() {
return this.getClass().getSimpleName() + "(" + getParameter() + ")";
}
}