1 11 12 package org.eclipse.ui.internal.util; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.Collections ; 17 import java.util.HashMap ; 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.MissingResourceException ; 23 import java.util.ResourceBundle ; 24 import java.util.Set ; 25 import java.util.SortedMap ; 26 import java.util.SortedSet ; 27 import java.util.StringTokenizer ; 28 import java.util.TreeMap ; 29 import java.util.TreeSet ; 30 31 import org.eclipse.core.runtime.Assert; 32 import org.eclipse.core.runtime.CoreException; 33 import org.eclipse.core.runtime.IAdaptable; 34 import org.eclipse.core.runtime.IConfigurationElement; 35 import org.eclipse.core.runtime.IStatus; 36 import org.eclipse.core.runtime.Platform; 37 import org.eclipse.core.runtime.PlatformObject; 38 import org.eclipse.core.runtime.Status; 39 import org.eclipse.swt.widgets.Shell; 40 import org.eclipse.ui.IWorkbench; 41 import org.eclipse.ui.IWorkbenchWindow; 42 import org.eclipse.ui.PlatformUI; 43 import org.eclipse.ui.internal.WorkbenchPlugin; 44 45 public final class Util { 46 47 public final static SortedMap EMPTY_SORTED_MAP = Collections 48 .unmodifiableSortedMap(new TreeMap ()); 49 50 public final static SortedSet EMPTY_SORTED_SET = Collections 51 .unmodifiableSortedSet(new TreeSet ()); 52 53 public final static String ZERO_LENGTH_STRING = ""; 55 65 public static String safeString(String input) { 66 if (input != null) { 67 return input; 68 } 69 70 return ZERO_LENGTH_STRING; 71 } 72 73 94 public static Object getAdapter(Object sourceObject, Class adapterType) { 95 Assert.isNotNull(adapterType); 96 if (sourceObject == null) { 97 return null; 98 } 99 if (adapterType.isInstance(sourceObject)) { 100 return sourceObject; 101 } 102 103 if (sourceObject instanceof IAdaptable) { 104 IAdaptable adaptable = (IAdaptable) sourceObject; 105 106 Object result = adaptable.getAdapter(adapterType); 107 if (result != null) { 108 Assert.isTrue(adapterType.isInstance(result)); 110 return result; 111 } 112 } 113 114 if (!(sourceObject instanceof PlatformObject)) { 115 Object result = Platform.getAdapterManager().getAdapter(sourceObject, adapterType); 116 if (result != null) { 117 return result; 118 } 119 } 120 121 return null; 122 } 123 124 public static void assertInstance(Object object, Class c) { 125 assertInstance(object, c, false); 126 } 127 128 public static void assertInstance(Object object, Class c, boolean allowNull) { 129 if (object == null && allowNull) { 130 return; 131 } 132 133 if (object == null || c == null) { 134 throw new NullPointerException (); 135 } else if (!c.isInstance(object)) { 136 throw new IllegalArgumentException (); 137 } 138 } 139 140 public static int compare(boolean left, boolean right) { 141 return left == false ? (right == true ? -1 : 0) : 1; 142 } 143 144 public static int compare(Comparable left, Comparable right) { 145 if (left == null && right == null) { 146 return 0; 147 } else if (left == null) { 148 return -1; 149 } else if (right == null) { 150 return 1; 151 } else { 152 return left.compareTo(right); 153 } 154 } 155 156 public static int compare(Comparable [] left, Comparable [] right) { 157 if (left == null && right == null) { 158 return 0; 159 } else if (left == null) { 160 return -1; 161 } else if (right == null) { 162 return 1; 163 } else { 164 int l = left.length; 165 int r = right.length; 166 167 if (l != r) { 168 return l - r; 169 } else { 170 for (int i = 0; i < l; i++) { 171 int compareTo = compare(left[i], right[i]); 172 173 if (compareTo != 0) { 174 return compareTo; 175 } 176 } 177 178 return 0; 179 } 180 } 181 } 182 183 public static int compare(int left, int right) { 184 return left - right; 185 } 186 187 public static int compare(List left, List right) { 188 if (left == null && right == null) { 189 return 0; 190 } else if (left == null) { 191 return -1; 192 } else if (right == null) { 193 return 1; 194 } else { 195 int l = left.size(); 196 int r = right.size(); 197 198 if (l != r) { 199 return l - r; 200 } else { 201 for (int i = 0; i < l; i++) { 202 int compareTo = compare((Comparable ) left.get(i), 203 (Comparable ) right.get(i)); 204 205 if (compareTo != 0) { 206 return compareTo; 207 } 208 } 209 210 return 0; 211 } 212 } 213 } 214 215 public static int compare(Object left, Object right) { 216 if (left == null && right == null) { 217 return 0; 218 } else if (left == null) { 219 return -1; 220 } else if (right == null) { 221 return 1; 222 } else if (left == right) { 223 return 0; 224 } else { 225 return compare(System.identityHashCode(left), System 226 .identityHashCode(right)); 227 } 228 } 229 230 244 public static final int compareIdentity(Object left, Object right) { 245 if (left == null && right == null) { 246 return 0; 247 } else if (left == null) { 248 return -1; 249 } else if (right == null) { 250 return 1; 251 } else { 252 return System.identityHashCode(left) 253 - System.identityHashCode(right); 254 } 255 } 256 257 public static void diff(Map left, Map right, Set leftOnly, Set different, 258 Set rightOnly) { 259 if (left == null || right == null || leftOnly == null 260 || different == null || rightOnly == null) { 261 throw new NullPointerException (); 262 } 263 264 Iterator iterator = left.keySet().iterator(); 265 266 while (iterator.hasNext()) { 267 Object key = iterator.next(); 268 269 if (!right.containsKey(key)) { 270 leftOnly.add(key); 271 } else if (!Util.equals(left.get(key), right.get(key))) { 272 different.add(key); 273 } 274 } 275 276 iterator = right.keySet().iterator(); 277 278 while (iterator.hasNext()) { 279 Object key = iterator.next(); 280 281 if (!left.containsKey(key)) { 282 rightOnly.add(key); 283 } 284 } 285 } 286 287 public static void diff(Set left, Set right, Set leftOnly, Set rightOnly) { 288 if (left == null || right == null || leftOnly == null 289 || rightOnly == null) { 290 throw new NullPointerException (); 291 } 292 293 Iterator iterator = left.iterator(); 294 295 while (iterator.hasNext()) { 296 Object object = iterator.next(); 297 298 if (!right.contains(object)) { 299 leftOnly.add(object); 300 } 301 } 302 303 iterator = right.iterator(); 304 305 while (iterator.hasNext()) { 306 Object object = iterator.next(); 307 308 if (!left.contains(object)) { 309 rightOnly.add(object); 310 } 311 } 312 } 313 314 public static boolean endsWith(List left, List right, boolean equals) { 315 if (left == null || right == null) { 316 return false; 317 } else { 318 int l = left.size(); 319 int r = right.size(); 320 321 if (r > l || !equals && r == l) { 322 return false; 323 } 324 325 for (int i = 0; i < r; i++) { 326 if (!equals(left.get(l - i - 1), right.get(r - i - 1))) { 327 return false; 328 } 329 } 330 331 return true; 332 } 333 } 334 335 public static boolean endsWith(Object [] left, Object [] right, boolean equals) { 336 if (left == null || right == null) { 337 return false; 338 } else { 339 int l = left.length; 340 int r = right.length; 341 342 if (r > l || !equals && r == l) { 343 return false; 344 } 345 346 for (int i = 0; i < r; i++) { 347 if (!equals(left[l - i - 1], right[r - i - 1])) { 348 return false; 349 } 350 } 351 352 return true; 353 } 354 } 355 356 public static boolean equals(boolean left, boolean right) { 357 return left == right; 358 } 359 360 public static boolean equals(int left, int right) { 361 return left == right; 362 } 363 364 public static boolean equals(Object left, Object right) { 365 return left == null ? right == null : ((right != null) && left 366 .equals(right)); 367 } 368 369 384 public static final boolean equals(final Object [] leftArray, 385 final Object [] rightArray) { 386 if (leftArray == rightArray) { 387 return true; 388 } 389 390 if (leftArray == null) { 391 return (rightArray == null); 392 } else if (rightArray == null) { 393 return false; 394 } 395 396 if (leftArray.length != rightArray.length) { 397 return false; 398 } 399 400 for (int i = 0; i < leftArray.length; i++) { 401 final Object left = leftArray[i]; 402 final Object right = rightArray[i]; 403 final boolean equal = (left == null) ? (right == null) : (left 404 .equals(right)); 405 if (!equal) { 406 return false; 407 } 408 } 409 410 return true; 411 } 412 413 public static int hashCode(boolean b) { 414 return b ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode(); 415 } 416 417 public static int hashCode(int i) { 418 return i; 419 } 420 421 public static int hashCode(Object object) { 422 return object != null ? object.hashCode() : 0; 423 } 424 425 public static Collection safeCopy(Collection collection, Class c) { 426 return safeCopy(collection, c, false); 427 } 428 429 public static Collection safeCopy(Collection collection, Class c, 430 boolean allowNullElements) { 431 if (collection == null || c == null) { 432 throw new NullPointerException (); 433 } 434 435 collection = Collections.unmodifiableCollection(new ArrayList ( 436 collection)); 437 Iterator iterator = collection.iterator(); 438 439 while (iterator.hasNext()) { 440 assertInstance(iterator.next(), c, allowNullElements); 441 } 442 443 return collection; 444 } 445 446 public static List safeCopy(List list, Class c) { 447 return safeCopy(list, c, false); 448 } 449 450 public static List safeCopy(List list, Class c, boolean allowNullElements) { 451 if (list == null || c == null) { 452 throw new NullPointerException (); 453 } 454 455 list = Collections.unmodifiableList(new ArrayList (list)); 456 Iterator iterator = list.iterator(); 457 458 while (iterator.hasNext()) { 459 assertInstance(iterator.next(), c, allowNullElements); 460 } 461 462 return list; 463 } 464 465 public static Map safeCopy(Map map, Class keyClass, Class valueClass) { 466 return safeCopy(map, keyClass, valueClass, false, false); 467 } 468 469 public static Map safeCopy(Map map, Class keyClass, Class valueClass, 470 boolean allowNullKeys, boolean allowNullValues) { 471 if (map == null || keyClass == null || valueClass == null) { 472 throw new NullPointerException (); 473 } 474 475 map = Collections.unmodifiableMap(new HashMap (map)); 476 Iterator iterator = map.entrySet().iterator(); 477 478 while (iterator.hasNext()) { 479 Map.Entry entry = (Map.Entry ) iterator.next(); 480 assertInstance(entry.getKey(), keyClass, allowNullKeys); 481 assertInstance(entry.getValue(), valueClass, allowNullValues); 482 } 483 484 return map; 485 } 486 487 public static Set safeCopy(Set set, Class c) { 488 return safeCopy(set, c, false); 489 } 490 491 public static Set safeCopy(Set set, Class c, boolean allowNullElements) { 492 if (set == null || c == null) { 493 throw new NullPointerException (); 494 } 495 496 set = Collections.unmodifiableSet(new HashSet (set)); 497 Iterator iterator = set.iterator(); 498 499 while (iterator.hasNext()) { 500 assertInstance(iterator.next(), c, allowNullElements); 501 } 502 503 return set; 504 } 505 506 public static SortedMap safeCopy(SortedMap sortedMap, Class keyClass, 507 Class valueClass) { 508 return safeCopy(sortedMap, keyClass, valueClass, false, false); 509 } 510 511 public static SortedMap safeCopy(SortedMap sortedMap, Class keyClass, 512 Class valueClass, boolean allowNullKeys, boolean allowNullValues) { 513 if (sortedMap == null || keyClass == null || valueClass == null) { 514 throw new NullPointerException (); 515 } 516 517 sortedMap = Collections.unmodifiableSortedMap(new TreeMap (sortedMap)); 518 Iterator iterator = sortedMap.entrySet().iterator(); 519 520 while (iterator.hasNext()) { 521 Map.Entry entry = (Map.Entry ) iterator.next(); 522 assertInstance(entry.getKey(), keyClass, allowNullKeys); 523 assertInstance(entry.getValue(), valueClass, allowNullValues); 524 } 525 526 return sortedMap; 527 } 528 529 public static SortedSet safeCopy(SortedSet sortedSet, Class c) { 530 return safeCopy(sortedSet, c, false); 531 } 532 533 public static SortedSet safeCopy(SortedSet sortedSet, Class c, 534 boolean allowNullElements) { 535 if (sortedSet == null || c == null) { 536 throw new NullPointerException (); 537 } 538 539 sortedSet = Collections.unmodifiableSortedSet(new TreeSet (sortedSet)); 540 Iterator iterator = sortedSet.iterator(); 541 542 while (iterator.hasNext()) { 543 assertInstance(iterator.next(), c, allowNullElements); 544 } 545 546 return sortedSet; 547 } 548 549 public static boolean startsWith(List left, List right, boolean equals) { 550 if (left == null || right == null) { 551 return false; 552 } else { 553 int l = left.size(); 554 int r = right.size(); 555 556 if (r > l || !equals && r == l) { 557 return false; 558 } 559 560 for (int i = 0; i < r; i++) { 561 if (!equals(left.get(i), right.get(i))) { 562 return false; 563 } 564 } 565 566 return true; 567 } 568 } 569 570 public static boolean startsWith(Object [] left, Object [] right, 571 boolean equals) { 572 if (left == null || right == null) { 573 return false; 574 } else { 575 int l = left.length; 576 int r = right.length; 577 578 if (r > l || !equals && r == l) { 579 return false; 580 } 581 582 for (int i = 0; i < r; i++) { 583 if (!equals(left[i], right[i])) { 584 return false; 585 } 586 } 587 588 return true; 589 } 590 } 591 592 public static String translateString(ResourceBundle resourceBundle, 593 String key) { 594 return Util.translateString(resourceBundle, key, key, true, true); 595 } 596 597 public static String translateString(ResourceBundle resourceBundle, 598 String key, String string, boolean signal, boolean trim) { 599 if (resourceBundle != null && key != null) { 600 try { 601 final String translatedString = resourceBundle.getString(key); 602 603 if (translatedString != null) { 604 return trim ? translatedString.trim() : translatedString; 605 } 606 } catch (MissingResourceException eMissingResource) { 607 if (signal) { 608 WorkbenchPlugin.log(eMissingResource); 609 } 610 } 611 } 612 613 return trim ? string.trim() : string; 614 } 615 616 public static void arrayCopyWithRemoval(Object [] src, Object [] dst, int idxToRemove) { 617 if (src == null || dst == null || src.length - 1 != dst.length || idxToRemove < 0 || idxToRemove >= src.length) { 618 throw new IllegalArgumentException (); 619 } 620 621 if (idxToRemove == 0) { 622 System.arraycopy(src, 1, dst, 0, src.length - 1); 623 } 624 else if (idxToRemove == src.length - 1) { 625 System.arraycopy(src, 0, dst, 0, src.length - 1); 626 } 627 else { 628 System.arraycopy(src, 0, dst, 0, idxToRemove); 629 System.arraycopy(src, idxToRemove + 1, dst, idxToRemove, src.length - idxToRemove - 1); 630 } 631 } 632 633 641 public static Object [] appendArray(Object [] array1, Object [] array2) { 642 Object [] result = new Object [array1.length + array2.length]; 643 System.arraycopy(array1, 0, result, 0, array1.length); 644 System.arraycopy(array2, 0, result, array1.length, array2.length); 645 return result; 646 } 647 648 private Util() { 649 } 650 651 656 public static String intern(String string) { 657 return string == null ? null : string.intern(); 658 } 659 660 670 public static String [] getArrayFromList(String prop, String separator) { 671 if (prop == null || prop.trim().equals("")) { return new String [0]; 673 } 674 ArrayList list = new ArrayList (); 675 StringTokenizer tokens = new StringTokenizer (prop, separator); 676 while (tokens.hasMoreTokens()) { 677 String token = tokens.nextToken().trim(); 678 if (!token.equals("")) { list.add(token); 680 } 681 } 682 return list.isEmpty() ? new String [0] : (String []) list.toArray(new String [list.size()]); 683 } 684 685 695 public static IWorkbenchWindow getWorkbenchWindowForShell(Shell shellToCheck) { 696 IWorkbenchWindow workbenchWindow = null; 697 while (workbenchWindow == null && shellToCheck != null) { 698 if (shellToCheck.getData() instanceof IWorkbenchWindow) { 699 workbenchWindow = (IWorkbenchWindow) shellToCheck.getData(); 700 } else { 701 shellToCheck = (Shell) shellToCheck.getParent(); 702 } 703 } 704 705 if (workbenchWindow == null) { 706 workbenchWindow = PlatformUI.getWorkbench() 707 .getActiveWorkbenchWindow(); 708 } 709 710 return workbenchWindow; 711 } 712 713 722 public static Shell getShellToParentOn() { 723 IWorkbench workbench = PlatformUI.getWorkbench(); 724 IWorkbenchWindow activeWindow = workbench.getActiveWorkbenchWindow(); 725 IWorkbenchWindow windowToParentOn = activeWindow == null ? (workbench 726 .getWorkbenchWindowCount() > 0 ? workbench 727 .getWorkbenchWindows()[0] : null) : activeWindow; 728 return windowToParentOn == null ? null : activeWindow.getShell(); 729 } 730 731 741 public static String [] split(String src, char delim) { 742 if (src == null) 743 return null; 744 745 String [] splitStr = new String [2]; 746 int delimIndex = src.indexOf(delim); 747 if (delimIndex == -1 || delimIndex == 0 || delimIndex == src.length()-1) { 748 splitStr[0] = src; 749 splitStr[1] = ZERO_LENGTH_STRING; 750 return splitStr; 751 } 752 753 splitStr[0] = src.substring(0, delimIndex); 754 splitStr[1] = src.substring(delimIndex+1); 755 756 return splitStr; 757 } 758 759 768 public static String replaceAll(String src, String find, String replacement) { 769 final int len = src.length(); 770 final int findLen = find.length(); 771 772 int idx = src.indexOf(find); 773 if (idx < 0) { 774 return src; 775 } 776 777 StringBuffer buf = new StringBuffer (); 778 int beginIndex = 0; 779 while (idx != -1 && idx < len) { 780 buf.append(src.substring(beginIndex, idx)); 781 buf.append(replacement); 782 783 beginIndex = idx + findLen; 784 if (beginIndex < len) { 785 idx = src.indexOf(find, beginIndex); 786 } else { 787 idx = -1; 788 } 789 } 790 if (beginIndex<len) { 791 buf.append(src.substring(beginIndex, (idx==-1?len:idx))); 792 } 793 return buf.toString(); 794 } 795 796 812 public static Object safeLoadExecutableExtension(IConfigurationElement element, 813 String attName, Class classSpec) { 814 Object loadedEE = null; 815 816 try { 818 loadedEE = element.createExecutableExtension(attName); 819 } catch (final CoreException e) { 820 final String classDef = element.getAttribute(attName); 823 824 final String message = "Class load Failure: '" + classDef + "'"; IStatus status = new Status(IStatus.ERROR, 826 WorkbenchPlugin.PI_WORKBENCH, 0, message, e); 827 WorkbenchPlugin.log(message, status); 828 } 829 830 if (classSpec != null && loadedEE != null && !classSpec.isInstance(loadedEE)) { 832 final String message = "Loaded class is of incorrect type: expected(" + classSpec.getName() + ") got (" + loadedEE.getClass().getName() + ")"; 836 IllegalArgumentException e = new IllegalArgumentException (message); 837 final IStatus status = new Status(IStatus.ERROR, 838 WorkbenchPlugin.PI_WORKBENCH, 0, message, e); 839 WorkbenchPlugin.log(message, status); 840 841 loadedEE = null; 843 } 844 845 return loadedEE; 846 } 847 848 } 849 | Popular Tags |