Define FunctionVisitor and corresponding accept method

This commit is contained in:
Riccardo Azzolini 2018-10-06 10:31:34 +02:00
parent 75fe857139
commit 72de605659
27 changed files with 199 additions and 20 deletions

View File

@ -80,4 +80,13 @@ public interface Function {
* @throws Error
*/
ObjectArrayList<Block> toBlock(MathContext context) throws Error;
/**
* Accepts a <code>FunctionVisitor</code> by calling the correct overload of <code>visit</code>.
*
* @param visitor The visitor to be accepted.
* @param <T> The return type of the <code>visit</code> method.
* @return The value returned by <code>visit</code>.
*/
<T> T accept(FunctionVisitor<T> visitor);
}

View File

@ -0,0 +1,41 @@
package it.cavallium.warppi.math;
import it.cavallium.warppi.math.functions.*;
import it.cavallium.warppi.math.functions.Number;
import it.cavallium.warppi.math.functions.equations.Equation;
import it.cavallium.warppi.math.functions.equations.EquationsSystem;
import it.cavallium.warppi.math.functions.equations.EquationsSystemPart;
import it.cavallium.warppi.math.functions.trigonometry.*;
/**
* Executes a different overload of a method for each <code>Function</code> implementation.
*
* @param <T> The return type of all <code>visit</code> method overloads.
*/
public interface FunctionVisitor<T> {
T visit(ArcCosine arcCosine);
T visit(ArcSine arcSine);
T visit(ArcTangent arcTangent);
T visit(Cosine cosine);
T visit(Division division);
T visit(EmptyNumber emptyNumber);
T visit(Equation equation);
T visit(EquationsSystem equationsSystem);
T visit(EquationsSystemPart equationsSystemPart);
T visit(Expression expression);
T visit(Joke joke);
T visit(Logarithm logarithm);
T visit(Multiplication multiplication);
T visit(Negative negative);
T visit(Number number);
T visit(Power power);
T visit(Root root);
T visit(RootSquare rootSquare);
T visit(Sine sine);
T visit(Subtraction subtraction);
T visit(SumSubtraction sumSubtraction);
T visit(Sum sum);
T visit(Tangent tangent);
T visit(Undefined undefined);
T visit(Variable variable);
}

View File

