View Javadoc
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 static java.util.Objects.requireNonNull;
23  
24  import org.eclipse.aether.RepositorySystem;
25  import org.eclipse.aether.RepositorySystemSession;
26  import org.eclipse.aether.metadata.Metadata;
27  import org.eclipse.aether.transfer.MetadataNotFoundException;
28  
29  /**
30   * The result of a metadata resolution request.
31   * 
32   * @see RepositorySystem#resolveMetadata(RepositorySystemSession, java.util.Collection)
33   */
34  public final class MetadataResult
35  {
36  
37      private final MetadataRequest request;
38  
39      private Exception exception;
40  
41      private boolean updated;
42  
43      private Metadata metadata;
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 MetadataResult( MetadataRequest request )
51      {
52          this.request = requireNonNull( request, "metadata request cannot be null" );
53      }
54  
55      /**
56       * Gets the resolution request that was made.
57       *
58       * @return The resolution request, never {@code null}.
59       */
60      public MetadataRequest getRequest()
61      {
62          return request;
63      }
64  
65      /**
66       * Gets the resolved metadata (if any).
67       * 
68       * @return The resolved metadata or {@code null} if the resolution failed.
69       */
70      public Metadata getMetadata()
71      {
72          return metadata;
73      }
74  
75      /**
76       * Sets the resolved metadata.
77       * 
78       * @param metadata The resolved metadata, may be {@code null} if the resolution failed.
79       * @return This result for chaining, never {@code null}.
80       */
81      public MetadataResult setMetadata( Metadata metadata )
82      {
83          this.metadata = metadata;
84          return this;
85      }
86  
87      /**
88       * Records the specified exception while resolving the metadata.
89       * 
90       * @param exception The exception to record, may be {@code null}.
91       * @return This result for chaining, never {@code null}.
92       */
93      public MetadataResult setException( Exception exception )
94      {
95          this.exception = exception;
96          return this;
97      }
98  
99      /**
100      * Gets the exception that occurred while resolving the metadata.
101      * 
102      * @return The exception that occurred or {@code null} if none.
103      */
104     public Exception getException()
105     {
106         return exception;
107     }
108 
109     /**
110      * Sets the updated flag for the metadata.
111      * 
112      * @param updated {@code true} if the metadata was actually fetched from the remote repository during the
113      *            resolution, {@code false} if the metadata was resolved from a locally cached copy.
114      * @return This result for chaining, never {@code null}.
115      */
116     public MetadataResult setUpdated( boolean updated )
117     {
118         this.updated = updated;
119         return this;
120     }
121 
122     /**
123      * Indicates whether the metadata was actually fetched from the remote repository or resolved from the local cache.
124      * If metadata has been locally cached during a previous resolution request and this local copy is still up-to-date
125      * according to the remote repository's update policy, no remote access is made.
126      * 
127      * @return {@code true} if the metadata was actually fetched from the remote repository during the resolution,
128      *         {@code false} if the metadata was resolved from a locally cached copy.
129      */
130     public boolean isUpdated()
131     {
132         return updated;
133     }
134 
135     /**
136      * Indicates whether the requested metadata was resolved. Note that the metadata might have been successfully
137      * resolved (from the local cache) despite {@link #getException()} indicating a transfer error while trying to
138      * refetch the metadata from the remote repository.
139      * 
140      * @return {@code true} if the metadata was resolved, {@code false} otherwise.
141      * @see Metadata#getFile()
142      */
143     public boolean isResolved()
144     {
145         return getMetadata() != null && getMetadata().getFile() != null;
146     }
147 
148     /**
149      * Indicates whether the requested metadata is not present in the remote repository.
150      * 
151      * @return {@code true} if the metadata is not present in the remote repository, {@code false} otherwise.
152      */
153     public boolean isMissing()
154     {
155         return getException() instanceof MetadataNotFoundException;
156     }
157 
158     @Override
159     public String toString()
160     {
161         return getMetadata() + ( isUpdated() ? " (updated)" : " (cached)" );
162     }
163 
164 }