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