@ -5,6 +5,7 @@ import it.cavallium.warppi.gui.expression.blocks.BlockContainer;
import it.cavallium.warppi.gui.expression.blocks.BlockDivision;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionOperator;
import it.cavallium.warppi.math.FunctionVisitor;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
@ -59,4 +60,9 @@ public class Division extends FunctionOperator {
result.add(bd);
return result;
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -2,6 +2,7 @@ package it.cavallium.warppi.math.functions;
import it.cavallium.warppi.gui.expression.blocks.Block;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionVisitor;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.rules.Rule;
import it.cavallium.warppi.util.Error;
@ -56,4 +57,9 @@ public class EmptyNumber implements Function {
return null;
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -5,6 +5,7 @@ import it.cavallium.warppi.gui.expression.blocks.BlockContainer;
import it.cavallium.warppi.gui.expression.blocks.BlockParenthesis;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionSingle;
import it.cavallium.warppi.math.FunctionVisitor;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.functions.trigonometry.ArcCosine;
import it.cavallium.warppi.math.functions.trigonometry.ArcSine;
@ -582,6 +583,11 @@ public class Expression extends FunctionSingle {
return result;
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
@Override
public String toString() {
String s = "(";

View File

@ -2,6 +2,7 @@ package it.cavallium.warppi.math.functions;
import it.cavallium.warppi.gui.expression.blocks.Block;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionVisitor;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.rules.Rule;
import it.cavallium.warppi.util.Error;
@ -61,4 +62,9 @@ public class Joke implements Function {
throw new Error(Errors.NOT_IMPLEMENTED, "Unknown function " + getClass().getSimpleName());
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -5,6 +5,7 @@ import it.cavallium.warppi.gui.expression.blocks.BlockContainer;
import it.cavallium.warppi.gui.expression.blocks.BlockLogarithm;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionOperator;
import it.cavallium.warppi.math.FunctionVisitor;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
@ -55,4 +56,9 @@ public class Logarithm extends FunctionOperator {
return result;
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -3,10 +3,7 @@ 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.gui.expression.blocks.BlockParenthesis;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionOperator;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.MathematicalSymbols;
import it.cavallium.warppi.math.*;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
@ -87,6 +84,11 @@ public class Multiplication extends FunctionOperator {
}
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
public boolean isNegative() {
return parameter1.equals(new Number(getMathContext(), -1)) || parameter2.equals(new Number(getMathContext(), -1));
}

View File

@ -3,10 +3,7 @@ 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.gui.expression.blocks.BlockParenthesis;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionSingle;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.MathematicalSymbols;
import it.cavallium.warppi.math.*;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
@ -52,4 +49,9 @@ public class Negative extends FunctionSingle {
return blocks;
// throw new Error(Errors.NOT_IMPLEMENTED, "Unknown function " + getClass().getSimpleName());
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -4,6 +4,7 @@ import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.LinkedList;
import it.cavallium.warppi.math.FunctionVisitor;
import org.nevec.rjm.BigDecimalMath;
import it.cavallium.warppi.gui.expression.blocks.Block;
@ -273,6 +274,11 @@ public class Number implements Function {
return result;
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
@Override
public Function setParameter(final int index, final Function var) throws IndexOutOfBoundsException {
throw new IndexOutOfBoundsException();

View File

@ -5,6 +5,7 @@ import it.cavallium.warppi.gui.expression.blocks.BlockContainer;
import it.cavallium.warppi.gui.expression.blocks.BlockPower;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionOperator;
import it.cavallium.warppi.math.FunctionVisitor;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
@ -50,4 +51,9 @@ public class Power extends FunctionOperator {
result.add(bp);
return result;
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -3,6 +3,7 @@ package it.cavallium.warppi.math.functions;
import it.cavallium.warppi.gui.expression.blocks.Block;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionOperator;
import it.cavallium.warppi.math.FunctionVisitor;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.util.Error;
import it.cavallium.warppi.util.Errors;
@ -39,4 +40,9 @@ public class Root extends FunctionOperator {
throw new Error(Errors.NOT_IMPLEMENTED, "Unknown function " + getClass().getSimpleName());
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -5,6 +5,7 @@ import it.cavallium.warppi.gui.expression.blocks.BlockContainer;
import it.cavallium.warppi.gui.expression.blocks.BlockSquareRoot;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionOperator;
import it.cavallium.warppi.math.FunctionVisitor;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
@ -48,4 +49,9 @@ public class RootSquare extends FunctionOperator {
return result;
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -2,10 +2,7 @@ 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.FunctionOperator;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.MathematicalSymbols;
import it.cavallium.warppi.math.*;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
@ -43,4 +40,9 @@ public class Subtraction extends FunctionOperator {
return result;
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -2,10 +2,7 @@ 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.FunctionOperator;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.MathematicalSymbols;
import it.cavallium.warppi.math.*;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
@ -47,4 +44,8 @@ public class Sum extends FunctionOperator {
return result;
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -2,10 +2,7 @@ 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.FunctionOperator;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.MathematicalSymbols;
import it.cavallium.warppi.math.*;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
@ -43,4 +40,9 @@ public class SumSubtraction extends FunctionOperator {
return result;
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -3,6 +3,7 @@ package it.cavallium.warppi.math.functions;
import it.cavallium.warppi.gui.expression.blocks.Block;
import it.cavallium.warppi.gui.expression.blocks.BlockUndefined;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionVisitor;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.rules.Rule;
import it.cavallium.warppi.util.Error;
@ -58,6 +59,11 @@ public class Undefined implements Function {
return result;
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
@Override
public String toString() {
return "UNDEFINED";

View File

@ -3,6 +3,7 @@ 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.FunctionVisitor;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.rules.Rule;
import it.cavallium.warppi.util.Error;
@ -130,4 +131,9 @@ public class Variable implements Function {
result.add(new BlockChar(getChar()));
return result;
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -7,6 +7,7 @@ import java.util.Set;
import it.cavallium.warppi.gui.expression.blocks.Block;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionOperator;
import it.cavallium.warppi.math.FunctionVisitor;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.solver.SolveMethod;
import it.cavallium.warppi.util.Error;
@ -68,4 +69,9 @@ public class Equation extends FunctionOperator {
return null;
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -3,6 +3,7 @@ package it.cavallium.warppi.math.functions.equations;
import it.cavallium.warppi.gui.expression.blocks.Block;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionDynamic;
import it.cavallium.warppi.math.FunctionVisitor;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
@ -46,4 +47,9 @@ public class EquationsSystem extends FunctionDynamic {
return null;
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -2,6 +2,7 @@ package it.cavallium.warppi.math.functions.equations;
import it.cavallium.warppi.gui.expression.blocks.Block;
import it.cavallium.warppi.math.FunctionSingle;
import it.cavallium.warppi.math.FunctionVisitor;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
@ -34,4 +35,9 @@ public class EquationsSystemPart extends FunctionSingle {
return null;
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -3,6 +3,7 @@ package it.cavallium.warppi.math.functions.trigonometry;
import it.cavallium.warppi.gui.expression.blocks.Block;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionSingle;
import it.cavallium.warppi.math.FunctionVisitor;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.util.Error;
import it.cavallium.warppi.util.Errors;
@ -38,4 +39,9 @@ public class ArcCosine extends FunctionSingle {
throw new Error(Errors.NOT_IMPLEMENTED, "Unknown function " + getClass().getSimpleName());
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -3,6 +3,7 @@ package it.cavallium.warppi.math.functions.trigonometry;
import it.cavallium.warppi.gui.expression.blocks.Block;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionSingle;
import it.cavallium.warppi.math.FunctionVisitor;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.util.Error;
import it.cavallium.warppi.util.Errors;
@ -38,4 +39,9 @@ public class ArcSine extends FunctionSingle {
throw new Error(Errors.NOT_IMPLEMENTED, "Unknown function " + getClass().getSimpleName());
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -3,6 +3,7 @@ package it.cavallium.warppi.math.functions.trigonometry;
import it.cavallium.warppi.gui.expression.blocks.Block;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionSingle;
import it.cavallium.warppi.math.FunctionVisitor;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.util.Error;
import it.cavallium.warppi.util.Errors;
@ -38,4 +39,9 @@ public class ArcTangent extends FunctionSingle {
throw new Error(Errors.NOT_IMPLEMENTED, "Unknown function " + getClass().getSimpleName());
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -3,6 +3,7 @@ package it.cavallium.warppi.math.functions.trigonometry;
import it.cavallium.warppi.gui.expression.blocks.Block;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionSingle;
import it.cavallium.warppi.math.FunctionVisitor;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.util.Error;
import it.cavallium.warppi.util.Errors;
@ -38,4 +39,9 @@ public class Cosine extends FunctionSingle {
throw new Error(Errors.NOT_IMPLEMENTED, "Unknown function " + getClass().getSimpleName());
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -5,6 +5,7 @@ import it.cavallium.warppi.gui.expression.blocks.BlockContainer;
import it.cavallium.warppi.gui.expression.blocks.BlockSine;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionSingle;
import it.cavallium.warppi.math.FunctionVisitor;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
@ -51,4 +52,9 @@ public class Sine extends FunctionSingle {
return result;
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}

View File

@ -3,6 +3,7 @@ package it.cavallium.warppi.math.functions.trigonometry;
import it.cavallium.warppi.gui.expression.blocks.Block;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionSingle;
import it.cavallium.warppi.math.FunctionVisitor;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
@ -37,4 +38,9 @@ public class Tangent extends FunctionSingle {
return null;
}
@Override
public <T> T accept(FunctionVisitor<T> visitor) {
return visitor.visit(this);
}
}