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