001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.collection;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.List;
024
025import org.eclipse.aether.RepositorySystem;
026import org.eclipse.aether.graph.DependencyCycle;
027import org.eclipse.aether.graph.DependencyNode;
028
029import static java.util.Objects.requireNonNull;
030
031/**
032 * The result of a dependency collection request.
033 *
034 * @see RepositorySystem#collectDependencies(org.eclipse.aether.RepositorySystemSession, CollectRequest)
035 */
036public final class CollectResult {
037
038    private final CollectRequest request;
039
040    private List<Exception> exceptions;
041
042    private List<DependencyCycle> cycles;
043
044    private DependencyNode root;
045
046    /**
047     * Creates a new result for the specified request.
048     *
049     * @param request The resolution request, must not be {@code null}.
050     */
051    public CollectResult(CollectRequest request) {
052        this.request = requireNonNull(request, "dependency collection request cannot be null");
053        exceptions = Collections.emptyList();
054        cycles = Collections.emptyList();
055    }
056
057    /**
058     * Gets the collection request that was made.
059     *
060     * @return The collection request, never {@code null}.
061     */
062    public CollectRequest getRequest() {
063        return request;
064    }
065
066    /**
067     * Gets the exceptions that occurred while building the dependency graph.
068     *
069     * @return The exceptions that occurred, never {@code null}.
070     */
071    public List<Exception> getExceptions() {
072        return exceptions;
073    }
074
075    /**
076     * Records the specified exception while building the dependency graph.
077     *
078     * @param exception The exception to record, may be {@code null}.
079     * @return This result for chaining, never {@code null}.
080     */
081    public CollectResult addException(Exception exception) {
082        if (exception != null) {
083            if (exceptions.isEmpty()) {
084                exceptions = new ArrayList<>();
085            }
086            exceptions.add(exception);
087        }
088        return this;
089    }
090
091    /**
092     * Gets the dependency cycles that were encountered while building the dependency graph.
093     *
094     * @return The dependency cycles in the (raw) graph, never {@code null}.
095     */
096    public List<DependencyCycle> getCycles() {
097        return cycles;
098    }
099
100    /**
101     * Records the specified dependency cycle.
102     *
103     * @param cycle The dependency cycle to record, may be {@code null}.
104     * @return This result for chaining, never {@code null}.
105     */
106    public CollectResult addCycle(DependencyCycle cycle) {
107        if (cycle != null) {
108            if (cycles.isEmpty()) {
109                cycles = new ArrayList<>();
110            }
111            cycles.add(cycle);
112        }
113        return this;
114    }
115
116    /**
117     * Gets the root node of the dependency graph.
118     *
119     * @return The root node of the dependency graph or {@code null} if none.
120     */
121    public DependencyNode getRoot() {
122        return root;
123    }
124
125    /**
126     * Sets the root node of the dependency graph.
127     *
128     * @param root The root node of the dependency graph, may be {@code null}.
129     * @return This result for chaining, never {@code null}.
130     */
131    public CollectResult setRoot(DependencyNode root) {
132        this.root = root;
133        return this;
134    }
135
136    @Override
137    public String toString() {
138        return String.valueOf(getRoot());
139    }
140}