1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.eclipse.aether.artifact; 20 21 import java.util.Collections; 22 import java.util.HashMap; 23 import java.util.Map; 24 25 import static java.util.Objects.requireNonNull; 26 27 /** 28 * A simple artifact type. 29 */ 30 public final class DefaultArtifactType implements ArtifactType { 31 32 private final String id; 33 34 private final String extension; 35 36 private final String classifier; 37 38 private final Map<String, String> properties; 39 40 /** 41 * Creates a new artifact type with the specified identifier. This constructor assumes the usual file extension 42 * equals the given type id and that the usual classifier is empty. Additionally, the properties 43 * {@link ArtifactProperties#LANGUAGE}, {@link ArtifactProperties#CONSTITUTES_BUILD_PATH} and 44 * {@link ArtifactProperties#INCLUDES_DEPENDENCIES} will be set to {@code "none"}, {@code true} and {@code false}, 45 * respectively. 46 * 47 * @param id The identifier of the type which will also be used as the value for the {@link ArtifactProperties#TYPE} 48 * property, must not be {@code null} or empty. 49 */ 50 public DefaultArtifactType(String id) { 51 this(id, id, "", "none", false, false); 52 } 53 54 /** 55 * Creates a new artifact type with the specified properties. Additionally, the properties 56 * {@link ArtifactProperties#CONSTITUTES_BUILD_PATH} and {@link ArtifactProperties#INCLUDES_DEPENDENCIES} will be 57 * set to {@code true} and {@code false}, respectively. 58 * 59 * @param id The identifier of the type which will also be used as the value for the {@link ArtifactProperties#TYPE} 60 * property, must not be {@code null} or empty. 61 * @param extension The usual file extension for artifacts of this type, may be {@code null}. 62 * @param classifier The usual classifier for artifacts of this type, may be {@code null}. 63 * @param language The value for the {@link ArtifactProperties#LANGUAGE} property, may be {@code null}. 64 */ 65 public DefaultArtifactType(String id, String extension, String classifier, String language) { 66 this(id, extension, classifier, language, true, false); 67 } 68 69 /** 70 * Creates a new artifact type with the specified properties. 71 * 72 * @param id The identifier of the type which will also be used as the value for the {@link ArtifactProperties#TYPE} 73 * property, must not be {@code null} or empty. 74 * @param extension The usual file extension for artifacts of this type, may be {@code null}. 75 * @param classifier The usual classifier for artifacts of this type, may be {@code null}. 76 * @param language The value for the {@link ArtifactProperties#LANGUAGE} property, may be {@code null}. 77 * @param constitutesBuildPath The value for the {@link ArtifactProperties#CONSTITUTES_BUILD_PATH} property. 78 * @param includesDependencies The value for the {@link ArtifactProperties#INCLUDES_DEPENDENCIES} property. 79 */ 80 public DefaultArtifactType( 81 String id, 82 String extension, 83 String classifier, 84 String language, 85 boolean constitutesBuildPath, 86 boolean includesDependencies) { 87 this.id = requireNonNull(id, "type id cannot be null"); 88 if (id.isEmpty()) { 89 throw new IllegalArgumentException("type id cannot be empty"); 90 } 91 this.extension = emptify(extension); 92 this.classifier = emptify(classifier); 93 Map<String, String> props = new HashMap<>(); 94 props.put(ArtifactProperties.TYPE, id); 95 props.put(ArtifactProperties.LANGUAGE, (language != null && !language.isEmpty()) ? language : "none"); 96 props.put(ArtifactProperties.INCLUDES_DEPENDENCIES, Boolean.toString(includesDependencies)); 97 props.put(ArtifactProperties.CONSTITUTES_BUILD_PATH, Boolean.toString(constitutesBuildPath)); 98 properties = Collections.unmodifiableMap(props); 99 } 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.isEmpty()) { 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 }