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