1 package org.jahia.utils; 2 3 import java.util.ArrayList ; 4 import java.util.StringTokenizer ; 5 6 23 24 public class Version implements Comparable { 25 26 private static org.apache.log4j.Logger logger = 27 org.apache.log4j.Logger.getLogger(Version.class); 28 29 private ArrayList orderedVersionNumbers = new ArrayList (); 30 private int betaNumber = -1; 31 private int releaseCandidateNumber = -1; 32 33 40 public Version(String versionString) 41 throws NumberFormatException { 42 String workString = versionString.toLowerCase(); 43 int betaPos = workString.indexOf("b"); 44 int rcPos = workString.indexOf("rc"); 45 46 String betaString = null; 47 String rcString = null; 48 if (betaPos != -1) { 49 betaString = workString.substring(betaPos + 1).trim(); 50 workString = workString.substring(0, betaPos); 51 betaNumber = Integer.parseInt(betaString); 52 } else if (rcPos != -1) { 53 rcString = workString.substring(rcPos + 2).trim(); 54 workString = workString.substring(0, rcPos); 55 releaseCandidateNumber = Integer.parseInt(rcString); 56 } 57 58 int underscorePos = workString.indexOf("_"); 59 if (underscorePos != -1) { 60 workString = workString.substring(0, underscorePos).trim(); 61 } 62 63 int hyphenPos = workString.indexOf("-"); 64 if (hyphenPos != -1) { 65 workString = workString.substring(0, hyphenPos).trim(); 66 } 67 68 StringTokenizer versionTokenizer = new StringTokenizer (workString, "."); 69 while (versionTokenizer.hasMoreTokens()) { 70 String curToken = versionTokenizer.nextToken().trim(); 71 int curVersionNumber = Integer.parseInt(curToken); 72 orderedVersionNumbers.add(new Integer (curVersionNumber)); 73 } 74 } 77 78 82 public boolean isBeta() { 83 if (betaNumber != -1) { 84 return true; 85 } else { 86 return false; 87 } 88 } 89 90 94 public boolean isReleaseCandidate() { 95 if (releaseCandidateNumber != -1) { 96 return true; 97 } else { 98 return false; 99 } 100 } 101 102 107 public boolean isFinal() { 108 if ( (betaNumber == -1) && 109 (releaseCandidateNumber == -1) ) { 110 return true; 111 } else { 112 return false; 113 } 114 } 115 116 122 public String toString() { 123 StringBuffer result = new StringBuffer (); 124 for (int i=0; i < orderedVersionNumbers.size(); i++) { 125 Integer curVersionNumber = (Integer ) orderedVersionNumbers.get(i); 126 result.append(curVersionNumber.intValue()); 127 if (i < (orderedVersionNumbers.size() -1) ) { 128 result.append("."); 129 } 130 } 131 if (betaNumber != -1) { 132 result.append("b"); 133 result.append(betaNumber); 134 } else if (releaseCandidateNumber != -1) { 135 result.append("rc"); 136 result.append(releaseCandidateNumber); 137 } 138 return result.toString(); 139 } 140 141 164 public int compareTo(Object o) 165 throws ClassCastException { 166 Version rightVersion = (Version) o; 167 ArrayList rightOrderedVersionNumbers = rightVersion.getOrderedVersionNumbers(); 168 169 if (this.equals(rightVersion)) { 170 return 0; 171 } 172 173 if (orderedVersionNumbers.size() == rightOrderedVersionNumbers.size()) { 174 for (int i = 0; i < orderedVersionNumbers.size(); i++) { 175 Integer versionNumber = (Integer ) orderedVersionNumbers.get(i); 176 Integer rightVersionNumber = (Integer ) rightOrderedVersionNumbers.get(i); 177 if (versionNumber.intValue() != rightVersionNumber.intValue()) { 178 return versionNumber.compareTo(rightVersionNumber); 179 } 180 } 181 if (isBeta() && rightVersion.isBeta()) { 184 if (betaNumber < rightVersion.getBetaNumber()) { 185 return -1; 186 } else { 187 return 1; 188 } 189 } 190 191 if (isReleaseCandidate() && rightVersion.isReleaseCandidate()) { 192 if (releaseCandidateNumber < rightVersion.getReleaseCandidateNumber()) { 193 return -1; 194 } else { 195 return 1; 196 } 197 } 198 199 if (isBeta() && rightVersion.isReleaseCandidate()) { return -1; } 200 if (isBeta() && rightVersion.isFinal()) { return -1; } 201 if (isReleaseCandidate() && rightVersion.isBeta()) { return 1; } 202 if (isReleaseCandidate() && rightVersion.isFinal()) { return -1; } 203 if (isFinal() && rightVersion.isBeta()) { return 1; } 204 if (isFinal() && rightVersion.isReleaseCandidate()) { return 1; } 205 206 logger.debug("Unable to compare two versions " + 207 this.toString() + " and " + 208 rightVersion.toString() + 209 " returning equality..."); 210 return 0; 211 212 } else if (orderedVersionNumbers.size() < rightOrderedVersionNumbers.size()) { 213 for (int i = 0; i < orderedVersionNumbers.size(); i++) { 215 Integer versionNumber = (Integer ) orderedVersionNumbers.get(i); 216 Integer rightVersionNumber = (Integer ) rightOrderedVersionNumbers.get(i); 217 if (versionNumber.intValue() != rightVersionNumber.intValue()) { 218 return versionNumber.compareTo(rightVersionNumber); 219 } 220 } 221 return -1; 222 } else { 223 for (int i = 0; i < rightOrderedVersionNumbers.size() ; i++) { 225 Integer versionNumber = (Integer ) orderedVersionNumbers.get(i); 226 Integer rightVersionNumber = (Integer ) rightOrderedVersionNumbers.get(i); 227 if (versionNumber.intValue() != rightVersionNumber.intValue()) { 228 return versionNumber.compareTo(rightVersionNumber); 229 } 230 } 231 return 1; 232 } 233 } 234 235 242 public ArrayList getOrderedVersionNumbers() { 243 return orderedVersionNumbers; 244 } 245 246 251 public int getBetaNumber() { 252 return betaNumber; 253 } 254 255 260 public int getReleaseCandidateNumber() { 261 return releaseCandidateNumber; 262 } 263 264 public boolean equals(Object obj) { 265 if (obj instanceof Version) { 266 Version rightVersion = (Version) obj; 267 ArrayList rightOrderedVersionNumbers = rightVersion.getOrderedVersionNumbers(); 268 if (orderedVersionNumbers.size() != rightOrderedVersionNumbers.size()) { 269 return false; 270 } 271 if (betaNumber != rightVersion.getBetaNumber()) { 272 return false; 273 } 274 if (releaseCandidateNumber != rightVersion.getReleaseCandidateNumber()) { 275 return false; 276 } 277 for (int i=0; i < orderedVersionNumbers.size(); i++) { 278 Integer leftVersionNumber = (Integer ) orderedVersionNumbers.get(i); 279 Integer rightVersionNumber = (Integer ) rightOrderedVersionNumbers.get(i); 280 if (leftVersionNumber.intValue() != rightVersionNumber.intValue()) { 281 return false; 282 } 283 } 284 return true; 286 } else { 287 return false; 288 } 289 } 290 291 } | Popular Tags |