Note that in your implementation of flattenInOrdeflattenInOrderr the if-clause if (tree.get().isLeft()) always returns, consequently the following "else"else is redundant and you should move the code in the else block outside (see code below).
Also note that the interface Function is pretty much java.util.function.Function provided in Java 8, so if you want to impress a potential employer for a Java job, learn and use Java 8 features wherever you can to demonstrate that you're technically at the forefront!
The following is your solution with the following changes:
- Java 8 Lambda expressions
- Added @Override annotation
- Removed unnecessary //end comments
- Removed redundant "else"
- Added final modifier where possible
/*
* @see flatten.FlattenTree#flattenInOrder(flatten.Tree)
*/
@Override
public List<T> flattenInOrder(final Tree<T> tree) {
if (tree == null)
throw new IllegalArgumentException("Tree is null.");
if (tree.get().isLeft()) {
// Java 8:
return Arrays.asList(tree.get().<T> ifLeft(p -> p));
}
return tree.get().ifRight(p -> {
final List<T> nodes = new ArrayList<>();
nodes.addAll(flattenInOrder(p.left()));
nodes.addAll(flattenInOrder(p.middle()));
nodes.addAll(flattenInOrder(p.right()));
return nodes; // return all fetched nodes
});
}