1 36 37 package jnlp.sample.util; 38 39 import java.util.ArrayList ; 40 import java.util.Arrays ; 41 42 49 public class VersionID implements Comparable { 50 private String [] _tuple; private boolean _usePrefixMatch; private boolean _useGreaterThan; private boolean _isCompound; private VersionID _rest; 56 57 public VersionID(String str) { 58 _usePrefixMatch = false; 59 _useGreaterThan = false; 60 _isCompound = false; 61 if (str == null && str.length() == 0) { 62 _tuple = new String [0]; 63 return; 64 } 65 66 int amp = str.indexOf("&"); 68 if (amp >= 0) { 69 _isCompound = true; 70 VersionID firstPart = new VersionID(str.substring(0, amp)); 71 _rest = new VersionID(str.substring(amp+1)); 72 _tuple = firstPart._tuple; 73 _usePrefixMatch = firstPart._usePrefixMatch; 74 _useGreaterThan = firstPart._useGreaterThan; 75 } else { 76 if (str.endsWith("+")) { 78 _useGreaterThan = true; 79 str = str.substring(0, str.length() - 1); 80 } else if (str.endsWith("*")) { 81 _usePrefixMatch = true; 82 str = str.substring(0, str.length() - 1); 83 } 84 85 ArrayList list = new ArrayList (); 86 int start = 0; 87 for(int i = 0; i < str.length(); i++) { 88 if (".-_".indexOf(str.charAt(i)) != -1) { 90 if (start < i) { 91 String value = str.substring(start, i); 92 list.add(value); 93 } 94 start = i + 1; 95 } 96 } 97 if (start < str.length()) { 98 list.add(str.substring(start, str.length())); 99 } 100 _tuple = new String [list.size()]; 101 _tuple = (String [])list.toArray(_tuple); 102 } 103 } 104 105 106 public boolean isSimpleVersion() { 107 return !_useGreaterThan && !_usePrefixMatch && !_isCompound; 108 } 109 110 115 public boolean match(VersionID vid) { 116 if (_isCompound) { 117 if (!_rest.match(vid)) { 118 return false; 119 } 120 } 121 return (_usePrefixMatch) ? this.isPrefixMatch(vid) : 122 (_useGreaterThan) ? vid.isGreaterThanOrEqual(this) : 123 matchTuple(vid); 124 } 125 126 127 public boolean equals(Object o) { 128 if (matchTuple(o)) { 129 VersionID ov = (VersionID) o; 130 if (_rest == null || _rest.equals(ov._rest)) { 131 if ((_useGreaterThan == ov._useGreaterThan) && 132 (_usePrefixMatch == ov._usePrefixMatch)) { 133 return true; 134 } 135 } 136 } 137 return false; 138 } 139 140 141 private boolean matchTuple(Object o) { 142 if (o == null || !(o instanceof VersionID)) return false; 144 VersionID vid = (VersionID)o; 145 146 String [] t1 = normalize(_tuple, vid._tuple.length); 148 String [] t2 = normalize(vid._tuple, _tuple.length); 149 150 for(int i = 0; i < t1.length; i++) { 152 Object o1 = getValueAsObject(t1[i]); 153 Object o2 = getValueAsObject(t2[i]); 154 if (!o1.equals(o2)) return false; 155 } 156 return true; 157 } 158 159 private Object getValueAsObject(String value) { 160 if (value.length() > 0 && value.charAt(0) != '-') { 161 try { return Integer.valueOf(value); 162 } catch(NumberFormatException nfe) { } 163 } 164 return value; 165 } 166 167 public boolean isGreaterThan(VersionID vid) { 168 return isGreaterThanOrEqualHelper(vid, false); 169 } 170 171 public boolean isGreaterThanOrEqual(VersionID vid) { 172 return isGreaterThanOrEqualHelper(vid, true); 173 } 174 175 176 private boolean isGreaterThanOrEqualHelper(VersionID vid, 177 boolean allowEqual) { 178 179 if (_isCompound) { 180 if (!_rest.isGreaterThanOrEqualHelper(vid, allowEqual)) { 181 return false; 182 } 183 } 184 String [] t1 = normalize(_tuple, vid._tuple.length); 186 String [] t2 = normalize(vid._tuple, _tuple.length); 187 188 for(int i = 0; i < t1.length; i++) { 189 Object e1 = getValueAsObject(t1[i]); 191 Object e2 = getValueAsObject(t2[i]); 192 if (e1.equals(e2)) { 193 } else { 195 if (e1 instanceof Integer && e2 instanceof Integer ) { 196 return ((Integer )e1).intValue() > ((Integer )e2).intValue(); 197 } else { 198 String s1 = t1[i].toString(); 199 String s2 = t2[i].toString(); 200 return s1.compareTo(s2) > 0; 201 } 202 203 } 204 } 205 return allowEqual; 207 } 208 209 210 public boolean isPrefixMatch(VersionID vid) { 211 212 if (_isCompound) { 213 if (!_rest.isPrefixMatch(vid)) { 214 return false; 215 } 216 } 217 String [] t2 = normalize(vid._tuple, _tuple.length); 219 220 for(int i = 0; i < _tuple.length; i++) { 221 Object e1 = _tuple[i]; 222 Object e2 = t2[i]; 223 if (e1.equals(e2)) { 224 } else { 226 return false; 228 } 229 } 230 return true; 231 } 232 233 234 private String [] normalize(String [] list, int minlength) { 235 if (list.length < minlength) { 236 String [] newlist = new String [minlength]; 238 System.arraycopy(list, 0, newlist, 0, list.length); 239 Arrays.fill(newlist, list.length, newlist.length, "0"); 240 return newlist; 241 } else { 242 return list; 243 } 244 } 245 246 public int compareTo(Object o) { 247 if (o == null || !(o instanceof VersionID)) return -1; 248 VersionID vid = (VersionID)o; 249 return equals(vid) ? 0 : (isGreaterThanOrEqual(vid) ? 1 : -1); 250 } 251 252 public String toString() { 253 StringBuffer sb = new StringBuffer (); 254 for(int i = 0; i < _tuple.length -1; i++) { 255 sb.append(_tuple[i]); 256 sb.append('.'); 257 } 258 if (_tuple.length > 0 ) sb.append(_tuple[_tuple.length - 1]); 259 if (_usePrefixMatch) sb.append('+'); 260 return sb.toString(); 261 } 262 } 263 264 | Popular Tags |