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.metadata;
020
021import java.io.File;
022import java.nio.file.Path;
023import java.util.Map;
024
025import static java.util.Objects.requireNonNull;
026
027/**
028 * A basic metadata instance. <em>Note:</em> Instances of this class are immutable and the exposed mutators return new
029 * objects rather than changing the current instance.
030 */
031public final class DefaultMetadata extends AbstractMetadata {
032
033    private final String groupId;
034
035    private final String artifactId;
036
037    private final String version;
038
039    private final String type;
040
041    private final Nature nature;
042
043    private final Path path;
044
045    private final Map<String, String> properties;
046
047    /**
048     * Creates a new metadata for the repository root with the specific type and nature.
049     *
050     * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
051     * @param nature The nature of the metadata, must not be {@code null}.
052     */
053    public DefaultMetadata(String type, Nature nature) {
054        this("", "", "", type, nature, null, (Path) null);
055    }
056
057    /**
058     * Creates a new metadata for the groupId level with the specific type and nature.
059     *
060     * @param groupId The group identifier to which this metadata applies, may be {@code null}.
061     * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
062     * @param nature The nature of the metadata, must not be {@code null}.
063     */
064    public DefaultMetadata(String groupId, String type, Nature nature) {
065        this(groupId, "", "", type, nature, null, (Path) null);
066    }
067
068    /**
069     * Creates a new metadata for the groupId:artifactId level with the specific type and nature.
070     *
071     * @param groupId The group identifier to which this metadata applies, may be {@code null}.
072     * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
073     * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
074     * @param nature The nature of the metadata, must not be {@code null}.
075     */
076    public DefaultMetadata(String groupId, String artifactId, String type, Nature nature) {
077        this(groupId, artifactId, "", type, nature, null, (Path) null);
078    }
079
080    /**
081     * Creates a new metadata for the groupId:artifactId:version level with the specific type and nature.
082     *
083     * @param groupId The group identifier to which this metadata applies, may be {@code null}.
084     * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
085     * @param version The version to which this metadata applies, may be {@code null}.
086     * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
087     * @param nature The nature of the metadata, must not be {@code null}.
088     */
089    public DefaultMetadata(String groupId, String artifactId, String version, String type, Nature nature) {
090        this(groupId, artifactId, version, type, nature, null, (Path) null);
091    }
092
093    /**
094     * Creates a new metadata for the groupId:artifactId:version level with the specific type and nature.
095     *
096     * @param groupId The group identifier to which this metadata applies, may be {@code null}.
097     * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
098     * @param version The version to which this metadata applies, may be {@code null}.
099     * @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}