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