Move all find methods into graphFinder.
This commit is contained in:
parent
e1325615dc
commit
e3e06ec3cf
|
|
@ -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());
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue