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