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   * @param <T> the model type
41   */
42  public class Result<T> {
43  
44      /**
45       * Success without warnings
46       *
47       * @param model
48       */
49      public static <T> Result<T> success(T model) {
50          return success(model, Collections.emptyList());
51      }
52  
53      /**
54       * Success with warnings
55       *
56       * @param model
57       * @param problems
58       */
59      public static <T> Result<T> success(T model, Iterable<? extends ModelProblem> problems) {
60          assert !hasErrors(problems);
61          return new Result<>(false, model, problems);
62      }
63  
64      /**
65       * Success with warnings
66       *
67       * @param model
68       * @param results
69       */
70      public static <T> Result<T> success(T model, Result<?>... results) {
71          final List<ModelProblem> problemsList = new ArrayList<>();
72  
73          for (Result<?> result1 : results) {
74              for (ModelProblem modelProblem : result1.getProblems()) {
75                  problemsList.add(modelProblem);
76              }
77          }
78  
79          return success(model, problemsList);
80      }
81  
82      /**
83       * Error with problems describing the cause
84       *
85       * @param problems
86       */
87      public static <T> Result<T> error(Iterable<? extends ModelProblem> problems) {
88          return error(null, problems);
89      }
90  
91      public static <T> Result<T> error(T model) {
92          return error(model, Collections.emptyList());
93      }
94  
95      public static <T> Result<T> error(Result<?> result) {
96          return error(result.getProblems());
97      }
98  
99      public static <T> Result<T> error(Result<?>... results) {
100         final List<ModelProblem> problemsList = new ArrayList<>();
101 
102         for (Result<?> result1 : results) {
103             for (ModelProblem modelProblem : result1.getProblems()) {
104                 problemsList.add(modelProblem);
105             }
106         }
107 
108         return error(problemsList);
109     }
110 
111     /**
112      * Error with partial result and problems describing the cause
113      *
114      * @param model
115      * @param problems
116      */
117     public static <T> Result<T> error(T model, Iterable<? extends ModelProblem> problems) {
118         return new Result<>(true, model, problems);
119     }
120 
121     /**
122      * New result - determine whether error or success by checking problems for errors
123      *
124      * @param model
125      * @param problems
126      */
127     public static <T> Result<T> newResult(T model, Iterable<? extends ModelProblem> problems) {
128         return new Result<>(hasErrors(problems), model, problems);
129     }
130 
131     /**
132      * New result consisting of given result and new problem. Convenience for newResult(result.get(),
133      * concat(result.getProblems(),problems)).
134      *
135      * @param result
136      * @param problem
137      */
138     public static <T> Result<T> addProblem(Result<T> result, ModelProblem problem) {
139         return addProblems(result, singleton(problem));
140     }
141 
142     /**
143      * New result that includes the given
144      *
145      * @param result
146      * @param problems
147      */
148     public static <T> Result<T> addProblems(Result<T> result, Iterable<? extends ModelProblem> problems) {
149         Collection<ModelProblem> list = new ArrayList<>();
150         for (ModelProblem item : problems) {
151             list.add(item);
152         }
153         for (ModelProblem item : result.getProblems()) {
154             list.add(item);
155         }
156         return new Result<>(result.hasErrors() || hasErrors(problems), result.get(), list);
157     }
158 
159     public static <T> Result<T> addProblems(Result<T> result, Result<?>... results) {
160         final List<ModelProblem> problemsList = new ArrayList<>();
161 
162         for (Result<?> result1 : results) {
163             for (ModelProblem modelProblem : result1.getProblems()) {
164                 problemsList.add(modelProblem);
165             }
166         }
167         return addProblems(result, problemsList);
168     }
169 
170     /**
171      * Turns the given results into a single result by combining problems and models into single collection.
172      *
173      * @param results
174      */
175     public static <T> Result<Iterable<T>> newResultSet(Iterable<? extends Result<? extends T>> results) {
176         boolean hasErrors = false;
177         List<T> modelsList = new ArrayList<>();
178         List<ModelProblem> problemsList = new ArrayList<>();
179 
180         for (Result<? extends T> result : results) {
181             modelsList.add(result.get());
182 
183             for (ModelProblem modelProblem : result.getProblems()) {
184                 problemsList.add(modelProblem);
185             }
186 
187             if (result.hasErrors()) {
188                 hasErrors = true;
189             }
190         }
191         return new Result<>(hasErrors, (Iterable<T>) modelsList, problemsList);
192     }
193 
194     // helper to determine if problems contain error
195     private static boolean hasErrors(Iterable<? extends ModelProblem> problems) {
196         for (ModelProblem input : problems) {
197             if (input.getSeverity().equals(ERROR) || input.getSeverity().equals(FATAL)) {
198                 return true;
199             }
200         }
201         return false;
202     }
203 
204     /**
205      * Class definition
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 }