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;
025
026import org.eclipse.aether.RequestTrace;
027import org.eclipse.aether.artifact.Artifact;
028import org.eclipse.aether.repository.RemoteRepository;
029
030/**
031 * A request to resolve a version range.
032 * 
033 * @see org.eclipse.aether.RepositorySystem#resolveVersionRange(org.eclipse.aether.RepositorySystemSession,
034 *  VersionRangeRequest)
035 */
036public final class VersionRangeRequest
037{
038
039    private Artifact artifact;
040
041    private List<RemoteRepository> repositories = Collections.emptyList();
042
043    private String context = "";
044
045    private RequestTrace trace;
046
047    /**
048     * Creates an uninitialized request.
049     */
050    public VersionRangeRequest()
051    {
052        // enables default constructor
053    }
054
055    /**
056     * Creates a request with the specified properties.
057     * 
058     * @param artifact The artifact whose version range should be resolved, may be {@code null}.
059     * @param repositories The repositories to resolve the version from, may be {@code null}.
060     * @param context The context in which this request is made, may be {@code null}.
061     */
062    public VersionRangeRequest( Artifact artifact, List<RemoteRepository> repositories, String context )
063    {
064        setArtifact( artifact );
065        setRepositories( repositories );
066        setRequestContext( context );
067    }
068
069    /**
070     * Gets the artifact whose version range shall be resolved.
071     * 
072     * @return The artifact or {@code null} if not set.
073     */
074    public Artifact getArtifact()
075    {
076        return artifact;
077    }
078
079    /**
080     * Sets the artifact whose version range shall be resolved.
081     * 
082     * @param artifact The artifact, may be {@code null}.
083     * @return This request for chaining, never {@code null}.
084     */
085    public VersionRangeRequest setArtifact( Artifact artifact )
086    {
087        this.artifact = artifact;
088        return this;
089    }
090
091    /**
092     * Gets the repositories to resolve the version range from.
093     * 
094     * @return The repositories, never {@code null}.
095     */
096    public List<RemoteRepository> getRepositories()
097    {
098        return repositories;
099    }
100
101    /**
102     * Sets the repositories to resolve the version range from.
103     * 
104     * @param repositories The repositories, may be {@code null}.
105     * @return This request for chaining, never {@code null}.
106     */
107    public VersionRangeRequest setRepositories( List<RemoteRepository> repositories )
108    {
109        if ( repositories == null )
110        {
111            this.repositories = Collections.emptyList();
112        }
113        else
114        {
115            this.repositories = repositories;
116        }
117        return this;
118    }
119
120    /**
121     * Adds the specified repository for the resolution.
122     * 
123     * @param repository The repository to add, may be {@code null}.
124     * @return This request for chaining, never {@code null}.
125     */
126    public VersionRangeRequest addRepository( RemoteRepository repository )
127    {
128        if ( repository != null )
129        {
130            if ( this.repositories.isEmpty() )
131            {
132                this.repositories = new ArrayList<>();
133            }
134            this.repositories.add( repository );
135        }
136        return this;
137    }
138
139    /**
140     * Gets the context in which this request is made.
141     * 
142     * @return The context, never {@code null}.
143     */
144    public String getRequestContext()
145    {
146        return context;
147    }
148
149    /**
150     * Sets the context in which this request is made.
151     * 
152     * @param context The context, may be {@code null}.
153     * @return This request for chaining, never {@code null}.
154     */
155    public VersionRangeRequest setRequestContext( String context )
156    {
157        this.context = ( context != null ) ? context : "";
158        return this;
159    }
160
161    /**
162     * Gets the trace information that describes the higher level request/operation in which this request is issued.
163     * 
164     * @return The trace information about the higher level operation or {@code null} if none.
165     */
166    public RequestTrace getTrace()
167    {
168        return trace;
169    }
170
171    /**
172     * Sets the trace information that describes the higher level request/operation in which this request is issued.
173     * 
174     * @param trace The trace information about the higher level operation, may be {@code null}.
175     * @return This request for chaining, never {@code null}.
176     */
177    public VersionRangeRequest setTrace( RequestTrace trace )
178    {
179        this.trace = trace;
180        return this;
181    }
182
183    @Override
184    public String toString()
185    {
186        return getArtifact() + " < " + getRepositories();
187    }
188
189}