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.util.Map;
23
24 import static java.util.Objects.requireNonNull;
25
26 /**
27 * A basic metadata instance. <em>Note:</em> Instances of this class are immutable and the exposed mutators return new
28 * objects rather than changing the current instance.
29 */
30 public final class DefaultMetadata extends AbstractMetadata {
31
32 private final String groupId;
33
34 private final String artifactId;
35
36 private final String version;
37
38 private final String type;
39
40 private final Nature nature;
41
42 private final File file;
43
44 private final Map<String, String> properties;
45
46 /**
47 * Creates a new metadata for the repository root with the specific type and nature.
48 *
49 * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
50 * @param nature The nature of the metadata, must not be {@code null}.
51 */
52 public DefaultMetadata(String type, Nature nature) {
53 this("", "", "", type, nature, null, (File) null);
54 }
55
56 /**
57 * Creates a new metadata for the groupId level with the specific type and nature.
58 *
59 * @param groupId The group identifier to which this metadata applies, may be {@code null}.
60 * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
61 * @param nature The nature of the metadata, must not be {@code null}.
62 */
63 public DefaultMetadata(String groupId, String type, Nature nature) {
64 this(groupId, "", "", type, nature, null, (File) null);
65 }
66
67 /**
68 * Creates a new metadata for the groupId:artifactId level with the specific type and nature.
69 *
70 * @param groupId The group identifier to which this metadata applies, may be {@code null}.
71 * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
72 * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
73 * @param nature The nature of the metadata, must not be {@code null}.
74 */
75 public DefaultMetadata(String groupId, String artifactId, String type, Nature nature) {
76 this(groupId, artifactId, "", type, nature, null, (File) null);
77 }
78
79 /**
80 * Creates a new metadata for the groupId:artifactId:version level with the specific type and nature.
81 *
82 * @param groupId The group identifier to which this metadata applies, may be {@code null}.
83 * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
84 * @param version The version to which this metadata applies, may be {@code null}.
85 * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
86 * @param nature The nature of the metadata, must not be {@code null}.
87 */
88 public DefaultMetadata(String groupId, String artifactId, String version, String type, Nature nature) {
89 this(groupId, artifactId, version, type, nature, null, (File) null);
90 }
91
92 /**
93 * Creates a new metadata for the groupId:artifactId:version level with the specific type and nature.
94 *
95 * @param groupId The group identifier to which this metadata applies, may be {@code null}.
96 * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
97 * @param version The version to which this metadata applies, may be {@code null}.
98 * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
99 * @param nature The nature of the metadata, must not be {@code null}.
100 * @param file The resolved file of the metadata, may be {@code null}.
101 */
102 public DefaultMetadata(String groupId, String artifactId, String version, String type, Nature nature, File file) {
103 this(groupId, artifactId, version, type, nature, null, file);
104 }
105
106 /**
107 * Creates a new metadata for the groupId:artifactId:version level with the specific type and nature.
108 *
109 * @param groupId The group identifier to which this metadata applies, may be {@code null}.
110 * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
111 * @param version The version to which this metadata applies, may be {@code null}.
112 * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
113 * @param nature The nature of the metadata, must not be {@code null}.
114 * @param properties The properties of the metadata, may be {@code null} if none.
115 * @param file The resolved file of the metadata, may be {@code null}.
116 */
117 public DefaultMetadata(
118 String groupId,
119 String artifactId,
120 String version,
121 String type,
122 Nature nature,
123 Map<String, String> properties,
124 File file) {
125 this.groupId = emptify(groupId);
126 this.artifactId = emptify(artifactId);
127 this.version = emptify(version);
128 this.type = emptify(type);
129 this.nature = requireNonNull(nature, "metadata nature cannot be null");
130 this.file = file;
131 this.properties = copyProperties(properties);
132 }
133
134 DefaultMetadata(
135 String groupId,
136 String artifactId,
137 String version,
138 String type,
139 Nature nature,
140 File file,
141 Map<String, String> properties) {
142 // NOTE: This constructor assumes immutability of the provided properties, for internal use only
143 this.groupId = emptify(groupId);
144 this.artifactId = emptify(artifactId);
145 this.version = emptify(version);
146 this.type = emptify(type);
147 this.nature = nature;
148 this.file = file;
149 this.properties = properties;
150 }
151
152 private static String emptify(String str) {
153 return (str == null) ? "" : str;
154 }
155
156 public String getGroupId() {
157 return groupId;
158 }
159
160 public String getArtifactId() {
161 return artifactId;
162 }
163
164 public String getVersion() {
165 return version;
166 }
167
168 public String getType() {
169 return type;
170 }
171
172 public Nature getNature() {
173 return nature;
174 }
175
176 public File getFile() {
177 return file;
178 }
179
180 public Map<String, String> getProperties() {
181 return properties;
182 }
183 }