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;
022
023import static java.util.Objects.requireNonNull;
024
025/**
026 * A result from the local repository about the existence of an artifact.
027 *
028 * @see LocalRepositoryManager#find(org.eclipse.aether.RepositorySystemSession, LocalArtifactRequest)
029 */
030public final class LocalArtifactResult {
031
032    private final LocalArtifactRequest request;
033
034    private File file;
035
036    private boolean available;
037
038    private RemoteRepository repository;
039
040    /**
041     * Creates a new result for the specified request.
042     *
043     * @param request The local artifact request, must not be {@code null}.
044     */
045    public LocalArtifactResult(LocalArtifactRequest request) {
046        this.request = requireNonNull(request, "local artifact request cannot be null");
047    }
048
049    /**
050     * Gets the request corresponding to this result.
051     *
052     * @return The corresponding request, never {@code null}.
053     */
054    public LocalArtifactRequest getRequest() {
055        return request;
056    }
057
058    /**
059     * Gets the file to the requested artifact. Note that this file must not be used unless {@link #isAvailable()}
060     * returns {@code true}. An artifact file can be found but considered unavailable if the artifact was cached from a
061     * remote repository that is not part of the list of remote repositories used for the query.
062     *
063     * @return The file to the requested artifact or {@code null} if the artifact does not exist locally.
064     */
065    public File getFile() {
066        return file;
067    }
068
069    /**
070     * Sets the file to requested artifact.
071     *
072     * @param file The artifact file, may be {@code null}.
073     * @return This result for chaining, never {@code null}.
074     */
075    public LocalArtifactResult setFile(File file) {
076        this.file = file;
077        return this;
078    }
079
080    /**
081     * Indicates whether the requested artifact is available for use. As a minimum, the file needs to be physically
082     * existent in the local repository to be available. Additionally, a local repository manager can consider the list
083     * of supplied remote repositories to determine whether the artifact is logically available and mark an artifact
084     * unavailable (despite its physical existence) if it is not known to be hosted by any of the provided repositories.
085     *
086     * @return {@code true} if the artifact is available, {@code false} otherwise.
087     * @see LocalArtifactRequest#getRepositories()
088     */
089    public boolean isAvailable() {
090        return available;
091    }
092
093    /**
094     * Sets whether the artifact is available.
095     *
096     * @param available {@code true} if the artifact is available, {@code false} otherwise.
097     * @return This result for chaining, never {@code null}.
098     */
099    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}