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
34
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
79
80
81
82
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
110
111
112
113
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
136
137
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 }