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