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.util.Collections;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Objects;
26
27
28
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
60
61
62
63
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().isEmpty()) {
77 buffer.append(getGroupId());
78 }
79 if (!getArtifactId().isEmpty()) {
80 buffer.append(':').append(getArtifactId());
81 }
82 if (!getVersion().isEmpty()) {
83 buffer.append(':').append(getVersion());
84 }
85 buffer.append('/').append(getType());
86 return buffer.toString();
87 }
88
89
90
91
92
93
94
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
117
118
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 }