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