1 package org.apache.maven.artifact.versioning; 2 3 18 19 import java.util.StringTokenizer ; 20 21 27 public class DefaultArtifactVersion 28 implements ArtifactVersion 29 { 30 private Integer majorVersion; 31 32 private Integer minorVersion; 33 34 private Integer incrementalVersion; 35 36 private Integer buildNumber; 37 38 private String qualifier; 39 40 public DefaultArtifactVersion( String version ) 41 { 42 parseVersion( version ); 43 } 44 45 public int compareTo( Object o ) 46 { 47 DefaultArtifactVersion otherVersion = (DefaultArtifactVersion) o; 48 49 int result = compareIntegers( majorVersion, otherVersion.majorVersion ); 50 if ( result == 0 ) 51 { 52 result = compareIntegers( minorVersion, otherVersion.minorVersion ); 53 } 54 if ( result == 0 ) 55 { 56 result = compareIntegers( incrementalVersion, otherVersion.incrementalVersion ); 57 } 58 if ( result == 0 ) 59 { 60 if ( buildNumber != null || otherVersion.buildNumber != null ) 61 { 62 result = compareIntegers( buildNumber, otherVersion.buildNumber ); 63 } 64 else if ( qualifier != null ) 65 { 66 if ( otherVersion.qualifier != null ) 67 { 68 if ( qualifier.length() > otherVersion.qualifier.length() && 69 qualifier.startsWith( otherVersion.qualifier ) ) 70 { 71 result = -1; 73 } 74 else if ( qualifier.length() < otherVersion.qualifier.length() && 75 otherVersion.qualifier.startsWith( qualifier ) ) 76 { 77 result = 1; 79 } 80 else 81 { 82 result = qualifier.compareTo( otherVersion.qualifier ); 83 } 84 } 85 else 86 { 87 result = -1; 89 } 90 } 91 else if ( otherVersion.qualifier != null ) 92 { 93 result = 1; 95 } 96 } 97 return result; 98 } 99 100 private int compareIntegers( Integer i1, Integer i2 ) 101 { 102 if ( i1 == null ? i2 == null : i1.equals( i2 ) ) 104 { 105 return 0; 106 } 107 else if ( i1 == null ) 108 { 109 return -i2.intValue(); 110 } 111 else if ( i2 == null ) 112 { 113 return i1.intValue(); 114 } 115 else 116 { 117 return i1.intValue() - i2.intValue(); 118 } 119 } 120 121 public int getMajorVersion() 122 { 123 return majorVersion != null ? majorVersion.intValue() : 0; 124 } 125 126 public int getMinorVersion() 127 { 128 return minorVersion != null ? minorVersion.intValue() : 0; 129 } 130 131 public int getIncrementalVersion() 132 { 133 return incrementalVersion != null ? incrementalVersion.intValue() : 0; 134 } 135 136 public int getBuildNumber() 137 { 138 return buildNumber != null ? buildNumber.intValue() : 0; 139 } 140 141 public String getQualifier() 142 { 143 return qualifier; 144 } 145 146 public final void parseVersion( String version ) 147 { 148 int index = version.indexOf( "-" ); 149 150 String part1; 151 String part2 = null; 152 153 if ( index < 0 ) 154 { 155 part1 = version; 156 } 157 else 158 { 159 part1 = version.substring( 0, index ); 160 part2 = version.substring( index + 1 ); 161 } 162 163 if ( part2 != null ) 164 { 165 try 166 { 167 if ( part2.length() == 1 || !part2.startsWith( "0" ) ) 168 { 169 buildNumber = Integer.valueOf( part2 ); 170 } 171 else 172 { 173 qualifier = part2; 174 } 175 } 176 catch ( NumberFormatException e ) 177 { 178 qualifier = part2; 179 } 180 } 181 182 if ( part1.indexOf( "." ) < 0 && !part1.startsWith( "0" ) ) 183 { 184 try 185 { 186 majorVersion = Integer.valueOf( part1 ); 187 } 188 catch ( NumberFormatException e ) 189 { 190 qualifier = version; 192 buildNumber = null; 193 } 194 } 195 else 196 { 197 boolean fallback = false; 198 StringTokenizer tok = new StringTokenizer ( part1, "." ); 199 try 200 { 201 majorVersion = getNextIntegerToken( tok ); 202 if ( tok.hasMoreTokens() ) 203 { 204 minorVersion = getNextIntegerToken( tok ); 205 } 206 if ( tok.hasMoreTokens() ) 207 { 208 incrementalVersion = getNextIntegerToken( tok ); 209 } 210 if ( tok.hasMoreTokens() ) 211 { 212 fallback = true; 213 } 214 } 215 catch ( NumberFormatException e ) 216 { 217 fallback = true; 218 } 219 220 if ( fallback ) 221 { 222 qualifier = version; 224 majorVersion = null; 225 minorVersion = null; 226 incrementalVersion = null; 227 buildNumber = null; 228 } 229 } 230 } 231 232 private static Integer getNextIntegerToken( StringTokenizer tok ) 233 { 234 String s = tok.nextToken(); 235 if ( s.length() > 1 && s.startsWith( "0" ) ) 236 { 237 throw new NumberFormatException ( "Number part has a leading 0: '" + s + "'" ); 238 } 239 return Integer.valueOf( s ); 240 } 241 242 public String toString() 243 { 244 StringBuffer buf = new StringBuffer (); 245 if ( majorVersion != null ) 246 { 247 buf.append( majorVersion ); 248 } 249 if ( minorVersion != null ) 250 { 251 buf.append( "." ); 252 buf.append( minorVersion ); 253 } 254 if ( incrementalVersion != null ) 255 { 256 buf.append( "." ); 257 buf.append( incrementalVersion ); 258 } 259 if ( buildNumber != null ) 260 { 261 buf.append( "-" ); 262 buf.append( buildNumber ); 263 } 264 else if ( qualifier != null ) 265 { 266 if ( buf.length() > 0 ) 267 { 268 buf.append( "-" ); 269 } 270 buf.append( qualifier ); 271 } 272 return buf.toString(); 273 } 274 } 275 | Popular Tags |