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