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