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