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