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