1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.building;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.List;
25
26 import static java.util.Collections.singleton;
27 import static org.apache.maven.model.building.ModelProblem.Severity.ERROR;
28 import static org.apache.maven.model.building.ModelProblem.Severity.FATAL;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class Result<T> {
44
45
46
47
48
49
50 public static <T> Result<T> success(T model) {
51 return success(model, Collections.<ModelProblem>emptyList());
52 }
53
54
55
56
57
58
59
60 public static <T> Result<T> success(T model, Iterable<? extends ModelProblem> problems) {
61 assert !hasErrors(problems);
62 return new Result<>(false, model, problems);
63 }
64
65
66
67
68
69
70
71 public static <T> Result<T> success(T model, Result<?>... results) {
72 final List<ModelProblem> problemsList = new ArrayList<>();
73
74 for (Result<?> result1 : results) {
75 for (ModelProblem modelProblem : result1.getProblems()) {
76 problemsList.add(modelProblem);
77 }
78 }
79
80 return success(model, problemsList);
81 }
82
83
84
85
86
87
88 public static <T> Result<T> error(Iterable<? extends ModelProblem> problems) {
89 return error(null, problems);
90 }
91
92 public static <T> Result<T> error(T model) {
93 return error(model, Collections.<ModelProblem>emptyList());
94 }
95
96 public static <T> Result<T> error(Result<?> result) {
97 return error(result.getProblems());
98 }
99
100 public static <T> Result<T> error(Result<?>... results) {
101 final List<ModelProblem> problemsList = new ArrayList<>();
102
103 for (Result<?> result1 : results) {
104 for (ModelProblem modelProblem : result1.getProblems()) {
105 problemsList.add(modelProblem);
106 }
107 }
108
109 return error(problemsList);
110 }
111
112
113
114
115
116
117
118 public static <T> Result<T> error(T model, Iterable<? extends ModelProblem> problems) {
119 return new Result<>(true, model, problems);
120 }
121
122
123
124
125
126
127
128 public static <T> Result<T> newResult(T model, Iterable<? extends ModelProblem> problems) {
129 return new Result<>(hasErrors(problems), model, problems);
130 }
131
132
133
134
135
136
137
138
139 public static <T> Result<T> addProblem(Result<T> result, ModelProblem problem) {
140 return addProblems(result, singleton(problem));
141 }
142
143
144
145
146
147
148
149 public static <T> Result<T> addProblems(Result<T> result, Iterable<? extends ModelProblem> problems) {
150 Collection<ModelProblem> list = new ArrayList<>();
151 for (ModelProblem item : problems) {
152 list.add(item);
153 }
154 for (ModelProblem item : result.getProblems()) {
155 list.add(item);
156 }
157 return new Result<>(result.hasErrors() || hasErrors(problems), result.get(), list);
158 }
159
160 public static <T> Result<T> addProblems(Result<T> result, Result<?>... results) {
161 final List<ModelProblem> problemsList = new ArrayList<>();
162
163 for (Result<?> result1 : results) {
164 for (ModelProblem modelProblem : result1.getProblems()) {
165 problemsList.add(modelProblem);
166 }
167 }
168 return addProblems(result, problemsList);
169 }
170
171
172
173
174
175
176 public static <T> Result<Iterable<T>> newResultSet(Iterable<? extends Result<? extends T>> results) {
177 boolean hasErrors = false;
178 List<T> modelsList = new ArrayList<>();
179 List<ModelProblem> problemsList = new ArrayList<>();
180
181 for (Result<? extends T> result : results) {
182 modelsList.add(result.get());
183
184 for (ModelProblem modelProblem : result.getProblems()) {
185 problemsList.add(modelProblem);
186 }
187
188 if (result.hasErrors()) {
189 hasErrors = true;
190 }
191 }
192 return new Result<>(hasErrors, (Iterable<T>) modelsList, problemsList);
193 }
194
195
196 private static boolean hasErrors(Iterable<? extends ModelProblem> problems) {
197 for (ModelProblem input : problems) {
198 if (input.getSeverity().equals(ERROR) || input.getSeverity().equals(FATAL)) {
199 return true;
200 }
201 }
202 return false;
203 }
204
205
206
207
208 private final boolean errors;
209
210 private final T value;
211
212 private final Iterable<? extends ModelProblem> problems;
213
214 private Result(boolean errors, T model, Iterable<? extends ModelProblem> problems) {
215 this.errors = errors;
216 this.value = model;
217 this.problems = problems;
218 }
219
220 public Iterable<? extends ModelProblem> getProblems() {
221 return problems;
222 }
223
224 public T get() {
225 return value;
226 }
227
228 public boolean hasErrors() {
229 return errors;
230 }
231 }