Clover icon

sunshower-core

  1. Project Clover database Fri Apr 6 2018 03:27:42 UTC
  2. Package io.sunshower.service.task

File Tasks.java

 

Coverage histogram

../../../../img/srcFileCovDistChart0.png
60% of files have more coverage

Code metrics

10
61
14
3
143
119
19
0.31
4.36
4.67
1.36

Classes

Class Line # Actions
Tasks 8 7 5
0.00%
Tasks.Dependency 31 2 1
0.00%
Tasks.TaskGraphBuilder 41 52 13
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    package io.sunshower.service.task;
2   
3    import io.sunshower.common.Identifier;
4    import io.sunshower.persistence.core.DistributableEntity;
5    import java.util.*;
6   
7    /** Created by haswell on 3/26/17. */
 
8    public class Tasks {
9   
 
10  0 toggle public static TaskContext context() {
11  0 return new TaskContext();
12    }
13   
 
14  0 toggle public static <T> TaskContext.Binding<T> bind(T key) {
15  0 return context().bind(key);
16    }
17   
 
18  0 toggle public static TaskGraphBuilder newGraph() {
19  0 return new TaskGraphBuilder();
20    }
21   
 
22  0 toggle public static Node find(TaskGraph graph, String name) {
23  0 for (Node node : graph.getNodes()) {
24  0 if (name.equals(node.getName())) {
25  0 return node;
26    }
27    }
28  0 throw new NoSuchElementException(String.format("No node named %s was found", name));
29    }
30   
 
31    private static class Dependency {
32    final String dependentName;
33    final String dependencyName;
34   
 
35  0 toggle private Dependency(String dependentName, String dependencyName) {
36  0 this.dependentName = dependentName;
37  0 this.dependencyName = dependencyName;
38    }
39    }
40   
 
41    public static class TaskGraphBuilder {
42    private final String name;
43    private final Identifier id;
44   
45    private final Map<String, Node> namedNodes;
46    private final Map<Identifier, Node> nodes;
47    private final Stack<String> currentNodeNames;
48    private final Map<String, Set<Dependency>> dependencies;
49   
 
50  0 toggle public TaskGraphBuilder() {
51  0 this(DistributableEntity.sequence.next().toString(), DistributableEntity.sequence.next());
52    }
53   
 
54  0 toggle public TaskGraphBuilder(String name, Identifier id) {
55  0 this.id = id;
56  0 this.name = name;
57  0 this.nodes = new HashMap<>();
58  0 this.namedNodes = new HashMap<>();
59  0 this.dependencies = new HashMap<>();
60  0 this.currentNodeNames = new Stack<>();
61    }
62   
 
63  0 toggle public TaskGraphBuilder withValue(Object value) {
64  0 final String current = currentNodeNames.peek();
65  0 Node node = this.namedNodes.get(current);
66  0 node.setValue(value);
67  0 return this;
68    }
69   
 
70  0 toggle public TaskGraphBuilder bind(String s) {
71  0 final String current = currentNodeNames.peek();
72  0 Node node = this.namedNodes.get(current);
73  0 node.setKey(s);
74  0 return this;
75    }
76   
 
77  0 toggle public TaskGraphBuilder toContext(Object value) {
78  0 return withValue(value);
79    }
80   
 
81  0 toggle public TaskGraphBuilder named(String name) {
82  0 this.currentNodeNames.push(name);
83  0 return new TaskGraphBuilder(name, DistributableEntity.sequence.next());
84    }
85   
 
86  0 toggle public TaskGraphBuilder dependsOn(String... names) {
87  0 if (currentNodeNames.isEmpty()) {
88  0 throw new IllegalArgumentException("You must specify a task somewhere to depend on");
89    }
90  0 final String current = currentNodeNames.pop();
91  0 for (String dependency : names) {
92  0 this.dependencies
93    .computeIfAbsent(current, d -> new HashSet<>())
94    .add(new Dependency(dependency, current));
95    }
96  0 return this;
97    }
98   
 
99  0 toggle public TaskGraphBuilder task(String name) {
100  0 if (!namedNodes.containsKey(name)) {
101  0 Identifier id = DistributableEntity.sequence.next();
102  0 final Node node = new Node();
103  0 node.setId(id);
104  0 node.setName(name);
105  0 nodes.put(id, node);
106  0 namedNodes.put(name, node);
107    }
108  0 this.currentNodeNames.push(name);
109  0 return this;
110    }
111   
 
112  0 toggle public TaskGraph create() {
113  0 final TaskGraph taskGraph = new TaskGraph();
114   
115  0 Iterable<Dependency> deps =
116    () -> dependencies.values().stream().flatMap(Collection::stream).iterator();
117   
118  0 for (Dependency dep : deps) {
119  0 Node dependent = namedNodes.get(dep.dependentName);
120  0 if (dependent == null) {
121  0 task(dep.dependentName);
122  0 dependent = namedNodes.get(dep.dependencyName);
123    }
124  0 Node dependency = namedNodes.get(dep.dependencyName);
125   
126  0 if (dependency == null) {
127  0 task(dep.dependencyName);
128  0 dependency = namedNodes.get(dep.dependencyName);
129    }
130  0 final Edge edge = new Edge();
131  0 edge.setId(DistributableEntity.sequence.next());
132  0 edge.setSource(dependent.getId());
133  0 edge.setTarget(dependency.getId());
134  0 taskGraph.addEdge(edge);
135    }
136   
137  0 for (Node node : namedNodes.values()) {
138  0 taskGraph.addNode(node);
139    }
140  0 return taskGraph;
141    }
142    }
143    }