1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.eclipse.aether.collection;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24
25 import org.eclipse.aether.RepositorySystem;
26 import org.eclipse.aether.graph.DependencyCycle;
27 import org.eclipse.aether.graph.DependencyNode;
28
29 import static java.util.Objects.requireNonNull;
30
31 /**
32 * The result of a dependency collection request.
33 *
34 * @see RepositorySystem#collectDependencies(org.eclipse.aether.RepositorySystemSession, CollectRequest)
35 */
36 public final class CollectResult {
37
38 private final CollectRequest request;
39
40 private List<Exception> exceptions;
41
42 private List<DependencyCycle> cycles;
43
44 private DependencyNode root;
45
46 /**
47 * Creates a new result for the specified request.
48 *
49 * @param request The resolution request, must not be {@code null}.
50 */
51 public CollectResult(CollectRequest request) {
52 this.request = requireNonNull(request, "dependency collection request cannot be null");
53 exceptions = Collections.emptyList();
54 cycles = Collections.emptyList();
55 }
56
57 /**
58 * Gets the collection request that was made.
59 *
60 * @return The collection request, never {@code null}.
61 */
62 public CollectRequest getRequest() {
63 return request;
64 }
65
66 /**
67 * Gets the exceptions that occurred while building the dependency graph.
68 *
69 * @return The exceptions that occurred, never {@code null}.
70 */
71 public List<Exception> getExceptions() {
72 return exceptions;
73 }
74
75 /**
76 * Records the specified exception while building the dependency graph.
77 *
78 * @param exception The exception to record, may be {@code null}.
79 * @return This result for chaining, never {@code null}.
80 */
81 public CollectResult addException(Exception exception) {
82 if (exception != null) {
83 if (exceptions.isEmpty()) {
84 exceptions = new ArrayList<>();
85 }
86 exceptions.add(exception);
87 }
88 return this;
89 }
90
91 /**
92 * Gets the dependency cycles that were encountered while building the dependency graph.
93 *
94 * @return The dependency cycles in the (raw) graph, never {@code null}.
95 */
96 public List<DependencyCycle> getCycles() {
97 return cycles;
98 }
99
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 }