dburyak

Nested interfaces example

Nov 3rd, 2019
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 1.12 KB | None | 0 0
  1. class PipelineNodesGraph {
  2.     // nested groovy trait, same as nested interface in java
  3.     static trait NodeVisitor {
  4.         void enter(GraphNode node) {}
  5.         void visit(GraphNode node) {}
  6.         void exit(GraphNode node) {}
  7.     }
  8.     static class CycleDetector implements NodeVisitor {
  9.         // ... graph cycles detecting implementation ...
  10.     }
  11.  
  12.     private List<Node> findCycle() {
  13.         def cycleDetector = new CycleDetector()
  14.         def cycle = []
  15.         try {
  16.             traverseDepthFirst([cycleDetector])
  17.         } catch (CyclicConnectionException e) {
  18.             cycle = e.cycle
  19.         }
  20.         return cycle
  21.     }
  22. }
  23.  
  24. class NodeBuildingVisitor implements NodeVisitor {
  25.     void visit(GraphNode graphNode) {
  26.         if (!graphNode.node.assembled) {
  27.             graphNode.node.assemble()
  28.         }
  29.     }
  30. }
  31.  
  32. class Pipeline {
  33.     void build() {
  34.          // build all nodes
  35.         def graph = new PipelineNodesGraph(nodes, links)
  36.         def buildVisitor = new NodeBuildingVisitor(this)
  37.         graph.traverseDepthFirst([buildVisitor])
  38.         assert nodes.every { it.assembled }
  39.     }
  40. }
Add Comment
Please, Sign In to add comment