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