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