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.repository;
020
021import java.util.Collections;
022import java.util.List;
023
024import org.eclipse.aether.artifact.Artifact;
025
026/**
027 * A query to the local repository for the existence of an artifact.
028 *
029 * @see LocalRepositoryManager#find(org.eclipse.aether.RepositorySystemSession, LocalArtifactRequest)
030 */
031public final class LocalArtifactRequest {
032
033    private Artifact artifact;
034
035    private String context = "";
036
037    private List<RemoteRepository> repositories = Collections.emptyList();
038
039    /**
040     * Creates an uninitialized query.
041     */
042    public LocalArtifactRequest() {
043        // enables default constructor
044    }
045
046    /**
047     * Creates a query with the specified properties.
048     *
049     * @param artifact The artifact to query for, may be {@code null}.
050     * @param repositories The remote repositories that should be considered as potential sources for the artifact, may
051     *            be {@code null} or empty to only consider locally installed artifacts.
052     * @param context The resolution context for the artifact, may be {@code null}.
053     */
054    public LocalArtifactRequest(Artifact artifact, List<RemoteRepository> repositories, String context) {
055        setArtifact(artifact);
056        setRepositories(repositories);
057        setContext(context);
058    }
059
060    /**
061     * Gets the artifact to query for.
062     *
063     * @return The artifact or {@code null} if not set.
064     */
065    public Artifact getArtifact() {
066        return artifact;
067    }
068
069    /**
070     * Sets the artifact to query for.
071     *
072     * @param artifact The artifact, may be {@code null}.
073     * @return This query for chaining, never {@code null}.
074     */
075    public LocalArtifactRequest setArtifact(Artifact artifact) {
076        this.artifact = artifact;
077        return this;
078    }
079
080    /**
081     * Gets the resolution context.
082     *
083     * @return The resolution context, never {@code null}.
084     */
085    public String getContext() {
086        return context;
087    }
088
089    /**
090     * Sets the resolution context.
091     *
092     * @param context The resolution context, may be {@code null}.
093     * @return This query for chaining, never {@code null}.
094     */
095    public LocalArtifactRequest setContext(String context) {
096        this.context = (context != null) ? context : "";
097        return this;
098    }
099
100    /**
101     * Gets the remote repositories to consider as sources of the artifact.
102     *
103     * @return The remote repositories, never {@code null}.
104     */
105    public List<RemoteRepository> getRepositories() {
106        return repositories;
107    }
108
109    /**
110     * Sets the remote repositories to consider as sources of the artifact.
111     *
112     * @param repositories The remote repositories, may be {@code null} or empty to only consider locally installed
113     *            artifacts.
114     * @return This query for chaining, never {@code null}.
115     */
116    public LocalArtifactRequest setRepositories(List<RemoteRepository> repositories) {
117        if (repositories != null) {
118            this.repositories = repositories;
119        } else {
120            this.repositories = Collections.emptyList();
121        }
122        return this;
123    }
124
125    @Override
126    public String toString() {
127        return getArtifact() + " @ " + getRepositories();
128    }
129}