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