1 package org.apache.maven.repository.metadata;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.maven.artifact.ArtifactScopeEnum;
23
24 /**
25 * metadata graph vertice - just a wrapper around artifact's metadata
26 *
27 * @author <a href="oleg@codehaus.org">Oleg Gusakov</a>
28 */
29 public class MetadataGraphVertex
30 implements Comparable<MetadataGraphVertex>
31 {
32 ArtifactMetadata md;
33
34 // indications to use these in comparison
35 private boolean compareVersion = false;
36 private boolean compareScope = false;
37
38 public MetadataGraphVertex( ArtifactMetadata md )
39 {
40 super();
41 this.md = md;
42 }
43
44 public MetadataGraphVertex( ArtifactMetadata md, boolean compareVersion, boolean compareScope )
45 {
46 this( md );
47 this.compareVersion = compareVersion;
48 this.compareScope = compareScope;
49 }
50
51 public ArtifactMetadata getMd()
52 {
53 return md;
54 }
55
56 public void setMd( ArtifactMetadata md )
57 {
58 this.md = md;
59 }
60
61 // ---------------------------------------------------------------------
62 public boolean isCompareVersion()
63 {
64 return compareVersion;
65 }
66
67 public void setCompareVersion( boolean compareVersion )
68 {
69 this.compareVersion = compareVersion;
70 }
71
72 public boolean isCompareScope()
73 {
74 return compareScope;
75 }
76
77 public void setCompareScope( boolean compareScope )
78 {
79 this.compareScope = compareScope;
80 }
81
82 // ---------------------------------------------------------------------
83 @Override
84 public String toString()
85 {
86 return "[" + ( md == null ? "no metadata" : md.toString() ) + "]";
87 }
88
89 // ---------------------------------------------------------------------
90 private static int compareStrings( String s1, String s2 )
91 {
92 if ( s1 == null && s2 == null )
93 {
94 return 0;
95 }
96
97 if ( s1 == null /* && s2 != null */ )
98 {
99 return -1;
100 }
101
102 if ( /* s1 != null && */ s2 == null )
103 {
104 return 1;
105 }
106
107 return s1.compareTo( s2 );
108 }
109
110 // ---------------------------------------------------------------------
111 public int compareTo( MetadataGraphVertex vertex )
112 {
113 if ( vertex == null || vertex.getMd() == null )
114 {
115 return 1;
116 }
117
118 ArtifactMetadata vmd = vertex.getMd();
119
120 if ( vmd == null )
121 {
122 if ( md == null )
123 {
124 return 0;
125 }
126 else
127 {
128 return 1;
129 }
130 }
131
132 int g = compareStrings( md.groupId, vmd.groupId );
133
134 if ( g == 0 )
135 {
136 int a = compareStrings( md.artifactId, vmd.artifactId );
137 if ( a == 0 )
138 {
139 if ( compareVersion )
140 {
141 int v = compareStrings( md.version, vmd.version );
142 if ( v == 0 )
143 {
144 if ( compareScope )
145 {
146 String s1 = ArtifactScopeEnum.checkScope( md.artifactScope ).getScope();
147 String s2 = ArtifactScopeEnum.checkScope( vmd.artifactScope ).getScope();
148 return s1.compareTo( s2 );
149 }
150 else
151 {
152 return 0;
153 }
154 }
155 else
156 {
157 return v;
158 }
159 }
160 else
161 {
162 return 0;
163 }
164 }
165 else
166 {
167 return a;
168 }
169 }
170
171 return g;
172 }
173
174 // ---------------------------------------------------------------------
175 @Override
176 public boolean equals( Object vo )
177 {
178 if ( vo == null || !( vo instanceof MetadataGraphVertex ) )
179 {
180 return false;
181 }
182 return compareTo( (MetadataGraphVertex) vo ) == 0;
183 }
184
185 // ---------------------------------------------------------------------
186
187 @Override
188 public int hashCode()
189 {
190 if ( md == null )
191 {
192 return super.hashCode();
193 }
194 StringBuilder hashString = new StringBuilder( 128 );
195 hashString.append( md.groupId ).append( '|' );
196 hashString.append( md.artifactId ).append( '|' );
197
198 if ( compareVersion )
199 {
200 hashString.append( md.version ).append( '|' );
201 }
202
203 if ( compareScope )
204 {
205 hashString.append( md.getArtifactScope() ).append( '|' );
206 }
207
208 return hashString.toString().hashCode();
209
210 // BASE64Encoder b64 = new BASE64Encoder();
211 // return b64.encode( hashString.toString().getBytes() ).hashCode();
212 }
213
214 // ---------------------------------------------------------------------
215 // ---------------------------------------------------------------------
216 }