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.repository.ArtifactRepository;
027
028import static java.util.Objects.requireNonNull;
029
030/**
031 * The result of a version resolution request.
032 *
033 * @see RepositorySystem#resolveVersion(org.eclipse.aether.RepositorySystemSession, VersionRequest)
034 */
035public final class VersionResult {
036
037    private final VersionRequest request;
038
039    private List<Exception> exceptions;
040
041    private String version;
042
043    private ArtifactRepository repository;
044
045    /**
046     * Creates a new result for the specified request.
047     *
048     * @param request The resolution request, must not be {@code null}.
049     */
050    public VersionResult(VersionRequest request) {
051        this.request = requireNonNull(request, "version request cannot be null");
052        exceptions = Collections.emptyList();
053    }
054
055    /**
056     * Gets the resolution request that was made.
057     *
058     * @return The resolution request, never {@code null}.
059     */
060    public VersionRequest getRequest() {
061        return request;
062    }
063
064    /**
065     * Gets the exceptions that occurred while resolving the version.
066     *
067     * @return The exceptions that occurred, never {@code null}.
068     */
069    public List<Exception> getExceptions() {
070        return exceptions;
071    }
072
073    /**
074     * Records the specified exception while resolving the version.
075     *
076     * @param exception The exception to record, may be {@code null}.
077     * @return This result for chaining, never {@code null}.
078     */
079    public VersionResult addException(Exception exception) {
080        if (exception != null) {
081            if (exceptions.isEmpty()) {
082                exceptions = new ArrayList<>();
083            }
084            exceptions.add(exception);
085        }
086        return this;
087    }
088
089    /**
090     * Gets the resolved version.
091     *
092     * @return The resolved version or {@code null} if the resolution failed.
093     */
094    public String getVersion() {
095        return version;
096    }
097
098    /**
099     * Sets the resolved version.
100     *
101     * @param version The resolved version, may be {@code null}.
102     * @return This result for chaining, never {@code null}.
103     */
104    public VersionResult setVersion(String version) {
105        this.version = version;
106        return this;
107    }
108
109    /**
110     * Gets the repository from which the version was eventually resolved.
111     *
112     * @return The repository from which the version was resolved or {@code null} if unknown.
113     */
114    public ArtifactRepository getRepository() {
115        return repository;
116    }
117
118    /**
119     * Sets the repository from which the version was resolved.
120     *
121     * @param repository The repository from which the version was resolved, may be {@code null}.
122     * @return This result for chaining, never {@code null}.
123     */
124    public VersionResult setRepository(ArtifactRepository repository) {
125        this.repository = repository;
126        return this;
127    }
128
129    @Override
130    public String toString() {
131        return getVersion() + " @ " + getRepository();
132    }
133}