1 19 package org.openide.modules; 20 21 22 import java.util.*; 26 27 28 32 public final class SpecificationVersion implements Comparable { 33 private static final Map<String ,int[]> parseCache = new HashMap<String ,int[]>(200); 38 private final int[] digits; 39 40 41 public SpecificationVersion(String version) throws NumberFormatException { 42 synchronized (parseCache) { 43 int[] d = (int[]) parseCache.get(version); 44 45 if (d == null) { 46 d = parse(version); 47 parseCache.put(version.intern(), d); 48 } 49 50 digits = d; 51 } 52 } 53 54 private static int[] parse(String version) throws NumberFormatException { 55 StringTokenizer tok = new StringTokenizer(version, ".", true); 57 int len = tok.countTokens(); 58 if ((len % 2) == 0) { 59 throw new NumberFormatException ("Even number of pieces in a spec version: `" + version + "'"); } 61 int[] digits = new int[len / 2 + 1]; 62 int i = 0; 63 64 boolean expectingNumber = true; 65 66 while (tok.hasMoreTokens()) { 67 if (expectingNumber) { 68 expectingNumber = false; 69 70 int piece = Integer.parseInt(tok.nextToken()); 71 72 if (piece < 0) { 73 throw new NumberFormatException ("Spec version component <0: " + piece); } 75 76 digits[i++] = piece; 77 } else { 78 if (!".".equals(tok.nextToken())) { throw new NumberFormatException ("Expected dot in spec version: `" + version + "'"); } 81 82 expectingNumber = true; 83 } 84 } 85 return digits; 86 } 87 88 89 public int compareTo(Object o) { 90 int[] od = ((SpecificationVersion) o).digits; 91 int len1 = digits.length; 92 int len2 = od.length; 93 int max = Math.max(len1, len2); 94 95 for (int i = 0; i < max; i++) { 96 int d1 = ((i < len1) ? digits[i] : 0); 97 int d2 = ((i < len2) ? od[i] : 0); 98 99 if (d1 != d2) { 100 return d1 - d2; 101 } 102 } 103 104 return 0; 105 } 106 107 108 public boolean equals(Object o) { 109 if (!(o instanceof SpecificationVersion)) { 110 return false; 111 } 112 113 return Arrays.equals(digits, ((SpecificationVersion) o).digits); 114 } 115 116 117 public int hashCode() { 118 int hash = 925295; 119 int len = digits.length; 120 121 for (int i = 0; i < len; i++) { 122 hash ^= (digits[i] << i); 123 } 124 125 return hash; 126 } 127 128 129 public String toString() { 130 StringBuilder buf = new StringBuilder ((digits.length * 3) + 1); 131 132 for (int i = 0; i < digits.length; i++) { 133 if (i > 0) { 134 buf.append('.'); } 136 137 buf.append(digits[i]); 138 } 139 140 return buf.toString(); 141 } 142 } 143 | Popular Tags |