View Javadoc
1   package org.eclipse.aether.metadata;
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.io.File;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  /**
28   * A skeleton class for metadata.
29   */
30  public abstract class AbstractMetadata
31      implements Metadata
32  {
33  
34      private Metadata newInstance( Map<String, String> properties, File file )
35      {
36          return new DefaultMetadata( getGroupId(), getArtifactId(), getVersion(), getType(), getNature(), file,
37                                      properties );
38      }
39  
40      public Metadata setFile( File file )
41      {
42          File current = getFile();
43          if ( ( current == null ) ? file == null : current.equals( file ) )
44          {
45              return this;
46          }
47          return newInstance( getProperties(), file );
48      }
49  
50      public Metadata setProperties( Map<String, String> properties )
51      {
52          Map<String, String> current = getProperties();
53          if ( current.equals( properties ) || ( properties == null && current.isEmpty() ) )
54          {
55              return this;
56          }
57          return newInstance( copyProperties( properties ), getFile() );
58      }
59  
60      public String getProperty( String key, String defaultValue )
61      {
62          String value = getProperties().get( key );
63          return ( value != null ) ? value : defaultValue;
64      }
65  
66      /**
67       * Copies the specified metadata properties. This utility method should be used when creating new metadata instances
68       * with caller-supplied properties.
69       * 
70       * @param properties The properties to copy, may be {@code null}.
71       * @return The copied and read-only properties, never {@code null}.
72       */
73      protected static Map<String, String> copyProperties( Map<String, String> properties )
74      {
75          if ( properties != null && !properties.isEmpty() )
76          {
77              return Collections.unmodifiableMap( new HashMap<String, String>( properties ) );
78          }
79          else
80          {
81              return Collections.emptyMap();
82          }
83      }
84  
85      @Override
86      public String toString()
87      {
88          StringBuilder buffer = new StringBuilder( 128 );
89          if ( getGroupId().length() > 0 )
90          {
91              buffer.append( getGroupId() );
92          }
93          if ( getArtifactId().length() > 0 )
94          {
95              buffer.append( ':' ).append( getArtifactId() );
96          }
97          if ( getVersion().length() > 0 )
98          {
99              buffer.append( ':' ).append( getVersion() );
100         }
101         buffer.append( '/' ).append( getType() );
102         return buffer.toString();
103     }
104 
105     /**
106      * Compares this metadata with the specified object.
107      * 
108      * @param obj The object to compare this metadata against, may be {@code null}.
109      * @return {@code true} if and only if the specified object is another {@link Metadata} with equal coordinates,
110      *         type, nature, properties and file, {@code false} otherwise.
111      */
112     @Override
113     public boolean equals( Object obj )
114     {
115         if ( obj == this )
116         {
117             return true;
118         }
119         else if ( !( obj instanceof Metadata ) )
120         {
121             return false;
122         }
123 
124         Metadata that = (Metadata) obj;
125 
126         return getArtifactId().equals( that.getArtifactId() ) && getGroupId().equals( that.getGroupId() )
127             && getVersion().equals( that.getVersion() ) && getType().equals( that.getType() )
128             && getNature().equals( that.getNature() ) && eq( getFile(), that.getFile() )
129             && eq( getProperties(), that.getProperties() );
130     }
131 
132     private static <T> boolean eq( T s1, T s2 )
133     {
134         return s1 != null ? s1.equals( s2 ) : s2 == null;
135     }
136 
137     /**
138      * Returns a hash code for this metadata.
139      * 
140      * @return A hash code for the metadata.
141      */
142     @Override
143     public int hashCode()
144     {
145         int hash = 17;
146         hash = hash * 31 + getGroupId().hashCode();
147         hash = hash * 31 + getArtifactId().hashCode();
148         hash = hash * 31 + getType().hashCode();
149         hash = hash * 31 + getNature().hashCode();
150         hash = hash * 31 + getVersion().hashCode();
151         hash = hash * 31 + hash( getFile() );
152         return hash;
153     }
154 
155     private static int hash( Object obj )
156     {
157         return ( obj != null ) ? obj.hashCode() : 0;
158     }
159 
160 }