1 package org.eclipse.aether.metadata;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
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
32 implements Metadata
33 {
34
35 private Metadata newInstance( Map<String, String> properties, File file )
36 {
37 return new DefaultMetadata( getGroupId(), getArtifactId(), getVersion(), getType(), getNature(), file,
38 properties );
39 }
40
41 public Metadata setFile( File file )
42 {
43 File current = getFile();
44 if ( Objects.equals( current, file ) )
45 {
46 return this;
47 }
48 return newInstance( getProperties(), file );
49 }
50
51 public Metadata setProperties( Map<String, String> properties )
52 {
53 Map<String, String> current = getProperties();
54 if ( current.equals( properties ) || ( properties == null && current.isEmpty() ) )
55 {
56 return this;
57 }
58 return newInstance( copyProperties( properties ), getFile() );
59 }
60
61 public String getProperty( String key, String defaultValue )
62 {
63 String value = getProperties().get( key );
64 return ( value != null ) ? value : defaultValue;
65 }
66
67
68
69
70
71
72
73
74 protected static Map<String, String> copyProperties( Map<String, String> properties )
75 {
76 if ( properties != null && !properties.isEmpty() )
77 {
78 return Collections.unmodifiableMap( new HashMap<>( properties ) );
79 }
80 else
81 {
82 return Collections.emptyMap();
83 }
84 }
85
86 @Override
87 public String toString()
88 {
89 StringBuilder buffer = new StringBuilder( 128 );
90 if ( getGroupId().length() > 0 )
91 {
92 buffer.append( getGroupId() );
93 }
94 if ( getArtifactId().length() > 0 )
95 {
96 buffer.append( ':' ).append( getArtifactId() );
97 }
98 if ( getVersion().length() > 0 )
99 {
100 buffer.append( ':' ).append( getVersion() );
101 }
102 buffer.append( '/' ).append( getType() );
103 return buffer.toString();
104 }
105
106
107
108
109
110
111
112
113 @Override
114 public boolean equals( Object obj )
115 {
116 if ( obj == this )
117 {
118 return true;
119 }
120 else if ( !( obj instanceof Metadata ) )
121 {
122 return false;
123 }
124
125 Metadata that = (Metadata) obj;
126
127 return Objects.equals( getArtifactId(), that.getArtifactId() )
128 && Objects.equals( getGroupId(), that.getGroupId() )
129 && Objects.equals( getVersion(), that.getVersion() )
130 && Objects.equals( getType(), that.getType() )
131 && Objects.equals( getNature(), that.getNature() )
132 && Objects.equals( getFile(), that.getFile() )
133 && Objects.equals( getProperties(), that.getProperties() );
134 }
135
136
137
138
139
140
141 @Override
142 public int hashCode()
143 {
144 int hash = 17;
145 hash = hash * 31 + getGroupId().hashCode();
146 hash = hash * 31 + getArtifactId().hashCode();
147 hash = hash * 31 + getType().hashCode();
148 hash = hash * 31 + getNature().hashCode();
149 hash = hash * 31 + getVersion().hashCode();
150 hash = hash * 31 + hash( getFile() );
151 return hash;
152 }
153
154 private static int hash( Object obj )
155 {
156 return ( obj != null ) ? obj.hashCode() : 0;
157 }
158
159 }