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