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