WarpPI/core/src/main/java/it/cavallium/warppi/math/functions/Variable.java

142 lines
3.4 KiB
Java
Raw Normal View History

package it.cavallium.warppi.math.functions;
import it.cavallium.warppi.gui.expression.blocks.Block;
import it.cavallium.warppi.gui.expression.blocks.BlockChar;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.MathContext;
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 class Variable implements Function {
protected char var;
protected final MathContext root;
2017-07-24 23:11:29 +02:00
protected V_TYPE type = V_TYPE.CONSTANT;
2018-10-12 23:20:12 +02:00
private boolean simplified;
2018-09-22 11:17:30 +02:00
public Variable(final MathContext root, final char val, final V_TYPE type) {
2017-01-17 22:32:40 +01:00
this.root = root;
var = val;
this.type = type;
}
2018-09-22 11:17:30 +02:00
public Variable(final MathContext root, final String s, final V_TYPE type) throws Error {
this(root, s.charAt(0), type);
}
2018-10-04 23:39:19 +02:00
/**
* Copy
* @param old
* @param root
*/
public Variable(Variable old, MathContext root) {
this.root = root;
this.type = old.type;
this.var = old.var;
}
public char getChar() {
return var;
}
2018-09-22 11:17:30 +02:00
public Variable setChar(final char val) {
return new Variable(root, val, type);
}
public V_TYPE getType() {
return type;
}
2018-09-22 11:17:30 +02:00
public Variable setType(final V_TYPE typ) {
return new Variable(root, var, typ);
}
@Override
public String toString() {
return "" + getChar();
}
2018-10-04 23:39:19 +02:00
public static class VariableValue {
2017-01-16 17:57:09 +01:00
public final Variable v;
public final Number n;
2018-09-22 11:17:30 +02:00
public VariableValue(final Variable v, final Number n) {
2017-01-16 17:57:09 +01:00
this.v = v;
this.n = n;
}
2018-10-04 23:39:19 +02:00
/**
* Copy
* @param old
* @param newContext
*/
public VariableValue(VariableValue old, MathContext newContext) {
this.v = new Variable(old.v, newContext);
this.n = new Number(old.n, newContext);
}
2017-01-16 17:57:09 +01:00
}
2018-05-12 21:18:29 +02:00
@Override
2018-09-22 11:17:30 +02:00
public ObjectArrayList<Function> simplify(final Rule rule) throws Error, InterruptedException {
2017-12-22 22:39:58 +01:00
return rule.execute(this);
}
@Override
public int hashCode() {
return Objects.hash(var, type);
}
@Override
2018-09-22 11:17:30 +02:00
public boolean equals(final Object o) {
2018-09-28 11:39:28 +02:00
if (o instanceof Variable) {
return ((Variable) o).getChar() == var && ((Variable) o).getType() == type;
2018-09-28 11:39:28 +02:00
}
return false;
}
@Override
public MathContext getMathContext() {
2017-01-17 22:32:40 +01:00
return root;
}
@Override
public Variable clone() {
return new Variable(root, var, type);
}
2018-10-04 23:39:19 +02:00
@Override
public Variable clone(MathContext c) {
return new Variable(c, var, type);
}
public static enum V_TYPE {
2017-07-24 23:11:29 +02:00
CONSTANT, VARIABLE, SOLUTION
}
@Override
2018-09-22 11:17:30 +02:00
public Function setParameter(final int index, final Function var) throws IndexOutOfBoundsException {
throw new IndexOutOfBoundsException();
}
@Override
2018-09-22 11:17:30 +02:00
public Function getParameter(final int index) throws IndexOutOfBoundsException {
throw new IndexOutOfBoundsException();
}
@Override
2018-09-22 11:17:30 +02:00
public ObjectArrayList<Block> toBlock(final MathContext context) {
2018-05-12 21:18:29 +02:00
final ObjectArrayList<Block> result = new ObjectArrayList<>();
//TODO: Temporary solution. In near future Variables will be distint objects and they will have a color. So they will be no longer a BlockChar/FeatureChar
result.add(new BlockChar(getChar()));
return result;
}
@Override
public <Argument, Result> Result accept(final Function.Visitor<Argument, Result> visitor, final Argument argument) {
return visitor.visit(this, argument);
}
}