1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
74
75
76
77
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
105
106
107
108
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
131
132
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 }