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