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