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