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.repository;
20  
21  import java.io.File;
22  
23  import static java.util.Objects.requireNonNull;
24  
25  /**
26   * A result from the local repository about the existence of an artifact.
27   *
28   * @see LocalRepositoryManager#find(org.eclipse.aether.RepositorySystemSession, LocalArtifactRequest)
29   */
30  public final class LocalArtifactResult {
31  
32      private final LocalArtifactRequest request;
33  
34      private File file;
35  
36      private boolean available;
37  
38      private RemoteRepository repository;
39  
40      /**
41       * Creates a new result for the specified request.
42       *
43       * @param request The local artifact request, must not be {@code null}.
44       */
45      public LocalArtifactResult(LocalArtifactRequest request) {
46          this.request = requireNonNull(request, "local artifact request cannot be null");
47      }
48  
49      /**
50       * Gets the request corresponding to this result.
51       *
52       * @return The corresponding request, never {@code null}.
53       */
54      public LocalArtifactRequest getRequest() {
55          return request;
56      }
57  
58      /**
59       * Gets the file to the requested artifact. Note that this file must not be used unless {@link #isAvailable()}
60       * returns {@code true}. An artifact file can be found but considered unavailable if the artifact was cached from a
61       * remote repository that is not part of the list of remote repositories used for the query.
62       *
63       * @return The file to the requested artifact or {@code null} if the artifact does not exist locally.
64       */
65      public File getFile() {
66          return file;
67      }
68  
69      /**
70       * Sets the file to requested artifact.
71       *
72       * @param file The artifact file, may be {@code null}.
73       * @return This result for chaining, never {@code null}.
74       */
75      public LocalArtifactResult setFile(File file) {
76          this.file = file;
77          return this;
78      }
79  
80      /**
81       * Indicates whether the requested artifact is available for use. As a minimum, the file needs to be physically
82       * existent in the local repository to be available. Additionally, a local repository manager can consider the list
83       * of supplied remote repositories to determine whether the artifact is logically available and mark an artifact
84       * unavailable (despite its physical existence) if it is not known to be hosted by any of the provided repositories.
85       *
86       * @return {@code true} if the artifact is available, {@code false} otherwise.
87       * @see LocalArtifactRequest#getRepositories()
88       */
89      public boolean isAvailable() {
90          return available;
91      }
92  
93      /**
94       * Sets whether the artifact is available.
95       *
96       * @param available {@code true} if the artifact is available, {@code false} otherwise.
97       * @return This result for chaining, never {@code null}.
98       */
99      public LocalArtifactResult setAvailable(boolean available) {
100         this.available = available;
101         return this;
102     }
103 
104     /**
105      * Gets the (first) remote repository from which the artifact was cached (if any).
106      *
107      * @return The remote repository from which the artifact was originally retrieved or {@code null} if unknown or if
108      *         the artifact has been locally installed.
109      * @see LocalArtifactRequest#getRepositories()
110      */
111     public RemoteRepository getRepository() {
112         return repository;
113     }
114 
115     /**
116      * Sets the (first) remote repository from which the artifact was cached.
117      *
118      * @param repository The remote repository from which the artifact was originally retrieved, may be {@code null} if
119      *            unknown or if the artifact has been locally installed.
120      * @return This result for chaining, never {@code null}.
121      */
122     public LocalArtifactResult setRepository(RemoteRepository repository) {
123         this.repository = repository;
124         return this;
125     }
126 
127     @Override
128     public String toString() {
129         return getFile() + " (" + (isAvailable() ? "available" : "unavailable") + ")";
130     }
131 }