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