All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.maven.model.building.Result Maven / Gradle / Ivy

There is a newer version: 3.0.0-alpha-3
Show newest version
/*
 * 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.
 */
package org.apache.maven.model.building;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static java.util.Collections.singleton;
import static org.apache.maven.model.building.ModelProblem.Severity.ERROR;
import static org.apache.maven.model.building.ModelProblem.Severity.FATAL;

/**
 * There are various forms of results that are represented by this class:
 * 
    *
  1. success - in which case only the model field is set *
  2. success with warnings - model field + non-error model problems *
  3. error - no model, but diagnostics *
  4. error - (partial) model and diagnostics *
* Could encode these variants as subclasses, but kept in one for now * * @author bbusjaeger * @param */ public class Result { /** * Success without warnings * * @param model */ public static Result success(T model) { return success(model, Collections.emptyList()); } /** * Success with warnings * * @param model * @param problems */ public static Result success(T model, Iterable problems) { assert !hasErrors(problems); return new Result<>(false, model, problems); } /** * Success with warnings * * @param model * @param results */ public static Result success(T model, Result... results) { final List problemsList = new ArrayList<>(); for (Result result1 : results) { for (ModelProblem modelProblem : result1.getProblems()) { problemsList.add(modelProblem); } } return success(model, problemsList); } /** * Error with problems describing the cause * * @param problems */ public static Result error(Iterable problems) { return error(null, problems); } public static Result error(T model) { return error(model, Collections.emptyList()); } public static Result error(Result result) { return error(result.getProblems()); } public static Result error(Result... results) { final List problemsList = new ArrayList<>(); for (Result result1 : results) { for (ModelProblem modelProblem : result1.getProblems()) { problemsList.add(modelProblem); } } return error(problemsList); } /** * Error with partial result and problems describing the cause * * @param model * @param problems */ public static Result error(T model, Iterable problems) { return new Result<>(true, model, problems); } /** * New result - determine whether error or success by checking problems for errors * * @param model * @param problems */ public static Result newResult(T model, Iterable problems) { return new Result<>(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 */ public static Result addProblem(Result result, ModelProblem problem) { return addProblems(result, singleton(problem)); } /** * New result that includes the given * * @param result * @param problems */ public static Result addProblems(Result result, Iterable problems) { Collection list = new ArrayList<>(); for (ModelProblem item : problems) { list.add(item); } for (ModelProblem item : result.getProblems()) { list.add(item); } return new Result<>(result.hasErrors() || hasErrors(problems), result.get(), list); } public static Result addProblems(Result result, Result... results) { final List problemsList = new ArrayList<>(); for (Result result1 : results) { for (ModelProblem modelProblem : result1.getProblems()) { problemsList.add(modelProblem); } } return addProblems(result, problemsList); } /** * Turns the given results into a single result by combining problems and models into single collection. * * @param results */ public static Result> newResultSet(Iterable> results) { boolean hasErrors = false; List modelsList = new ArrayList<>(); List problemsList = new ArrayList<>(); for (Result result : results) { modelsList.add(result.get()); for (ModelProblem modelProblem : result.getProblems()) { problemsList.add(modelProblem); } if (result.hasErrors()) { hasErrors = true; } } return new Result<>(hasErrors, (Iterable) modelsList, problemsList); } // helper to determine if problems contain error private static boolean hasErrors(Iterable problems) { for (ModelProblem input : problems) { if (input.getSeverity().equals(ERROR) || input.getSeverity().equals(FATAL)) { return true; } } return false; } /** * Class definition */ private final boolean errors; private final T value; private final Iterable problems; private Result(boolean errors, T model, Iterable problems) { this.errors = errors; this.value = model; this.problems = problems; } public Iterable getProblems() { return problems; } public T get() { return value; } public boolean hasErrors() { return errors; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy