1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.pipeline.validation;
19
20 import org.apache.commons.pipeline.Stage;
21
22
23
24
25
26 public class ValidationUtils {
27
28
29 private ValidationUtils() { }
30
31
32
33
34
35
36
37
38
39 public static final Boolean canSucceed(Stage upstream, Stage downstream) {
40 if (upstream.getClass().isAnnotationPresent(ProducedTypes.class) &&
41 downstream.getClass().isAnnotationPresent(ConsumedTypes.class)) {
42 ProducedTypes p = upstream.getClass().getAnnotation(ProducedTypes.class);
43 ConsumedTypes c = downstream.getClass().getAnnotation(ConsumedTypes.class);
44 return compatible(p.value(), c.value());
45 }
46
47 return null;
48 }
49
50
51
52
53
54
55
56
57
58
59 public static final Boolean canSucceedOnBranch(Stage upstream, String downstreamBranchKey, Stage downstream) {
60 if (downstream.getClass().isAnnotationPresent(ConsumedTypes.class)) {
61 ConsumedTypes c = downstream.getClass().getAnnotation(ConsumedTypes.class);
62 if (upstream.getClass().isAnnotationPresent(ProductionOnBranch.class)) {
63 ProductionOnBranch pob = upstream.getClass().getAnnotation(ProductionOnBranch.class);
64
65 if (!downstreamBranchKey.equals(pob.branchKey())) {
66 return false;
67 } else {
68 return compatible(pob.producedTypes(), c.value());
69 }
70 } else if (upstream.getClass().isAnnotationPresent(Branches.class)) {
71 Branches branches = upstream.getClass().getAnnotation(Branches.class);
72 for (ProductionOnBranch pob : branches.productionOnBranches()) {
73 if (downstreamBranchKey.equals(pob.branchKey())) {
74 return compatible(pob.producedTypes(), c.value());
75 }
76 }
77
78 return false;
79 }
80 }
81
82 return null;
83 }
84
85
86
87
88 private static Boolean compatible(Class<?>[] producedTypes, Class<?>[] consumedTypes) {
89 for (Class<?> consumed : consumedTypes) {
90 for (Class<?> produced : producedTypes) {
91 if (consumed.isAssignableFrom(produced)) return true;
92 }
93 }
94
95
96 return false;
97 }
98 }