Skip to content

Commit

Permalink
javadoc and reorganized a few things
Browse files Browse the repository at this point in the history
  • Loading branch information
Tran-Antoine committed Mar 25, 2019
1 parent 145142f commit 61ae894
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
6 changes: 1 addition & 5 deletions src/main/java/net/akami/mask/operation/MaskOperator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
54 changes: 45 additions & 9 deletions src/main/java/net/akami/mask/tree/BinaryTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
/**
* A Binary tree handles branch splitting, evaluating and merging with the given behaviours. <br/> <br/>
* 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. <br/>
* 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. <br/>
* 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.
* <br/> <br/>
* 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 <T> what kind of branch will be handled by the tree.
* @author Antoine Tran
Expand All @@ -26,36 +26,69 @@ public abstract class BinaryTree<T extends Branch> implements Iterable<T> {
private List<T> 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. <br/>
* 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.
* @param self the current branch not split yet
* @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. <br/>
* If the finalResult method does not return an empty optional, the value found is returned <br/>
* 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
Expand All @@ -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();
Expand All @@ -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<String> 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();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/akami/mask/tree/Branch.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import net.akami.mask.utils.ExpressionUtils;

/**
*
* @param <T> must be the kind of branch itself, except if the children are different kind of branches
*/
public class Branch<T extends Branch> {

private T left;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/akami/mask/tree/CalculationTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down

0 comments on commit 61ae894

Please sign in to comment.