Untitled
unknown
java
10 months ago
1.4 kB
5
Indexable
List<Node> dbNodes = getDBNodes(); //getTerminalNodes(); or getNodesWithoutOutboundEdges(); for (Node dbNode : dbNodes) { Double result = traverse(dbNode); dbNode.apply(result); } public static Double traverse(Node node) { if (node == null) { return 0; } List<Node> childs = getChildsInPriorityOrder(node); if (childs.isEmpty()) { // or if(node instanceof NumericNode) return node.apply(); } List<Double> childResults = new ArrayList<>(); for (Node child : childs) { childResults.add(traverse(child)); } return node.apply(childResults); } abstract class Node { abstract Double apply(List<Double> inputs); } class SumNode extends Node { Double apply(List<Double> inputs) { assertThatHasTwoParameter(inputs); return inputs.get(0) + inputs.get(1); } } class NumericNode extends Node { Double value; .... Double apply(List<Double> inputs) { return value; } } class DivNode extends Node { Double apply(List<Double> inputs) { assertThatHasTwoParameter(inputs); assertThatSecondParameterIsNotNull(inputs); return inputs.get(0) / inputs.get(1); } } class DbNode extends Node { DbClient dbClient; .... Double apply(List<Double> inputs) { assertThatHasOneParameter(inputs); db.saveToDB(inputs.get(0)); } }
Editor is loading...
Leave a Comment