提交 e3e06ec3 编写于 作者: wu-sheng's avatar wu-sheng

Move all find methods into graphFinder.

上级 e1325615
......@@ -43,16 +43,6 @@ public final class Graph<INPUT> {
}
}
public Next findNext(int handlerId) {
Node node = nodeIndex.get(handlerId);
if (node == null) {
throw new NodeNotFoundException("Can't find node with handlerId="
+ handlerId
+ " in graph[" + id + "]");
}
return node.getNext();
}
void checkForNewNode(Node node) {
int nodeId = node.getHandler().id();
if (nodeIndex.containsKey(nodeId)) {
......@@ -63,11 +53,15 @@ public final class Graph<INPUT> {
nodeIndex.put(nodeId, node);
}
public GraphBuilder toBuilder(){
return new GraphBuilder(this);
public GraphNodeFinder toFinder() {
return new GraphNodeFinder(this);
}
ConcurrentHashMap<Integer, Node> getNodeIndex() {
return nodeIndex;
}
int getId() {
return id;
}
}
......@@ -23,21 +23,40 @@ import java.util.concurrent.ConcurrentHashMap;
/**
* @author wu-sheng
*/
public class GraphBuilder {
public class GraphNodeFinder {
private Graph graph;
GraphBuilder(Graph graph) {
GraphNodeFinder(Graph graph) {
this.graph = graph;
}
/**
* Find an exist node to build the graph.
*
* @param handlerId of specific node in graph.
* @param outputClass of the found node
* @param <NODE_OUTPUT> type of given output class
* @return Node instance.
*/
public <NODE_OUTPUT> Node<?, NODE_OUTPUT> findNode(int handlerId, Class<NODE_OUTPUT> outputClass) {
ConcurrentHashMap<Integer, Node> graphNodeIndex = graph.getNodeIndex();
Node node = graphNodeIndex.get(handlerId);
if (node == null) {
throw new NodeNotFoundException("Can't find node with handlerId="
+ handlerId
+ " in graph[" + handlerId + "]");
+ " in graph[" + graph.getId() + "]");
}
return node;
}
public Next findNext(int handlerId) {
ConcurrentHashMap<Integer, Node> graphNodeIndex = graph.getNodeIndex();
Node node = graphNodeIndex.get(handlerId);
if (node == null) {
throw new NodeNotFoundException("Can't find node with handlerId="
+ handlerId
+ " in graph[" + graph.getId() + "]");
}
return node.getNext();
}
}
......@@ -87,7 +87,7 @@ public class GraphManagerTest {
Graph<String> graph = GraphManager.INSTANCE.createIfAbsent(4, String.class);
graph.addNode(new Node1Processor()).addNext(new Node2Processor()).addNext(new Node4Processor());
Next next = GraphManager.INSTANCE.findGraph(4).findNext(2);
Next next = GraphManager.INSTANCE.findGraph(4).toFinder().findNext(2);
next.execute(123);
String output = outputStream.toString();
......@@ -102,7 +102,7 @@ public class GraphManagerTest {
Graph<String> graph = GraphManager.INSTANCE.createIfAbsent(5, String.class);
graph.addNode(new Node1Processor()).addNext(new Node2Processor()).addNext(new Node4Processor());
Next next = GraphManager.INSTANCE.findGraph(5).findNext(3);
Next next = GraphManager.INSTANCE.findGraph(5).toFinder().findNext(3);
}
@Test
......@@ -110,7 +110,7 @@ public class GraphManagerTest {
Graph<String> graph = GraphManager.INSTANCE.createIfAbsent(6, String.class);
graph.addNode(new Node1Processor()).addNext(new Node2Processor());
Node<?, Integer> foundNode = GraphManager.INSTANCE.findGraph(6).toBuilder().findNode(2, Integer.class);
Node<?, Integer> foundNode = GraphManager.INSTANCE.findGraph(6).toFinder().findNode(2, Integer.class);
foundNode.addNext(new Node4Processor());
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册