aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/maven/apache-maven-3.3.3/maven-model-builder/src/main/java/org/apache/maven/model/building/Result.java
blob: a962897714fa2a7a23ba3937c951cb0a0bd54c30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package org.apache.maven.model.building;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import static com.google.common.base.Predicates.in;
import static com.google.common.collect.Iterables.any;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Iterables.transform;
import static java.util.Collections.singleton;
import static java.util.EnumSet.of;
import static org.apache.maven.model.building.ModelProblem.Severity.ERROR;
import static org.apache.maven.model.building.ModelProblem.Severity.FATAL;

import java.util.Arrays;
import java.util.Collections;

import org.apache.maven.model.building.ModelProblem.Severity;

import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;

/**
 * There are various forms of results that are represented by this class:
 * <ol>
 * <li>success - in which case only the model field is set
 * <li>success with warnings - model field + non-error model problems
 * <li>error - no model, but diagnostics
 * <li>error - (partial) model and diagnostics
 * </ol>
 * Could encode these variants as subclasses, but kept in one for now
 * 
 * @author bbusjaeger
 * @param <T>
 */
public class Result<T>
{

    /**
     * Success without warnings
     * 
     * @param model
     * @return
     */
    public static <T> Result<T> success( T model )
    {
        return success( model, Collections.<ModelProblem>emptyList() );
    }

    /**
     * Success with warnings
     * 
     * @param model
     * @param problems
     * @return
     */
    public static <T> Result<T> success( T model, Iterable<? extends ModelProblem> problems )
    {
        assert !hasErrors( problems );
        return new Result<T>( false, model, problems );
    }

    /**
     * Success with warnings
     * 
     * @param model
     * @param results
     * @return
     */
    public static <T> Result<T> success( T model, Result<?>... results )
    {
        return success( model, Iterables.concat( Iterables.transform( Arrays.asList( results ), GET_PROBLEMS ) ) );
    }

    /**
     * Error with problems describing the cause
     *
     * @param problems
     * @return
     */
    public static <T> Result<T> error( Iterable<? extends ModelProblem> problems )
    {
        return error( null, problems );
    }

    public static <T> Result<T> error( T model )
    {
        return error( model, Collections.<ModelProblem>emptyList() );
    }

    public static <T> Result<T> error( Result<?> result )
    {
        return error( result.getProblems() );
    }

    public static <T> Result<T> error( Result<?>... results )
    {
        return error( Iterables.concat( Iterables.transform( Arrays.asList( results ), GET_PROBLEMS ) ) );
    }

    /**
     * Error with partial result and problems describing the cause
     *
     * @param model
     * @param problems
     * @return
     */
    public static <T> Result<T> error( T model, Iterable<? extends ModelProblem> problems )
    {
        return new Result<T>( true, model, problems );
    }

    /**
     * New result - determine whether error or success by checking problems for errors
     * 
     * @param model
     * @param problems
     * @return
     */
    public static <T> Result<T> newResult( T model, Iterable<? extends ModelProblem> problems )
    {
        return new Result<T>( hasErrors( problems ), model, problems );
    }

    /**
     * New result consisting of given result and new problem. Convenience for newResult(result.get(),
     * concat(result.getProblems(),problems)).
     * 
     * @param result
     * @param problem
     * @return
     */
    public static <T> Result<T> addProblem( Result<T> result, ModelProblem problem )
    {
        return addProblems( result, singleton( problem ) );
    }

    /**
     * New result that includes the given
     *
     * @param result
     * @param problems
     * @return
     */
    public static <T> Result<T> addProblems( Result<T> result, Iterable<? extends ModelProblem> problems )
    {
        return new Result<T>( result.hasErrors() || hasErrors( problems ), result.get(), concat( result.getProblems(),
                                                                                                 problems ) );
    }

    public static <T> Result<T> addProblems( Result<T> result, Result<?>... results )
    {
        return addProblems( result, Iterables.concat( Iterables.transform( Arrays.asList( results ), GET_PROBLEMS ) ) );
    }

    /**
     * Turns the given results into a single result by combining problems and models into single collection.
     * 
     * @param results
     * @return
     */
    public static <T> Result<Iterable<T>> newResultSet( Iterable<? extends Result<? extends T>> results )
    {
        final boolean hasErrors = any( transform( results, new Function<Result<?>, Boolean>()
        {
            @Override
            public Boolean apply( Result<?> input )
            {
                return input.hasErrors();
            }
        } ), Predicates.equalTo( true ) );
        final Iterable<T> models = transform( results, new Function<Result<? extends T>, T>()
        {
            @Override
            public T apply( Result<? extends T> input )
            {
                return input.get();
            }
        } );
        final Iterable<ModelProblem> problems = concat( transform( results, GET_PROBLEMS ) );
        return new Result<Iterable<T>>( hasErrors, models, problems );
    }

    // helper to determine if problems contain error
    private static boolean hasErrors( Iterable<? extends ModelProblem> problems )
    {
        return any( transform( problems, new Function<ModelProblem, Severity>()
        {
            @Override
            public Severity apply( ModelProblem input )
            {
                return input.getSeverity();
            }
        } ), in( of( ERROR, FATAL ) ) );
    }

    /**
     * Class definition
     */

    private final boolean errors;

    private final T value;

    private final Iterable<? extends ModelProblem> problems;

    private Result( boolean errors, T model, Iterable<? extends ModelProblem> problems )
    {
        this.errors = errors;
        this.value = model;
        this.problems = problems;
    }

    public Iterable<? extends ModelProblem> getProblems()
    {
        return problems;
    }

    public T get()
    {
        return value;
    }

    public boolean hasErrors()
    {
        return errors;
    }

    private static final Function<Result<?>, Iterable<? extends ModelProblem>> GET_PROBLEMS =
        new Function<Result<?>, Iterable<? extends ModelProblem>>()
        {
            @Override
            public Iterable<? extends ModelProblem> apply( Result<?> input )
            {
                return input.getProblems();
            }
        };
}