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;
024
025/**
026 * A basic metadata instance. <em>Note:</em> Instances of this class are immutable and the exposed mutators return new
027 * objects rather than changing the current instance.
028 */
029public final class DefaultMetadata
030    extends AbstractMetadata
031{
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 File file;
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    {
055        this( "", "", "", type, nature, null, (File) null );
056    }
057
058    /**
059     * Creates a new metadata for the groupId level with the specific type and nature.
060     * 
061     * @param groupId The group identifier to which this metadata applies, may be {@code null}.
062     * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
063     * @param nature The nature of the metadata, must not be {@code null}.
064     */
065    public DefaultMetadata( String groupId, String type, Nature nature )
066    {
067        this( groupId, "", "", type, nature, null, (File) null );
068    }
069
070    /**
071     * Creates a new metadata for the groupId:artifactId level with the specific type and nature.
072     * 
073     * @param groupId The group identifier to which this metadata applies, may be {@code null}.
074     * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
075     * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
076     * @param nature The nature of the metadata, must not be {@code null}.
077     */
078    public DefaultMetadata( String groupId, String artifactId, String type, Nature nature )
079    {
080        this( groupId, artifactId, "", type, nature, null, (File) null );
081    }
082
083    /**
084     * Creates a new metadata for the groupId:artifactId:version level with the specific type and nature.
085     * 
086     * @param groupId The group identifier to which this metadata applies, may be {@code null}.
087     * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
088     * @param version The version to which this metadata applies, may be {@code null}.
089     * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
090     * @param nature The nature of the metadata, must not be {@code null}.
091     */
092    public DefaultMetadata( String groupId, String artifactId, String version, String type, Nature nature )
093    {
094        this( groupId, artifactId, version, type, nature, null, (File) null );
095    }
096
097    /**
098     * Creates a new metadata for the groupId:artifactId:version level with the specific type and nature.
099     * 
100     * @param groupId The group identifier to which this metadata applies, may be {@code null}.
101     * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
102     * @param version The version to which this metadata applies, may be {@code null}.
103     * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
104     * @param nature The nature of the metadata, must not be {@code null}.
105     * @param file The resolved file of the metadata, may be {@code null}.
106     */
107    public DefaultMetadata( String groupId, String artifactId, String version, String type, Nature nature, File file )
108    {
109        this( groupId, artifactId, version, type, nature, null, file );
110    }
111
112    /**
113     * Creates a new metadata for the groupId:artifactId:version level with the specific type and nature.
114     * 
115     * @param groupId The group identifier to which this metadata applies, may be {@code null}.
116     * @param artifactId The artifact identifier to which this metadata applies, may be {@code null}.
117     * @param version The version to which this metadata applies, may be {@code null}.
118     * @param type The type of the metadata, e.g. "maven-metadata.xml", may be {@code null}.
119     * @param nature The nature of the metadata, must not be {@code null}.
120     * @param properties The properties of the metadata, may be {@code null} if none.
121     * @param file The resolved file of the metadata, may be {@code null}.
122     */
123    public DefaultMetadata( String groupId, String artifactId, String version, String type, Nature nature,
124                            Map<String, String> properties, File file )
125    {
126        this.groupId = emptify( groupId );
127        this.artifactId = emptify( artifactId );
128        this.version = emptify( version );
129        this.type = emptify( type );
130        if ( nature == null )
131        {
132            throw new IllegalArgumentException( "metadata nature was not specified" );
133        }
134        this.nature = nature;
135        this.file = file;
136        this.properties = copyProperties( properties );
137    }
138
139    DefaultMetadata( String groupId, String artifactId, String version, String type, Nature nature, File file,
140                     Map<String, String> properties )
141    {
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    {
154        return ( str == null ) ? "" : str;
155    }
156
157    public String getGroupId()
158    {
159        return groupId;
160    }
161
162    public String getArtifactId()
163    {
164        return artifactId;
165    }
166
167    public String getVersion()
168    {
169        return version;
170    }
171
172    public String getType()
173    {
174        return type;
175    }
176
177    public Nature getNature()
178    {
179        return nature;
180    }
181
182    public File getFile()
183    {
184        return file;
185    }
186
187    public Map<String, String> getProperties()
188    {
189        return properties;
190    }
191
192}