diff --git a/src/main/java/net/akami/mask/operation/MaskOperator.java b/src/main/java/net/akami/mask/operation/MaskOperator.java index ee3c507..915f698 100644 --- a/src/main/java/net/akami/mask/operation/MaskOperator.java +++ b/src/main/java/net/akami/mask/operation/MaskOperator.java @@ -4,20 +4,16 @@ import net.akami.mask.math.MaskExpression; import net.akami.mask.tree.DerivativeTree; import net.akami.mask.utils.ExpressionUtils; -import net.akami.mask.utils.MathUtils; import net.akami.mask.utils.ReducerFactory; -import java.util.List; import java.util.function.Consumer; public class MaskOperator { - public static final String NON_VARIABLES = "0123456789+-*=^."; private static final MaskOperator instance = new MaskOperator(); private MaskExpression mask; - - + /** * Gets the static instance of {@link MaskOperator}, though no {@link MaskExpression} will be used by default * @return the static instance of {@link MaskOperator} diff --git a/src/main/java/net/akami/mask/tree/BinaryTree.java b/src/main/java/net/akami/mask/tree/BinaryTree.java index 11b1862..4deb05b 100644 --- a/src/main/java/net/akami/mask/tree/BinaryTree.java +++ b/src/main/java/net/akami/mask/tree/BinaryTree.java @@ -11,11 +11,11 @@ /** * A Binary tree handles branch splitting, evaluating and merging with the given behaviours.

* When instantiating a BinaryTree, note that the splitting should automatically and instantly be performed - * in the {@link BinaryTree#create(Branch)}, starting off with the given branch.
- * This {@link BinaryTree#create(Branch)} method must define how a branch must be divided according to ALL the splitters, whereas + * in the {@link BinaryTree#begin(Branch)}, starting off with the given branch.
+ * This {@link BinaryTree#begin(Branch)} method must define how a branch must be divided according to ALL the splitters, whereas * {@link BinaryTree#split(Branch, char...)} defines how each branch must be divided, according to the splitter(s) given. *

- * In other words, the create method defines how and with which parameter the split method will be called. + * In other words, the begin method defines how and with which parameter the split method will be called. * * @param what kind of branch will be handled by the tree. * @author Antoine Tran @@ -26,19 +26,39 @@ public abstract class BinaryTree implements Iterable { private List branches; private char[] splitters; + /** + * Available constructor for any binary tree. Note that as soon as the tree is created, the splitting will + * automatically begin. + * @param expression the initial expression that forms the top of the tree + * @param splitters the chars used to split the tree, see {@link BinaryTree#begin(Branch)} for further information. + */ public BinaryTree(String expression, char... splitters) { this.branches = new ArrayList<>(); this.splitters = splitters; load(expression); } + /** + * Loads a new branch for the given expression. Note that {@link BinaryTree#generate(String)} should + * never be used outside binary classes, since it should only return a branch from the given expression, whereas + * the load() method performs necessary actions for splitting the tree. + * @param expression + * @return a branch created from the expression given + */ public T load(String expression) { T initial = generate(expression); branches.add(initial); - create(initial); + begin(initial); return initial; } + /** + * Defines how the splitting of a defined branch must be planned.
+ * In other words, begin must call the split() method one or more times according to the different splitters. + * @param self the branch itself + */ + protected abstract void begin(T self); + /** * Defines how each branch must be split. Note that checks concerning size / values of the char array are * usually not needed, since the user is supposed to know what the array must be. @@ -46,16 +66,29 @@ public T load(String expression) { * @param by the 'splitters', which are used to determine the left and right part of the branch */ protected abstract void split(T self, char... by); - protected abstract void create(T self); + + /** + * @param origin the string the branch must be based on + * @return a branch getting along with the tree, from the origin given + */ protected abstract T generate(String origin); /** + * Defines how a branch must be evaluated. * Note that if the branch type used hasn't redefined the canBeEvaluated method, you are guaranteed that * the branch has a left and a right part. - * @param self + * @param self the branch itself */ protected abstract void evalBranch(T self); + /** + * Merges the whole tree. The usual behavior is to go from the last branch to the first one, + * see if the current actually is calculable, and if yes then calls the {@link BinaryTree#evalBranch(Branch)} + * method.
+ * If the finalResult method does not return an empty optional, the value found is returned
+ * Note that the merge method can be redefined if the behavior does not suits the tree. + * @return the reduced value of the first branch, while finalResult() is not redefined + */ public String merge() { /* If we give a very simple expression such as '50' to the reducer, it will detect that no operation @@ -79,6 +112,8 @@ public String merge() { } evalBranch(self); + if(self.getReducedValue() != null && self.getReducedValue().equals("Infinity")) + throw new ArithmeticException("Infinity value found"); if(finalResult().isPresent()) { return finalResult().get(); @@ -87,12 +122,13 @@ public String merge() { return null; } + /** + * Defines whether the final result has been calculated or not. + * @return the final result if calculated, otherwise an empty optional- + */ public Optional finalResult() { T first = getBranches().get(0); if (first.hasReducedValue()) { - if (String.valueOf(first.getReducedValue()).equals("Infinity")) - throw new ArithmeticException(); - return Optional.of(String.valueOf(first.getReducedValue())); } return Optional.empty(); diff --git a/src/main/java/net/akami/mask/tree/Branch.java b/src/main/java/net/akami/mask/tree/Branch.java index def6816..8f52967 100644 --- a/src/main/java/net/akami/mask/tree/Branch.java +++ b/src/main/java/net/akami/mask/tree/Branch.java @@ -2,6 +2,10 @@ import net.akami.mask.utils.ExpressionUtils; +/** + * + * @param must be the kind of branch itself, except if the children are different kind of branches + */ public class Branch { private T left; diff --git a/src/main/java/net/akami/mask/tree/CalculationTree.java b/src/main/java/net/akami/mask/tree/CalculationTree.java index fc69429..163a1bc 100644 --- a/src/main/java/net/akami/mask/tree/CalculationTree.java +++ b/src/main/java/net/akami/mask/tree/CalculationTree.java @@ -11,7 +11,7 @@ public CalculationTree(String initial) { } @Override - protected void create(T self) { + protected void begin(T self) { for (int i = 0; i < getSplitters().length; i += 2) { split(self, getSplitters()[i], getSplitters()[i + 1]); }