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.internal.impl.model;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.maven.api.services.BuilderProblem.Severity;
27  import org.apache.maven.api.services.ModelProblem;
28  
29  /**
30   * There are various forms of results that are represented by this class:
31   * <ol>
32   * <li>success - in which case only the model field is set
33   * <li>success with warnings - model field + non-error model problems
34   * <li>error - no model, but diagnostics
35   * <li>error - (partial) model and diagnostics
36   * </ol>
37   * Could encode these variants as subclasses, but kept in one for now
38   *
39   * @param <T> the model type
40   */
41  public class Result<T> {
42  
43      /**
44       * Success without warnings
45       *
46       * @param model
47       */
48      public static <T> Result<T> success(T model) {
49          return success(model, Collections.emptyList());
50      }
51  
52      /**
53       * Success with warnings
54       *
55       * @param model
56       * @param problems
57       */
58      public static <T> Result<T> success(T model, Iterable<? extends ModelProblem> problems) {
59          assert !hasErrors(problems);
60          return new Result<>(false, model, problems);
61      }
62  
63      /**
64       * Success with warnings
65       *
66       * @param model
67       * @param results
68       */
69      public static <T> Result<T> success(T model, Result<?>... results) {
70          final List<ModelProblem> problemsList = new ArrayList<>();
71  
72          for (Result<?> result1 : results) {
73              for (ModelProblem modelProblem : result1.getProblems()) {
74                  problemsList.add(modelProblem);
75              }
76          }
77  
78          return success(model, problemsList);
79      }
80  
81      /**
82       * Error with problems describing the cause
83       *
84       * @param problems
85       */
86      public static <T> Result<T> error(Iterable<? extends ModelProblem> problems) {
87          return error(null, problems);
88      }
89  
90      public static <T> Result<T> error(T model) {
91          return error(model, Collections.emptyList());
92      }
93  
94      public static <T> Result<T> error(Result<?> result) {
95          return error(result.getProblems());
96      }
97  
98      public static <T> Result<T> error(Result<?>... results) {
99          final List<ModelProblem> problemsList = new ArrayList<>();
100 
101         for (Result<?> result1 : results) {
102             for (ModelProblem modelProblem : result1.getProblems()) {
103                 problemsList.add(modelProblem);
104             }
105         }
106 
107         return error(problemsList);
108     }
109 
110     /**
111      * Error with partial result and problems describing the cause
112      *
113      * @param model
114      * @param problems
115      */
116     public static <T> Result<T> error(T model, Iterable<? extends ModelProblem> problems) {
117         return new Result<>(true, model, problems);
118     }
119 
120     /**
121      * New result - determine whether error or success by checking problems for errors
122      *
123      * @param model
124      * @param problems
125      */
126     public static <T> Result<T> newResult(T model, Iterable<? extends ModelProblem> problems) {
127         return new Result<>(hasErrors(problems), model, problems);
128     }
129 
130     /**
131      * New result consisting of given result and new problem. Convenience for newResult(result.get(),
132      * concat(result.getProblems(),problems)).
133      *
134      * @param result
135      * @param problem
136      */
137     public static <T> Result<T> addProblem(Result<T> result, ModelProblem problem) {
138         return addProblems(result, Collections.singleton(problem));
139     }
140 
141     /**
142      * New result that includes the given
143      *
144      * @param result
145      * @param problems
146      */
147     public static <T> Result<T> addProblems(Result<T> result, Iterable<? extends ModelProblem> problems) {
148         Collection<ModelProblem> list = new ArrayList<>();
149         for (ModelProblem item : problems) {
150             list.add(item);
151         }
152         for (ModelProblem item : result.getProblems()) {
153             list.add(item);
154         }
155         return new Result<>(result.hasErrors() || hasErrors(problems), result.get(), list);
156     }
157 
158     public static <T> Result<T> addProblems(Result<T> result, Result<?>... results) {
159         final List<ModelProblem> problemsList = new ArrayList<>();
160 
161         for (Result<?> result1 : results) {
162             for (ModelProblem modelProblem : result1.getProblems()) {
163                 problemsList.add(modelProblem);
164             }
165         }
166         return addProblems(result, problemsList);
167     }
168 
169     /**
170      * Turns the given results into a single result by combining problems and models into single collection.
171      *
172      * @param results
173      */
174     public static <T> Result<Iterable<T>> newResultSet(Iterable<? extends Result<? extends T>> results) {
175         boolean hasErrors = false;
176         List<T> modelsList = new ArrayList<>();
177         List<ModelProblem> problemsList = new ArrayList<>();
178 
179         for (Result<? extends T> result : results) {
180             modelsList.add(result.get());
181 
182             for (ModelProblem modelProblem : result.getProblems()) {
183                 problemsList.add(modelProblem);
184             }
185 
186             if (result.hasErrors()) {
187                 hasErrors = true;
188             }
189         }
190         return new Result<>(hasErrors, (Iterable<T>) modelsList, problemsList);
191     }
192 
193     // helper to determine if problems contain error
194     private static boolean hasErrors(Iterable<? extends ModelProblem> problems) {
195         for (ModelProblem input : problems) {
196             if (input.getSeverity().equals(Severity.ERROR)
197                     || input.getSeverity().equals(Severity.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 }