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;
023
024import org.eclipse.aether.RepositorySystemSession;
025
026/**
027 * A result from the local repository about the existence of an artifact.
028 * 
029 * @see LocalRepositoryManager#find(RepositorySystemSession, LocalArtifactRequest)
030 */
031public final class LocalArtifactResult
032{
033
034    private final LocalArtifactRequest request;
035
036    private File file;
037
038    private boolean available;
039
040    private RemoteRepository repository;
041
042    /**
043     * Creates a new result for the specified request.
044     * 
045     * @param request The local artifact request, must not be {@code null}.
046     */
047    public LocalArtifactResult( LocalArtifactRequest request )
048    {
049        if ( request == null )
050        {
051            throw new IllegalArgumentException( "local artifact request has not been specified" );
052        }
053        this.request = request;
054    }
055
056    /**
057     * Gets the request corresponding to this result.
058     * 
059     * @return The corresponding request, never {@code null}.
060     */
061    public LocalArtifactRequest getRequest()
062    {
063        return request;
064    }
065
066    /**
067     * Gets the file to the requested artifact. Note that this file must not be used unless {@link #isAvailable()}
068     * returns {@code true}. An artifact file can be found but considered unavailable if the artifact was cached from a
069     * remote repository that is not part of the list of remote repositories used for the query.
070     * 
071     * @return The file to the requested artifact or {@code null} if the artifact does not exist locally.
072     */
073    public File getFile()
074    {
075        return file;
076    }
077
078    /**
079     * Sets the file to requested artifact.
080     * 
081     * @param file The artifact file, may be {@code null}.
082     * @return This result for chaining, never {@code null}.
083     */
084    public LocalArtifactResult setFile( File file )
085    {
086        this.file = file;
087        return this;
088    }
089
090    /**
091     * Indicates whether the requested artifact is available for use. As a minimum, the file needs to be physically
092     * existent in the local repository to be available. Additionally, a local repository manager can consider the list
093     * of supplied remote repositories to determine whether the artifact is logically available and mark an artifact
094     * unavailable (despite its physical existence) if it is not known to be hosted by any of the provided repositories.
095     * 
096     * @return {@code true} if the artifact is available, {@code false} otherwise.
097     * @see LocalArtifactRequest#getRepositories()
098     */
099    public boolean isAvailable()
100    {
101        return available;
102    }
103
104    /**
105     * Sets whether the artifact is available.
106     * 
107     * @param available {@code true} if the artifact is available, {@code false} otherwise.
108     * @return This result for chaining, never {@code null}.
109     */
110    public LocalArtifactResult setAvailable( boolean available )
111    {
112        this.available = available;
113        return this;
114    }
115
116    /**
117     * Gets the (first) remote repository from which the artifact was cached (if any).
118     * 
119     * @return The remote repository from which the artifact was originally retrieved or {@code null} if unknown or if
120     *         the artifact has been locally installed.
121     * @see LocalArtifactRequest#getRepositories()
122     */
123    public RemoteRepository getRepository()
124    {
125        return repository;
126    }
127
128    /**
129     * Sets the (first) remote repository from which the artifact was cached.
130     * 
131     * @param repository The remote repository from which the artifact was originally retrieved, may be {@code null} if
132     *            unknown or if the artifact has been locally installed.
133     * @return This result for chaining, never {@code null}.
134     */
135    public LocalArtifactResult setRepository( RemoteRepository repository )
136    {
137        this.repository = repository;
138        return this;
139    }
140
141    @Override
142    public String toString()
143    {
144        return getFile() + " (" + ( isAvailable() ? "available" : "unavailable" ) + ")";
145    }
146
147}