View Javadoc
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.resolution;
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.repository.ArtifactRepository;
27  
28  import static java.util.Objects.requireNonNull;
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      private final VersionRequest request;
38  
39      private List<Exception> exceptions;
40  
41      private String version;
42  
43      private ArtifactRepository repository;
44  
45      /**
46       * Creates a new result for the specified request.
47       *
48       * @param request The resolution request, must not be {@code null}.
49       */
50      public VersionResult(VersionRequest request) {
51          this.request = requireNonNull(request, "version request cannot be null");
52          exceptions = Collections.emptyList();
53      }
54  
55      /**
56       * Gets the resolution request that was made.
57       *
58       * @return The resolution request, never {@code null}.
59       */
60      public VersionRequest getRequest() {
61          return request;
62      }
63  
64      /**
65       * Gets the exceptions that occurred while resolving the version.
66       *
67       * @return The exceptions that occurred, never {@code null}.
68       */
69      public List<Exception> getExceptions() {
70          return exceptions;
71      }
72  
73      /**
74       * Records the specified exception while resolving the version.
75       *
76       * @param exception The exception to record, may be {@code null}.
77       * @return This result for chaining, never {@code null}.
78       */
79      public VersionResult addException(Exception exception) {
80          if (exception != null) {
81              if (exceptions.isEmpty()) {
82                  exceptions = new ArrayList<>();
83              }
84              exceptions.add(exception);
85          }
86          return this;
87      }
88  
89      /**
90       * Gets the resolved version.
91       *
92       * @return The resolved version or {@code null} if the resolution failed.
93       */
94      public String getVersion() {
95          return version;
96      }
97  
98      /**
99       * Sets the resolved version.
100      *
101      * @param version The resolved version, may be {@code null}.
102      * @return This result for chaining, never {@code null}.
103      */
104     public VersionResult setVersion(String version) {
105         this.version = version;
106         return this;
107     }
108 
109     /**
110      * Gets the repository from which the version was eventually resolved.
111      *
112      * @return The repository from which the version was resolved or {@code null} if unknown.
113      */
114     public ArtifactRepository getRepository() {
115         return repository;
116     }
117 
118     /**
119      * Sets the repository from which the version was resolved.
120      *
121      * @param repository The repository from which the version was resolved, may be {@code null}.
122      * @return This result for chaining, never {@code null}.
123      */
124     public VersionResult setRepository(ArtifactRepository repository) {
125         this.repository = repository;
126         return this;
127     }
128 
129     @Override
130     public String toString() {
131         return getVersion() + " @ " + getRepository();
132     }
133 }