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