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;
025
026import org.eclipse.aether.RepositorySystem;
027import org.eclipse.aether.RepositorySystemSession;
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(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        if ( request == null )
055        {
056            throw new IllegalArgumentException( "dependency collection request has not been specified" );
057        }
058        this.request = request;
059        exceptions = Collections.emptyList();
060        cycles = Collections.emptyList();
061    }
062
063    /**
064     * Gets the collection request that was made.
065     * 
066     * @return The collection request, never {@code null}.
067     */
068    public CollectRequest getRequest()
069    {
070        return request;
071    }
072
073    /**
074     * Gets the exceptions that occurred while building the dependency graph.
075     * 
076     * @return The exceptions that occurred, never {@code null}.
077     */
078    public List<Exception> getExceptions()
079    {
080        return exceptions;
081    }
082
083    /**
084     * Records the specified exception while building the dependency graph.
085     * 
086     * @param exception The exception to record, may be {@code null}.
087     * @return This result for chaining, never {@code null}.
088     */
089    public CollectResult addException( Exception exception )
090    {
091        if ( exception != null )
092        {
093            if ( exceptions.isEmpty() )
094            {
095                exceptions = new ArrayList<Exception>();
096            }
097            exceptions.add( exception );
098        }
099        return this;
100    }
101
102    /**
103     * Gets the dependency cycles that were encountered while building the dependency graph.
104     * 
105     * @return The dependency cycles in the (raw) graph, never {@code null}.
106     */
107    public List<DependencyCycle> getCycles()
108    {
109        return cycles;
110    }
111
112    /**
113     * Records the specified dependency cycle.
114     * 
115     * @param cycle The dependency cycle to record, may be {@code null}.
116     * @return This result for chaining, never {@code null}.
117     */
118    public CollectResult addCycle( DependencyCycle cycle )
119    {
120        if ( cycle != null )
121        {
122            if ( cycles.isEmpty() )
123            {
124                cycles = new ArrayList<DependencyCycle>();
125            }
126            cycles.add( cycle );
127        }
128        return this;
129    }
130
131    /**
132     * Gets the root node of the dependency graph.
133     * 
134     * @return The root node of the dependency graph or {@code null} if none.
135     */
136    public DependencyNode getRoot()
137    {
138        return root;
139    }
140
141    /**
142     * Sets the root node of the dependency graph.
143     * 
144     * @param root The root node of the dependency graph, may be {@code null}.
145     * @return This result for chaining, never {@code null}.
146     */
147    public CollectResult setRoot( DependencyNode root )
148    {
149        this.root = root;
150        return this;
151    }
152
153    @Override
154    public String toString()
155    {
156        return String.valueOf( getRoot() );
157    }
158
159}