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.metadata;
20  
21  import java.io.File;
22  import java.nio.file.Path;
23  import java.util.Map;
24  
25  import static java.util.Objects.requireNonNull;
26  
27  /**
28   * A basic metadata instance. <em>Note:</em> Instances of this class are immutable and the exposed mutators return new
29   * objects rather than changing the current instance.
30   */
31  public final class DefaultMetadata extends AbstractMetadata {
32  
33      private final String groupId;
34  
35      private final String artifactId;
36  
37      private final String version;
38  
39      private final String type;
40  
41      private final Nature nature;
42  
43      private final Path path;
44  
45      private final Map<String, String> properties;
46  
47      /**
48       * Creates a new metadata for the repository root with the specific type and nature.
49       *
50       * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
51       * @param nature The nature of the metadata, must not be {@code null}.
52       */
53      public DefaultMetadata(String type, Nature nature) {
54          this("", "", "", type, nature, null, (Path) null);
55      }
56  
57      /**
58       * Creates a new metadata for the groupId level with the specific type and nature.
59       *
60       * @param groupId The group identifier to which this metadata applies, may be {@code null}.
61       * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
62       * @param nature The nature of the metadata, must not be {@code null}.
63       */
64      public DefaultMetadata(String groupId, String type, Nature nature) {
65          this(groupId, "", "", type, nature, null, (Path) null);
66      }
67  
68      /**
69       * Creates a new metadata for the groupId:artifactId level with the specific type and nature.
70       *
71       * @param groupId The group identifier to which this metadata applies, may be {@code null}.
72       * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
73       * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
74       * @param nature The nature of the metadata, must not be {@code null}.
75       */
76      public DefaultMetadata(String groupId, String artifactId, String type, Nature nature) {
77          this(groupId, artifactId, "", type, nature, null, (Path) null);
78      }
79  
80      /**
81       * Creates a new metadata for the groupId:artifactId:version level with the specific type and nature.
82       *
83       * @param groupId The group identifier to which this metadata applies, may be {@code null}.
84       * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
85       * @param version The version to which this metadata applies, may be {@code null}.
86       * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
87       * @param nature The nature of the metadata, must not be {@code null}.
88       */
89      public DefaultMetadata(String groupId, String artifactId, String version, String type, Nature nature) {
90          this(groupId, artifactId, version, type, nature, null, (Path) null);
91      }
92  
93      /**
94       * Creates a new metadata for the groupId:artifactId:version level with the specific type and nature.
95       *
96       * @param groupId The group identifier to which this metadata applies, may be {@code null}.
97       * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
98       * @param version The version to which this metadata applies, may be {@code null}.
99       * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
100      * @param nature The nature of the metadata, must not be {@code null}.
101      * @param file The resolved file of the metadata, may be {@code null}.
102      * @deprecated Use {@link #DefaultMetadata(String, String, String, String, Nature, Path)} instead.
103      */
104     @Deprecated
105     public DefaultMetadata(String groupId, String artifactId, String version, String type, Nature nature, File file) {
106         this(groupId, artifactId, version, type, nature, null, file);
107     }
108 
109     /**
110      * Creates a new metadata for the groupId:artifactId:version level with the specific type and nature.
111      *
112      * @param groupId The group identifier to which this metadata applies, may be {@code null}.
113      * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
114      * @param version The version to which this metadata applies, may be {@code null}.
115      * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
116      * @param nature The nature of the metadata, must not be {@code null}.
117      * @param path The resolved file of the metadata, may be {@code null}.
118      * @since 2.0.0
119      */
120     public DefaultMetadata(String groupId, String artifactId, String version, String type, Nature nature, Path path) {
121         this(groupId, artifactId, version, type, nature, null, path);
122     }
123 
124     /**
125      * Creates a new metadata for the groupId:artifactId:version level with the specific type and nature.
126      *
127      * @param groupId The group identifier to which this metadata applies, may be {@code null}.
128      * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
129      * @param version The version to which this metadata applies, may be {@code null}.
130      * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
131      * @param nature The nature of the metadata, must not be {@code null}.
132      * @param properties The properties of the metadata, may be {@code null} if none.
133      * @param file The resolved file of the metadata, may be {@code null}.
134      * @deprecated Use {@link #DefaultMetadata(String, String, String, String, Nature, Map, Path)} instead.
135      */
136     @Deprecated
137     public DefaultMetadata(
138             String groupId,
139             String artifactId,
140             String version,
141             String type,
142             Nature nature,
143             Map<String, String> properties,
144             File file) {
145         this.groupId = emptify(groupId);
146         this.artifactId = emptify(artifactId);
147         this.version = emptify(version);
148         this.type = emptify(type);
149         this.nature = requireNonNull(nature, "metadata nature cannot be null");
150         this.path = file != null ? file.toPath() : null;
151         this.properties = copyProperties(properties);
152     }
153 
154     /**
155      * Creates a new metadata for the groupId:artifactId:version level with the specific type and nature.
156      *
157      * @param groupId The group identifier to which this metadata applies, may be {@code null}.
158      * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
159      * @param version The version to which this metadata applies, may be {@code null}.
160      * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
161      * @param nature The nature of the metadata, must not be {@code null}.
162      * @param properties The properties of the metadata, may be {@code null} if none.
163      * @param path The resolved file of the metadata, may be {@code null}.
164      * @since 2.0.0
165      */
166     public DefaultMetadata(
167             String groupId,
168             String artifactId,
169             String version,
170             String type,
171             Nature nature,
172             Map<String, String> properties,
173             Path path) {
174         this.groupId = emptify(groupId);
175         this.artifactId = emptify(artifactId);
176         this.version = emptify(version);
177         this.type = emptify(type);
178         this.nature = requireNonNull(nature, "metadata nature cannot be null");
179         this.path = path;
180         this.properties = copyProperties(properties);
181     }
182 
183     DefaultMetadata(
184             String groupId,
185             String artifactId,
186             String version,
187             String type,
188             Nature nature,
189             Path path,
190             Map<String, String> properties) {
191         // NOTE: This constructor assumes immutability of the provided properties, for internal use only
192         this.groupId = emptify(groupId);
193         this.artifactId = emptify(artifactId);
194         this.version = emptify(version);
195         this.type = emptify(type);
196         this.nature = nature;
197         this.path = path;
198         this.properties = properties;
199     }
200 
201     private static String emptify(String str) {
202         return (str == null) ? "" : str;
203     }
204 
205     @Override
206     public String getGroupId() {
207         return groupId;
208     }
209 
210     @Override
211     public String getArtifactId() {
212         return artifactId;
213     }
214 
215     @Override
216     public String getVersion() {
217         return version;
218     }
219 
220     @Override
221     public String getType() {
222         return type;
223     }
224 
225     @Override
226     public Nature getNature() {
227         return nature;
228     }
229 
230     @Deprecated
231     @Override
232     public File getFile() {
233         return path != null ? path.toFile() : null;
234     }
235 
236     @Override
237     public Path getPath() {
238         return path;
239     }
240 
241     @Override
242     public Map<String, String> getProperties() {
243         return properties;
244     }
245 }