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