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.resolution;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.List;
024
025import org.eclipse.aether.RepositorySystem;
026import org.eclipse.aether.artifact.Artifact;
027import org.eclipse.aether.repository.ArtifactRepository;
028import org.eclipse.aether.repository.LocalArtifactResult;
029import org.eclipse.aether.transfer.ArtifactNotFoundException;
030
031import static java.util.Objects.requireNonNull;
032
033/**
034 * The result of an artifact resolution request.
035 *
036 * @see RepositorySystem#resolveArtifacts(org.eclipse.aether.RepositorySystemSession, java.util.Collection)
037 * @see Artifact#getFile()
038 */
039public final class ArtifactResult {
040
041    private final ArtifactRequest request;
042
043    private List<Exception> exceptions;
044
045    private Artifact artifact;
046
047    private ArtifactRepository repository;
048
049    private LocalArtifactResult localArtifactResult;
050
051    /**
052     * Creates a new result for the specified request.
053     *
054     * @param request The resolution request, must not be {@code null}.
055     */
056    public ArtifactResult(ArtifactRequest request) {
057        this.request = requireNonNull(request, "artifact request cannot be null");
058        exceptions = Collections.emptyList();
059    }
060
061    /**
062     * Gets the resolution request that was made.
063     *
064     * @return The resolution request, never {@code null}.
065     */
066    public ArtifactRequest getRequest() {
067        return request;
068    }
069
070    /**
071     * Gets the resolved artifact (if any). Use {@link #getExceptions()} to query the errors that occurred while trying
072     * to resolve the artifact.
073     *
074     * @return The resolved artifact or {@code null} if the resolution failed.
075     */
076    public Artifact getArtifact() {
077        return artifact;
078    }
079
080    /**
081     * Sets the resolved artifact.
082     *
083     * @param artifact The resolved artifact, may be {@code null} if the resolution failed.
084     * @return This result for chaining, never {@code null}.
085     */
086    public ArtifactResult setArtifact(Artifact artifact) {
087        this.artifact = artifact;
088        return this;
089    }
090
091    /**
092     * Gets the exceptions that occurred while resolving the artifact. Note that this list can be non-empty even if the
093     * artifact was successfully resolved, e.g. when one of the contacted remote repositories didn't contain the
094     * artifact but a later repository eventually contained it.
095     *
096     * @return The exceptions that occurred, never {@code null}.
097     * @see #isResolved()
098     */
099    public List<Exception> getExceptions() {
100        return exceptions;
101    }
102
103    /**
104     * Records the specified exception while resolving the artifact.
105     *
106     * @param exception The exception to record, may be {@code null}.
107     * @return This result for chaining, never {@code null}.
108     */
109    public ArtifactResult addException(Exception exception) {
110        if (exception != null) {
111            if (exceptions.isEmpty()) {
112                exceptions = new ArrayList<>();
113            }
114            exceptions.add(exception);
115        }
116        return this;
117    }
118
119    /**
120     * Gets the repository from which the artifact was eventually resolved. Note that successive resolutions of the same
121     * artifact might yield different results if the employed local repository does not track the origin of an artifact.
122     *
123     * @return The repository from which the artifact was resolved or {@code null} if unknown.
124     */
125    public ArtifactRepository getRepository() {
126        return repository;
127    }
128
129    /**
130     * Sets the repository from which the artifact was resolved.
131     *
132     * @param repository The repository from which the artifact was resolved, may be {@code null}.
133     * @return This result for chaining, never {@code null}.
134     */
135    public ArtifactResult setRepository(ArtifactRepository repository) {
136        this.repository = repository;
137        return this;
138    }
139
140    /**
141     * Gets the {@link LocalArtifactResult} received during artifact resolution.
142     *
143     * @return The {@link LocalArtifactResult} or {@code null}.
144     * @since 1.9.6
145     */
146    public LocalArtifactResult getLocalArtifactResult() {
147        return localArtifactResult;
148    }
149
150    /**
151     * Sets the {@link LocalArtifactResult} that is received during artifact resolution.
152     *
153     * @param localArtifactResult The local artifact result.
154     * @since 1.9.6
155     */
156    public void setLocalArtifactResult(LocalArtifactResult localArtifactResult) {
157        this.localArtifactResult = localArtifactResult;
158    }
159
160    /**
161     * Indicates whether the requested artifact was resolved. Note that the artifact might have been successfully
162     * resolved despite {@link #getExceptions()} indicating transfer errors while trying to fetch the artifact from some
163     * of the specified remote repositories.
164     *
165     * @return {@code true} if the artifact was resolved, {@code false} otherwise.
166     * @see Artifact#getFile()
167     */
168    public boolean isResolved() {
169        return getArtifact() != null && getArtifact().getFile() != null;
170    }
171
172    /**
173     * Indicates whether the requested artifact is not present in any of the specified repositories.
174     *
175     * @return {@code true} if the artifact is not present in any repository, {@code false} otherwise.
176     */
177    public boolean isMissing() {
178        for (Exception e : getExceptions()) {
179            if (!(e instanceof ArtifactNotFoundException)) {
180                return false;
181            }
182        }
183        return !isResolved();
184    }
185
186    @Override
187    public String toString() {
188        return getArtifact() + " < " + getRepository();
189    }
190}