001package org.eclipse.aether.metadata;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.util.Map;
024import static java.util.Objects.requireNonNull;
025
026/**
027 * A basic metadata instance. <em>Note:</em> Instances of this class are immutable and the exposed mutators return new
028 * objects rather than changing the current instance.
029 */
030public final class DefaultMetadata
031    extends AbstractMetadata
032{
033
034    private final String groupId;
035
036    private final String artifactId;
037
038    private final String version;
039
040    private final String type;
041
042    private final Nature nature;
043
044    private final File file;
045
046    private final Map<String, String> properties;
047
048    /**
049     * Creates a new metadata for the repository root with the specific type and nature.
050     * 
051     * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
052     * @param nature The nature of the metadata, must not be {@code null}.
053     */
054    public DefaultMetadata( String type, Nature nature )
055    {
056        this( "", "", "", type, nature, null, (File) null );
057    }
058
059    /**
060     * Creates a new metadata for the groupId level with the specific type and nature.
061     * 
062     * @param groupId The group identifier to which this metadata applies, may be {@code null}.
063     * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
064     * @param nature The nature of the metadata, must not be {@code null}.
065     */
066    public DefaultMetadata( String groupId, String type, Nature nature )
067    {
068        this( groupId, "", "", type, nature, null, (File) null );
069    }
070
071    /**
072     * Creates a new metadata for the groupId:artifactId level with the specific type and nature.
073     * 
074     * @param groupId The group identifier to which this metadata applies, may be {@code null}.
075     * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
076     * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
077     * @param nature The nature of the metadata, must not be {@code null}.
078     */
079    public DefaultMetadata( String groupId, String artifactId, String type, Nature nature )
080    {
081        this( groupId, artifactId, "", type, nature, null, (File) null );
082    }
083
084    /**
085     * Creates a new metadata for the groupId:artifactId:version level with the specific type and nature.
086     * 
087     * @param groupId The group identifier to which this metadata applies, may be {@code null}.
088     * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
089     * @param version The version to which this metadata applies, may be {@code null}.
090     * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
091     * @param nature The nature of the metadata, must not be {@code null}.
092     */
093    public DefaultMetadata( String groupId, String artifactId, String version, String type, Nature nature )
094    {
095        this( groupId, artifactId, version, type, nature, null, (File) null );
096    }
097
098    /**
099     * Creates a new metadata for the groupId:artifactId:version level with the specific type and nature.
100     * 
101     * @param groupId The group identifier to which this metadata applies, may be {@code null}.
102     * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
103     * @param version The version to which this metadata applies, may be {@code null}.
104     * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
105     * @param nature The nature of the metadata, must not be {@code null}.
106     * @param file The resolved file of the metadata, may be {@code null}.
107     */
108    public DefaultMetadata( String groupId, String artifactId, String version, String type, Nature nature, File file )
109    {
110        this( groupId, artifactId, version, type, nature, null, file );
111    }
112
113    /**
114     * Creates a new metadata for the groupId:artifactId:version level with the specific type and nature.
115     * 
116     * @param groupId The group identifier to which this metadata applies, may be {@code null}.
117     * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
118     * @param version The version to which this metadata applies, may be {@code null}.
119     * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
120     * @param nature The nature of the metadata, must not be {@code null}.
121     * @param properties The properties of the metadata, may be {@code null} if none.
122     * @param file The resolved file of the metadata, may be {@code null}.
123     */
124    public DefaultMetadata( String groupId, String artifactId, String version, String type, Nature nature,
125                            Map<String, String> properties, File file )
126    {
127        this.groupId = emptify( groupId );
128        this.artifactId = emptify( artifactId );
129        this.version = emptify( version );
130        this.type = emptify( type );
131        this.nature = requireNonNull( nature, "metadata nature cannot be null" );
132        this.file = file;
133        this.properties = copyProperties( properties );
134    }
135
136    DefaultMetadata( String groupId, String artifactId, String version, String type, Nature nature, File file,
137                     Map<String, String> properties )
138    {
139        // NOTE: This constructor assumes immutability of the provided properties, for internal use only
140        this.groupId = emptify( groupId );
141        this.artifactId = emptify( artifactId );
142        this.version = emptify( version );
143        this.type = emptify( type );
144        this.nature = nature;
145        this.file = file;
146        this.properties = properties;
147    }
148
149    private static String emptify( String str )
150    {
151        return ( str == null ) ? "" : str;
152    }
153
154    public String getGroupId()
155    {
156        return groupId;
157    }
158
159    public String getArtifactId()
160    {
161        return artifactId;
162    }
163
164    public String getVersion()
165    {
166        return version;
167    }
168
169    public String getType()
170    {
171        return type;
172    }
173
174    public Nature getNature()
175    {
176        return nature;
177    }
178
179    public File getFile()
180    {
181        return file;
182    }
183
184    public Map<String, String> getProperties()
185    {
186        return properties;
187    }
188
189}