1 11 12 package org.eclipse.jface.util; 13 14 import java.util.Collections ; 15 import java.util.List ; 16 import java.util.MissingResourceException ; 17 import java.util.ResourceBundle ; 18 import java.util.SortedSet ; 19 import java.util.TreeSet ; 20 21 28 public final class Util { 29 30 34 public static final SortedSet EMPTY_SORTED_SET = Collections 35 .unmodifiableSortedSet(new TreeSet ()); 36 37 41 public static final String ZERO_LENGTH_STRING = ""; 43 52 public static final void assertInstance(final Object object, final Class c) { 53 assertInstance(object, c, false); 54 } 55 56 69 private static final void assertInstance(final Object object, 70 final Class c, final boolean allowNull) { 71 if (object == null && allowNull) { 72 return; 73 } 74 75 if (object == null || c == null) { 76 throw new NullPointerException (); 77 } else if (!c.isInstance(object)) { 78 throw new IllegalArgumentException (); 79 } 80 } 81 82 94 public static final int compare(final boolean left, final boolean right) { 95 return left == false ? (right == true ? -1 : 0) : 1; 96 } 97 98 107 public static final int compare(final int left, final int right) { 108 return left - right; 109 } 110 111 121 public static final int compare(final Comparable left, 122 final Comparable right) { 123 if (left == null && right == null) { 124 return 0; 125 } else if (left == null) { 126 return -1; 127 } else if (right == null) { 128 return 1; 129 } else { 130 return left.compareTo(right); 131 } 132 } 133 134 146 public static final int compare(final Comparable [] left, 147 final Comparable [] right) { 148 if (left == null && right == null) { 149 return 0; 150 } else if (left == null) { 151 return -1; 152 } else if (right == null) { 153 return 1; 154 } else { 155 int l = left.length; 156 int r = right.length; 157 158 if (l != r) { 159 return l - r; 160 } 161 162 for (int i = 0; i < l; i++) { 163 int compareTo = compare(left[i], right[i]); 164 165 if (compareTo != 0) { 166 return compareTo; 167 } 168 } 169 170 return 0; 171 } 172 } 173 174 188 public static final int compare(final List left, final List right) { 189 if (left == null && right == null) { 190 return 0; 191 } else if (left == null) { 192 return -1; 193 } else if (right == null) { 194 return 1; 195 } else { 196 int l = left.size(); 197 int r = right.size(); 198 199 if (l != r) { 200 return l - r; 201 } 202 203 for (int i = 0; i < l; i++) { 204 int compareTo = compare((Comparable ) left.get(i), 205 (Comparable ) right.get(i)); 206 207 if (compareTo != 0) { 208 return compareTo; 209 } 210 } 211 212 return 0; 213 } 214 } 215 216 229 public static final boolean endsWith(final Object [] left, 230 final Object [] right, final boolean equals) { 231 if (left == null || right == null) { 232 return false; 233 } 234 235 int l = left.length; 236 int r = right.length; 237 238 if (r > l || !equals && r == l) { 239 return false; 240 } 241 242 for (int i = 0; i < r; i++) { 243 if (!equals(left[l - i - 1], right[r - i - 1])) { 244 return false; 245 } 246 } 247 248 return true; 249 } 250 251 262 public static final boolean equals(final Object left, final Object right) { 263 return left == null ? right == null : ((right != null) && left 264 .equals(right)); 265 } 266 267 282 public static final boolean equals(final Object [] leftArray, 283 final Object [] rightArray) { 284 if (leftArray == rightArray) { 285 return true; 286 } 287 288 if (leftArray == null) { 289 return (rightArray == null); 290 } else if (rightArray == null) { 291 return false; 292 } 293 294 if (leftArray.length != rightArray.length) { 295 return false; 296 } 297 298 for (int i = 0; i < leftArray.length; i++) { 299 final Object left = leftArray[i]; 300 final Object right = rightArray[i]; 301 final boolean equal = (left == null) ? (right == null) : (left 302 .equals(right)); 303 if (!equal) { 304 return false; 305 } 306 } 307 308 return true; 309 } 310 311 318 public static final int hashCode(final int i) { 319 return i; 320 } 321 322 331 public static final int hashCode(final Object object) { 332 return object != null ? object.hashCode() : 0; 333 } 334 335 345 public static final int hashCode(final Object [] objects) { 346 if (objects == null) { 347 return 0; 348 } 349 350 int hashCode = 89; 351 for (int i = 0; i < objects.length; i++) { 352 final Object object = objects[i]; 353 if (object != null) { 354 hashCode = hashCode * 31 + object.hashCode(); 355 } 356 } 357 358 return hashCode; 359 } 360 361 374 public static final boolean startsWith(final Object [] left, 375 final Object [] right, final boolean equals) { 376 if (left == null || right == null) { 377 return false; 378 } 379 380 int l = left.length; 381 int r = right.length; 382 383 if (r > l || !equals && r == l) { 384 return false; 385 } 386 387 for (int i = 0; i < r; i++) { 388 if (!equals(left[i], right[i])) { 389 return false; 390 } 391 } 392 393 return true; 394 } 395 396 404 public static final String toString(final Object [] array) { 405 if (array == null) { 406 return "null"; } 408 409 final StringBuffer buffer = new StringBuffer (); 410 buffer.append('['); 411 412 final int length = array.length; 413 for (int i = 0; i < length; i++) { 414 if (i != 0) { 415 buffer.append(','); 416 } 417 final Object object = array[i]; 418 final String element = String.valueOf(object); 419 buffer.append(element); 420 } 421 buffer.append(']'); 422 423 return buffer.toString(); 424 } 425 426 442 public static final String translateString( 443 final ResourceBundle resourceBundle, final String key, 444 final String defaultString) { 445 if (resourceBundle != null && key != null) { 446 try { 447 final String translatedString = resourceBundle.getString(key); 448 449 if (translatedString != null) { 450 return translatedString; 451 } 452 } catch (MissingResourceException eMissingResource) { 453 } 455 } 456 457 return defaultString; 458 } 459 460 463 private Util() { 464 } 466 } 467 | Popular Tags |