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