View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * There are various forms of results that are represented by this class:
32   * <ol>
33   * <li>success - in which case only the model field is set
34   * <li>success with warnings - model field + non-error model problems
35   * <li>error - no model, but diagnostics
36   * <li>error - (partial) model and diagnostics
37   * </ol>
38   * Could encode these variants as subclasses, but kept in one for now
39   *
40   * @author bbusjaeger
41   * @param <T>
42   */
43  public class Result<T> {
44  
45      /**
46       * Success without warnings
47       *
48       * @param model
49       */
50      public static <T> Result<T> success(T model) {
51          return success(model, Collections.<ModelProblem>emptyList());
52      }
53  
54      /**
55       * Success with warnings
56       *
57       * @param model
58       * @param problems
59       */
60      public static <T> Result<T> success(T model, Iterable<? extends ModelProblem> problems) {
61          assert !hasErrors(problems);
62          return new Result<>(false, model, problems);
63      }
64  
65      /**
66       * Success with warnings
67       *
68       * @param model
69       * @param results
70       */
71      public static <T> Result<T> success(T model, Result<?>... results) {
72          final List<ModelProblem> problemsList = new ArrayList<>();
73  
74          for (Result<?> result1 : results) {
75              for (ModelProblem modelProblem : result1.getProblems()) {
76                  problemsList.add(modelProblem);
77              }
78          }
79  
80          return success(model, problemsList);
81      }
82  
83      /**
84       * Error with problems describing the cause
85       *
86       * @param problems
87       */
88      public static <T> Result<T> error(Iterable<? extends ModelProblem> problems) {
89          return error(null, problems);
90      }
91  
92      public static <T> Result<T> error(T model) {
93          return error(model, Collections.<ModelProblem>emptyList());
94      }
95  
96      public static <T> Result<T> error(Result<?> result) {
97          return error(result.getProblems());
98      }
99  
100     public static <T> Result<T> error(Result<?>... results) {
101         final List<ModelProblem> problemsList = new ArrayList<>();
102 
103         for (Result<?> result1 : results) {
104             for (ModelProblem modelProblem : result1.getProblems()) {
105                 problemsList.add(modelProblem);
106             }
107         }
108 
109         return error(problemsList);
110     }
111 
112     /**
113      * Error with partial result and problems describing the cause
114      *
115      * @param model
116      * @param problems
117      */
118     public static <T> Result<T> error(T model, Iterable<? extends ModelProblem> problems) {
119         return new Result<>(true, model, problems);
120     }
121 
122     /**
123      * New result - determine whether error or success by checking problems for errors
124      *
125      * @param model
126      * @param problems
127      */
128     public static <T> Result<T> newResult(T model, Iterable<? extends ModelProblem> problems) {
129         return new Result<>(hasErrors(problems), model, problems);
130     }
131 
132     /**
133      * New result consisting of given result and new problem. Convenience for newResult(result.get(),
134      * concat(result.getProblems(),problems)).
135      *
136      * @param result
137      * @param problem
138      */
139     public static <T> Result<T> addProblem(Result<T> result, ModelProblem problem) {
140         return addProblems(result, singleton(problem));
141     }
142 
143     /**
144      * New result that includes the given
145      *
146      * @param result
147      * @param problems
148      */
149     public static <T> Result<T> addProblems(Result<T> result, Iterable<? extends ModelProblem> problems) {
150         Collection<ModelProblem> list = new ArrayList<>();
151         for (ModelProblem item : problems) {
152             list.add(item);
153         }
154         for (ModelProblem item : result.getProblems()) {
155             list.add(item);
156         }
157         return new Result<>(result.hasErrors() || hasErrors(problems), result.get(), list);
158     }
159 
160     public static <T> Result<T> addProblems(Result<T> result, Result<?>... results) {
161         final List<ModelProblem> problemsList = new ArrayList<>();
162 
163         for (Result<?> result1 : results) {
164             for (ModelProblem modelProblem : result1.getProblems()) {
165                 problemsList.add(modelProblem);
166             }
167         }
168         return addProblems(result, problemsList);
169     }
170 
171     /**
172      * Turns the given results into a single result by combining problems and models into single collection.
173      *
174      * @param results
175      */
176     public static <T> Result<Iterable<T>> newResultSet(Iterable<? extends Result<? extends T>> results) {
177         boolean hasErrors = false;
178         List<T> modelsList = new ArrayList<>();
179         List<ModelProblem> problemsList = new ArrayList<>();
180 
181         for (Result<? extends T> result : results) {
182             modelsList.add(result.get());
183 
184             for (ModelProblem modelProblem : result.getProblems()) {
185                 problemsList.add(modelProblem);
186             }
187 
188             if (result.hasErrors()) {
189                 hasErrors = true;
190             }
191         }
192         return new Result<>(hasErrors, (Iterable<T>) modelsList, problemsList);
193     }
194 
195     // helper to determine if problems contain error
196     private static boolean hasErrors(Iterable<? extends ModelProblem> problems) {
197         for (ModelProblem input : problems) {
198             if (input.getSeverity().equals(ERROR) || input.getSeverity().equals(FATAL)) {
199                 return true;
200             }
201         }
202         return false;
203     }
204 
205     /**
206      * Class definition
207      */
208     private final boolean errors;
209 
210     private final T value;
211 
212     private final Iterable<? extends ModelProblem> problems;
213 
214     private Result(boolean errors, T model, Iterable<? extends ModelProblem> problems) {
215         this.errors = errors;
216         this.value = model;
217         this.problems = problems;
218     }
219 
220     public Iterable<? extends ModelProblem> getProblems() {
221         return problems;
222     }
223 
224     public T get() {
225         return value;
226     }
227 
228     public boolean hasErrors() {
229         return errors;
230     }
231 }