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