View Javadoc
1   /*******************************************************************************
2    * Copyright (c) 2010, 2012 Sonatype, Inc.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *    Sonatype, Inc. - initial API and implementation
10   *******************************************************************************/
11  package org.eclipse.aether.resolution;
12  
13  import java.util.ArrayList;
14  import java.util.Collections;
15  import java.util.List;
16  
17  import org.eclipse.aether.RepositorySystem;
18  import org.eclipse.aether.RepositorySystemSession;
19  import org.eclipse.aether.repository.ArtifactRepository;
20  
21  /**
22   * The result of a version resolution request.
23   * 
24   * @see RepositorySystem#resolveVersion(RepositorySystemSession, VersionRequest)
25   */
26  public final class VersionResult
27  {
28  
29      private final VersionRequest request;
30  
31      private List<Exception> exceptions;
32  
33      private String version;
34  
35      private ArtifactRepository repository;
36  
37      /**
38       * Creates a new result for the specified request.
39       * 
40       * @param request The resolution request, must not be {@code null}.
41       */
42      public VersionResult( VersionRequest request )
43      {
44          if ( request == null )
45          {
46              throw new IllegalArgumentException( "version request has not been specified" );
47          }
48          this.request = request;
49          exceptions = Collections.emptyList();
50      }
51  
52      /**
53       * Gets the resolution request that was made.
54       * 
55       * @return The resolution request, never {@code null}.
56       */
57      public VersionRequest getRequest()
58      {
59          return request;
60      }
61  
62      /**
63       * Gets the exceptions that occurred while resolving the version.
64       * 
65       * @return The exceptions that occurred, never {@code null}.
66       */
67      public List<Exception> getExceptions()
68      {
69          return exceptions;
70      }
71  
72      /**
73       * Records the specified exception while resolving the version.
74       * 
75       * @param exception The exception to record, may be {@code null}.
76       * @return This result for chaining, never {@code null}.
77       */
78      public VersionResult addException( Exception exception )
79      {
80          if ( exception != null )
81          {
82              if ( exceptions.isEmpty() )
83              {
84                  exceptions = new ArrayList<Exception>();
85              }
86              exceptions.add( exception );
87          }
88          return this;
89      }
90  
91      /**
92       * Gets the resolved version.
93       * 
94       * @return The resolved version or {@code null} if the resolution failed.
95       */
96      public String getVersion()
97      {
98          return version;
99      }
100 
101     /**
102      * Sets the resolved version.
103      * 
104      * @param version The resolved version, may be {@code null}.
105      * @return This result for chaining, never {@code null}.
106      */
107     public VersionResult setVersion( String version )
108     {
109         this.version = version;
110         return this;
111     }
112 
113     /**
114      * Gets the repository from which the version was eventually resolved.
115      * 
116      * @return The repository from which the version was resolved or {@code null} if unknown.
117      */
118     public ArtifactRepository getRepository()
119     {
120         return repository;
121     }
122 
123     /**
124      * Sets the repository from which the version was resolved.
125      * 
126      * @param repository The repository from which the version was resolved, may be {@code null}.
127      * @return This result for chaining, never {@code null}.
128      */
129     public VersionResult setRepository( ArtifactRepository repository )
130     {
131         this.repository = repository;
132         return this;
133     }
134 
135     @Override
136     public String toString()
137     {
138         return getVersion() + " @ " + getRepository();
139     }
140 
141 }