|
5 | 5 | import java.util.HashSet;
|
6 | 6 | import java.util.Map;
|
7 | 7 | import java.util.Set;
|
| 8 | +import java.util.stream.Collectors; |
8 | 9 |
|
9 | 10 | import io.fabric8.kubernetes.api.model.HasMetadata;
|
10 | 11 | import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
|
|
14 | 15 | @SuppressWarnings({"rawtypes", "unchecked"})
|
15 | 16 | public class WorkflowBuilder<P extends HasMetadata> {
|
16 | 17 |
|
17 |
| - private final Map<String, DependentResourceNode<?, P>> dependentResourceNodes = |
18 |
| - new HashMap<>(); |
19 |
| - private boolean throwExceptionAutomatically = THROW_EXCEPTION_AUTOMATICALLY_DEFAULT; |
| 18 | + private final Map<String, DependentResourceNode<?, P>> dependentResourceNodes; |
| 19 | + private boolean throwExceptionAutomatically; |
20 | 20 | private DependentResourceNode currentNode;
|
21 |
| - private boolean isCleaner = false; |
| 21 | + private boolean isCleaner; |
| 22 | + |
| 23 | + public WorkflowBuilder() { |
| 24 | + this(new HashMap<>(), THROW_EXCEPTION_AUTOMATICALLY_DEFAULT, false); |
| 25 | + } |
| 26 | + |
| 27 | + private WorkflowBuilder(Map<String, DependentResourceNode<?, P>> dependentResourceNodes, |
| 28 | + boolean throwExceptionAutomatically, boolean isCleaner) { |
| 29 | + this.dependentResourceNodes = dependentResourceNodes; |
| 30 | + this.throwExceptionAutomatically = throwExceptionAutomatically; |
| 31 | + this.isCleaner = isCleaner; |
| 32 | + } |
22 | 33 |
|
23 | 34 | public WorkflowBuilder<P> addDependentResource(DependentResource dependentResource) {
|
24 | 35 | return addDependentResource(dependentResource, null);
|
@@ -91,4 +102,12 @@ public Workflow<P> build() {
|
91 | 102 | return new DefaultWorkflow(new HashSet<>(dependentResourceNodes.values()),
|
92 | 103 | throwExceptionAutomatically, isCleaner);
|
93 | 104 | }
|
| 105 | + |
| 106 | + public static <P extends HasMetadata> WorkflowBuilder<P> from(Workflow<P> workflow) { |
| 107 | + final Map<String, DependentResourceNode<?, P>> nodes = |
| 108 | + workflow.getDependentResourcesByName().entrySet().stream() |
| 109 | + .collect(Collectors.toMap(Map.Entry::getKey, |
| 110 | + e -> new DependentResourceNode(e.getKey(), e.getValue()))); |
| 111 | + return new WorkflowBuilder<>(nodes, false, workflow.hasCleaner()); |
| 112 | + } |
94 | 113 | }
|
0 commit comments