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 public static <T> Result<T> success( T model )
62 {
63 return success( model, Collections.<ModelProblem>emptyList() );
64 }
65
66
67
68
69
70
71
72 public static <T> Result<T> success( T model, Iterable<? extends ModelProblem> problems )
73 {
74 assert !hasErrors( problems );
75 return new Result<>( false, model, problems );
76 }
77
78
79
80
81
82
83
84 public static <T> Result<T> success( T model, Result<?>... results )
85 {
86 return success( model, Iterables.concat( Iterables.transform( Arrays.asList( results ), GET_PROBLEMS ) ) );
87 }
88
89
90
91
92
93
94 public static <T> Result<T> error( Iterable<? extends ModelProblem> problems )
95 {
96 return error( null, problems );
97 }
98
99 public static <T> Result<T> error( T model )
100 {
101 return error( model, Collections.<ModelProblem>emptyList() );
102 }
103
104 public static <T> Result<T> error( Result<?> result )
105 {
106 return error( result.getProblems() );
107 }
108
109 public static <T> Result<T> error( Result<?>... results )
110 {
111 return error( Iterables.concat( Iterables.transform( Arrays.asList( results ), GET_PROBLEMS ) ) );
112 }
113
114
115
116
117
118
119
120 public static <T> Result<T> error( T model, Iterable<? extends ModelProblem> problems )
121 {
122 return new Result<>( true, model, problems );
123 }
124
125
126
127
128
129
130
131 public static <T> Result<T> newResult( T model, Iterable<? extends ModelProblem> problems )
132 {
133 return new Result<>( hasErrors( problems ), model, problems );
134 }
135
136
137
138
139
140
141
142
143 public static <T> Result<T> addProblem( Result<T> result, ModelProblem problem )
144 {
145 return addProblems( result, singleton( problem ) );
146 }
147
148
149
150
151
152
153
154 public static <T> Result<T> addProblems( Result<T> result, Iterable<? extends ModelProblem> problems )
155 {
156 return new Result<>( result.hasErrors() || hasErrors( problems ), result.get(), concat( result.getProblems(),
157 problems ) );
158 }
159
160 public static <T> Result<T> addProblems( Result<T> result, Result<?>... results )
161 {
162 return addProblems( result, Iterables.concat( Iterables.transform( Arrays.asList( results ), GET_PROBLEMS ) ) );
163 }
164
165
166
167
168
169
170 public static <T> Result<Iterable<T>> newResultSet( Iterable<? extends Result<? extends T>> results )
171 {
172 final boolean hasErrors = any( transform( results, new Function<Result<?>, Boolean>()
173 {
174 @Override
175 public Boolean apply( Result<?> input )
176 {
177 return input.hasErrors();
178 }
179 } ), Predicates.equalTo( true ) );
180 final Iterable<T> models = transform( results, new Function<Result<? extends T>, T>()
181 {
182 @Override
183 public T apply( Result<? extends T> input )
184 {
185 return input.get();
186 }
187 } );
188 final Iterable<ModelProblem> problems = concat( transform( results, GET_PROBLEMS ) );
189 return new Result<>( hasErrors, models, problems );
190 }
191
192
193 private static boolean hasErrors( Iterable<? extends ModelProblem> problems )
194 {
195 return any( transform( problems, new Function<ModelProblem, Severity>()
196 {
197 @Override
198 public Severity apply( ModelProblem input )
199 {
200 return input.getSeverity();
201 }
202 } ), in( of( ERROR, FATAL ) ) );
203 }
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 {
217 this.errors = errors;
218 this.value = model;
219 this.problems = problems;
220 }
221
222 public Iterable<? extends ModelProblem> getProblems()
223 {
224 return problems;
225 }
226
227 public T get()
228 {
229 return value;
230 }
231
232 public boolean hasErrors()
233 {
234 return errors;
235 }
236
237 private static final Function<Result<?>, Iterable<? extends ModelProblem>> GET_PROBLEMS =
238 new Function<Result<?>, Iterable<? extends ModelProblem>>()
239 {
240 @Override
241 public Iterable<? extends ModelProblem> apply( Result<?> input )
242 {
243 return input.getProblems();
244 }
245 };
246 }