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