001package org.eclipse.aether.installation;
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.Collection;
024import java.util.Collections;
025
026import org.eclipse.aether.RepositorySystem;
027import org.eclipse.aether.RepositorySystemSession;
028import org.eclipse.aether.artifact.Artifact;
029import org.eclipse.aether.metadata.Metadata;
030
031/**
032 * The result of installing artifacts and their accompanying metadata into the a remote repository.
033 * 
034 * @see RepositorySystem#install(RepositorySystemSession, InstallRequest)
035 */
036public final class InstallResult
037{
038
039    private final InstallRequest request;
040
041    private Collection<Artifact> artifacts;
042
043    private Collection<Metadata> metadata;
044
045    /**
046     * Creates a new result for the specified request.
047     * 
048     * @param request The installation request, must not be {@code null}.
049     */
050    public InstallResult( InstallRequest request )
051    {
052        if ( request == null )
053        {
054            throw new IllegalArgumentException( "install request has not been specified" );
055        }
056        this.request = request;
057        artifacts = Collections.emptyList();
058        metadata = Collections.emptyList();
059    }
060
061    /**
062     * Gets the install request that was made.
063     * 
064     * @return The install request, never {@code null}.
065     */
066    public InstallRequest getRequest()
067    {
068        return request;
069    }
070
071    /**
072     * Gets the artifacts that got installed.
073     * 
074     * @return The installed artifacts, never {@code null}.
075     */
076    public Collection<Artifact> getArtifacts()
077    {
078        return artifacts;
079    }
080
081    /**
082     * Sets the artifacts that got installed.
083     * 
084     * @param artifacts The installed artifacts, may be {@code null}.
085     * @return This result for chaining, never {@code null}.
086     */
087    public InstallResult setArtifacts( Collection<Artifact> artifacts )
088    {
089        if ( artifacts == null )
090        {
091            this.artifacts = Collections.emptyList();
092        }
093        else
094        {
095            this.artifacts = artifacts;
096        }
097        return this;
098    }
099
100    /**
101     * Adds the specified artifacts to the result.
102     * 
103     * @param artifact The installed artifact to add, may be {@code null}.
104     * @return This result for chaining, never {@code null}.
105     */
106    public InstallResult addArtifact( Artifact artifact )
107    {
108        if ( artifact != null )
109        {
110            if ( artifacts.isEmpty() )
111            {
112                artifacts = new ArrayList<Artifact>();
113            }
114            artifacts.add( artifact );
115        }
116        return this;
117    }
118
119    /**
120     * Gets the metadata that got installed. Note that due to automatically generated metadata, there might have been
121     * more metadata installed than originally specified in the install request.
122     * 
123     * @return The installed metadata, never {@code null}.
124     */
125    public Collection<Metadata> getMetadata()
126    {
127        return metadata;
128    }
129
130    /**
131     * Sets the metadata that got installed.
132     * 
133     * @param metadata The installed metadata, may be {@code null}.
134     * @return This result for chaining, never {@code null}.
135     */
136    public InstallResult setMetadata( Collection<Metadata> metadata )
137    {
138        if ( metadata == null )
139        {
140            this.metadata = Collections.emptyList();
141        }
142        else
143        {
144            this.metadata = metadata;
145        }
146        return this;
147    }
148
149    /**
150     * Adds the specified metadata to this result.
151     * 
152     * @param metadata The installed metadata to add, may be {@code null}.
153     * @return This result for chaining, never {@code null}.
154     */
155    public InstallResult addMetadata( Metadata metadata )
156    {
157        if ( metadata != null )
158        {
159            if ( this.metadata.isEmpty() )
160            {
161                this.metadata = new ArrayList<Metadata>();
162            }
163            this.metadata.add( metadata );
164        }
165        return this;
166    }
167
168    @Override
169    public String toString()
170    {
171        return getArtifacts() + ", " + getMetadata();
172    }
173
174}