001package org.eclipse.aether.artifact;
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.util.Collections;
023import java.util.HashMap;
024import java.util.Map;
025import static java.util.Objects.requireNonNull;
026
027/**
028 * A simple artifact type.
029 */
030public final class DefaultArtifactType
031    implements ArtifactType
032{
033
034    private final String id;
035
036    private final String extension;
037
038    private final String classifier;
039
040    private final Map<String, String> properties;
041
042    /**
043     * Creates a new artifact type with the specified identifier. This constructor assumes the usual file extension
044     * equals the given type id and that the usual classifier is empty. Additionally, the properties
045     * {@link ArtifactProperties#LANGUAGE}, {@link ArtifactProperties#CONSTITUTES_BUILD_PATH} and
046     * {@link ArtifactProperties#INCLUDES_DEPENDENCIES} will be set to {@code "none"}, {@code true} and {@code false},
047     * respectively.
048     * 
049     * @param id The identifier of the type which will also be used as the value for the {@link ArtifactProperties#TYPE}
050     *            property, must not be {@code null} or empty.
051     */
052    public DefaultArtifactType( String id )
053    {
054        this( id, id, "", "none", false, false );
055    }
056
057    /**
058     * Creates a new artifact type with the specified properties. Additionally, the properties
059     * {@link ArtifactProperties#CONSTITUTES_BUILD_PATH} and {@link ArtifactProperties#INCLUDES_DEPENDENCIES} will be
060     * set to {@code true} and {@code false}, respectively.
061     * 
062     * @param id The identifier of the type which will also be used as the value for the {@link ArtifactProperties#TYPE}
063     *            property, must not be {@code null} or empty.
064     * @param extension The usual file extension for artifacts of this type, may be {@code null}.
065     * @param classifier The usual classifier for artifacts of this type, may be {@code null}.
066     * @param language The value for the {@link ArtifactProperties#LANGUAGE} property, may be {@code null}.
067     */
068    public DefaultArtifactType( String id, String extension, String classifier, String language )
069    {
070        this( id, extension, classifier, language, true, false );
071    }
072
073    /**
074     * Creates a new artifact type with the specified properties.
075     * 
076     * @param id The identifier of the type which will also be used as the value for the {@link ArtifactProperties#TYPE}
077     *            property, must not be {@code null} or empty.
078     * @param extension The usual file extension for artifacts of this type, may be {@code null}.
079     * @param classifier The usual classifier for artifacts of this type, may be {@code null}.
080     * @param language The value for the {@link ArtifactProperties#LANGUAGE} property, may be {@code null}.
081     * @param constitutesBuildPath The value for the {@link ArtifactProperties#CONSTITUTES_BUILD_PATH} property.
082     * @param includesDependencies The value for the {@link ArtifactProperties#INCLUDES_DEPENDENCIES} property.
083     */
084    public DefaultArtifactType( String id, String extension, String classifier, String language,
085                                boolean constitutesBuildPath, boolean includesDependencies )
086    {
087        this.id = requireNonNull( id, "type id cannot be null" );
088        if ( id.length() == 0 )
089        {
090            throw new IllegalArgumentException( "type id cannot be empty" );
091        }
092        this.extension = emptify( extension );
093        this.classifier = emptify( classifier );
094        Map<String, String> props = new HashMap<>();
095        props.put( ArtifactProperties.TYPE, id );
096        props.put( ArtifactProperties.LANGUAGE, ( language != null && language.length() > 0 ) ? language : "none" );
097        props.put( ArtifactProperties.INCLUDES_DEPENDENCIES, Boolean.toString( includesDependencies ) );
098        props.put( ArtifactProperties.CONSTITUTES_BUILD_PATH, Boolean.toString( constitutesBuildPath ) );
099        properties = Collections.unmodifiableMap( props );
100    }
101
102    /**
103     * Creates a new artifact type with the specified properties.
104     * 
105     * @param id The identifier of the type, must not be {@code null} or empty.
106     * @param extension The usual file extension for artifacts of this type, may be {@code null}.
107     * @param classifier The usual classifier for artifacts of this type, may be {@code null}.
108     * @param properties The properties for artifacts of this type, may be {@code null}.
109     */
110    public DefaultArtifactType( String id, String extension, String classifier, Map<String, String> properties )
111    {
112        this.id = requireNonNull( id, "type id cannot be null" );
113        if ( id.length() == 0 )
114        {
115            throw new IllegalArgumentException( "type id cannot be empty" );
116        }
117        this.extension = emptify( extension );
118        this.classifier = emptify( classifier );
119        this.properties = AbstractArtifact.copyProperties( properties );
120    }
121
122    private static String emptify( String str )
123    {
124        return ( str == null ) ? "" : str;
125    }
126
127    public String getId()
128    {
129        return id;
130    }
131
132    public String getExtension()
133    {
134        return extension;
135    }
136
137    public String getClassifier()
138    {
139        return classifier;
140    }
141
142    public Map<String, String> getProperties()
143    {
144        return properties;
145    }
146
147}