View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.installation;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  
25  import org.eclipse.aether.artifact.Artifact;
26  import org.eclipse.aether.metadata.Metadata;
27  
28  import static java.util.Objects.requireNonNull;
29  
30  /**
31   * The result of installing artifacts and their accompanying metadata into the a remote repository.
32   *
33   * @see org.eclipse.aether.RepositorySystem#install(org.eclipse.aether.RepositorySystemSession, InstallRequest)
34   */
35  public final class InstallResult {
36  
37      private final InstallRequest request;
38  
39      private Collection<Artifact> artifacts;
40  
41      private Collection<Metadata> metadata;
42  
43      /**
44       * Creates a new result for the specified request.
45       *
46       * @param request The installation request, must not be {@code null}.
47       */
48      public InstallResult(InstallRequest request) {
49          this.request = requireNonNull(request, "install request cannot be null");
50          artifacts = Collections.emptyList();
51          metadata = Collections.emptyList();
52      }
53  
54      /**
55       * Gets the install request that was made.
56       *
57       * @return The install request, never {@code null}.
58       */
59      public InstallRequest getRequest() {
60          return request;
61      }
62  
63      /**
64       * Gets the artifacts that got installed.
65       *
66       * @return The installed artifacts, never {@code null}.
67       */
68      public Collection<Artifact> getArtifacts() {
69          return artifacts;
70      }
71  
72      /**
73       * Sets the artifacts that got installed.
74       *
75       * @param artifacts The installed artifacts, may be {@code null}.
76       * @return This result for chaining, never {@code null}.
77       */
78      public InstallResult setArtifacts(Collection<Artifact> artifacts) {
79          if (artifacts == null) {
80              this.artifacts = Collections.emptyList();
81          } else {
82              this.artifacts = artifacts;
83          }
84          return this;
85      }
86  
87      /**
88       * Adds the specified artifacts to the result.
89       *
90       * @param artifact The installed artifact to add, may be {@code null}.
91       * @return This result for chaining, never {@code null}.
92       */
93      public InstallResult addArtifact(Artifact artifact) {
94          if (artifact != null) {
95              if (artifacts.isEmpty()) {
96                  artifacts = new ArrayList<>();
97              }
98              artifacts.add(artifact);
99          }
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 }