001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.repository;
020
021import java.io.File;
022import java.nio.file.Path;
023
024import static java.util.Objects.requireNonNull;
025
026/**
027 * A result from the local repository about the existence of an artifact.
028 *
029 * @see LocalRepositoryManager#find(org.eclipse.aether.RepositorySystemSession, LocalArtifactRequest)
030 */
031public final class LocalArtifactResult {
032
033    private final LocalArtifactRequest request;
034
035    private Path path;
036
037    private boolean available;
038
039    private RemoteRepository repository;
040
041    /**
042     * Creates a new result for the specified request.
043     *
044     * @param request The local artifact request, must not be {@code null}.
045     */
046    public LocalArtifactResult(LocalArtifactRequest request) {
047        this.request = requireNonNull(request, "local artifact request cannot be null");
048    }
049
050    /**
051     * Gets the request corresponding to this result.
052     *
053     * @return The corresponding request, never {@code null}.
054     */
055    public LocalArtifactRequest getRequest() {
056        return request;
057    }
058
059    /**
060     * Gets the file to the requested artifact. Note that this file must not be used unless {@link #isAvailable()}
061     * returns {@code true}. An artifact file can be found but considered unavailable if the artifact was cached from a
062     * remote repository that is not part of the list of remote repositories used for the query.
063     *
064     * @return The file to the requested artifact or {@code null} if the artifact does not exist locally.
065     * @deprecated Use {@link #getPath()} instead.
066     */
067    @Deprecated
068    public File getFile() {
069        return path != null ? path.toFile() : null;
070    }
071
072    /**
073     * Gets the file to the requested artifact. Note that this file must not be used unless {@link #isAvailable()}
074     * returns {@code true}. An artifact file can be found but considered unavailable if the artifact was cached from a
075     * remote repository that is not part of the list of remote repositories used for the query.
076     *
077     * @return The file to the requested artifact or {@code null} if the artifact does not exist locally.
078     * @since 2.0.0
079     */
080    public Path getPath() {
081        return path;
082    }
083
084    /**
085     * Sets the file to requested artifact.
086     *
087     * @param file The artifact file, may be {@code null}.
088     * @return This result for chaining, never {@code null}.
089     * @deprecated Use {@link #setPath(Path)} instead.
090     */
091    @Deprecated
092    public LocalArtifactResult setFile(File file) {
093        return setPath(file != null ? file.toPath() : null);
094    }
095
096    /**
097     * Sets the file to requested artifact.
098     *
099     * @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}