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