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