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