1 package org.eclipse.aether.resolution;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import static java.util.Objects.requireNonNull;
26
27 import org.eclipse.aether.RepositorySystem;
28 import org.eclipse.aether.repository.ArtifactRepository;
29
30 /**
31 * The result of a version resolution request.
32 *
33 * @see RepositorySystem#resolveVersion(org.eclipse.aether.RepositorySystemSession, VersionRequest)
34 */
35 public final class VersionResult
36 {
37
38 private final VersionRequest request;
39
40 private List<Exception> exceptions;
41
42 private String version;
43
44 private ArtifactRepository repository;
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 VersionResult( VersionRequest request )
52 {
53 this.request = requireNonNull( request, "version request cannot be null" );
54 exceptions = Collections.emptyList();
55 }
56
57 /**
58 * Gets the resolution request that was made.
59 *
60 * @return The resolution request, never {@code null}.
61 */
62 public VersionRequest getRequest()
63 {
64 return request;
65 }
66
67 /**
68 * Gets the exceptions that occurred while resolving the version.
69 *
70 * @return The exceptions that occurred, never {@code null}.
71 */
72 public List<Exception> getExceptions()
73 {
74 return exceptions;
75 }
76
77 /**
78 * Records the specified exception while resolving the version.
79 *
80 * @param exception The exception to record, may be {@code null}.
81 * @return This result for chaining, never {@code null}.
82 */
83 public VersionResult addException( Exception exception )
84 {
85 if ( exception != null )
86 {
87 if ( exceptions.isEmpty() )
88 {
89 exceptions = new ArrayList<>();
90 }
91 exceptions.add( exception );
92 }
93 return this;
94 }
95
96 /**
97 * Gets the resolved version.
98 *
99 * @return The resolved version or {@code null} if the resolution failed.
100 */
101 public String getVersion()
102 {
103 return version;
104 }
105
106 /**
107 * Sets the resolved version.
108 *
109 * @param version The resolved version, may be {@code null}.
110 * @return This result for chaining, never {@code null}.
111 */
112 public VersionResult setVersion( String version )
113 {
114 this.version = version;
115 return this;
116 }
117
118 /**
119 * Gets the repository from which the version was eventually resolved.
120 *
121 * @return The repository from which the version was resolved or {@code null} if unknown.
122 */
123 public ArtifactRepository getRepository()
124 {
125 return repository;
126 }
127
128 /**
129 * Sets the repository from which the version was resolved.
130 *
131 * @param repository The repository from which the version was resolved, may be {@code null}.
132 * @return This result for chaining, never {@code null}.
133 */
134 public VersionResult setRepository( ArtifactRepository repository )
135 {
136 this.repository = repository;
137 return this;
138 }
139
140 @Override
141 public String toString()
142 {
143 return getVersion() + " @ " + getRepository();
144 }
145
146 }