1 21 package oracle.toplink.essentials.internal.helper; 23 24 import java.util.*; 25 import java.io.*; 26 import java.lang.reflect.*; 27 import java.security.AccessController ; 28 import java.security.PrivilegedActionException ; 29 import java.sql.Timestamp ; 30 31 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 32 import oracle.toplink.essentials.internal.security.PrivilegedNewInstanceFromClass; 33 import oracle.toplink.essentials.internal.security.PrivilegedGetField; 34 import oracle.toplink.essentials.internal.security.PrivilegedGetMethod; 35 import oracle.toplink.essentials.exceptions.*; 36 37 42 public class Helper implements Serializable { 43 44 45 protected static boolean shouldOptimizeDates = false; 46 47 48 protected static Object nullWrapper = new Helper(); 49 50 51 protected static Vector calendarCache = new Vector(10); 52 53 54 protected static TimeZone defaultTimeZone = TimeZone.getDefault(); 55 56 58 59 protected static String CR = null; 60 61 62 protected static String PATH_SEPARATOR = null; 63 64 65 protected static String FILE_SEPARATOR = null; 66 67 68 protected static String CURRENT_WORKING_DIRECTORY = null; 69 70 71 protected static String TEMP_DIRECTORY = null; 72 73 76 public static boolean shouldOptimizeDates() { 77 return shouldOptimizeDates; 78 } 79 80 83 public static void setShouldOptimizeDates(boolean value) { 84 shouldOptimizeDates = value; 85 } 86 87 92 public static Calendar allocateCalendar() { 93 Calendar calendar = null; 94 synchronized (calendarCache) { 95 if (calendarCache.size() > 0) { 96 calendar = (Calendar)calendarCache.remove(calendarCache.size() - 1); 97 } 98 } 99 if (calendar == null) { 100 calendar = Calendar.getInstance(); 101 } 102 return calendar; 103 } 104 105 110 public static TimeZone getDefaultTimeZone() { 111 return defaultTimeZone; 112 } 113 114 119 public static void releaseCalendar(Calendar calendar) { 120 if (calendarCache.size() < 10) { 121 calendarCache.add(calendar); 122 } 123 } 124 125 public static void addAllToVector(Vector theVector, Vector elementsToAdd) { 126 for (Enumeration stream = elementsToAdd.elements(); stream.hasMoreElements();) { 127 theVector.addElement(stream.nextElement()); 128 } 129 } 130 131 public static void addAllToVector(Vector theVector, List elementsToAdd) { 132 theVector.addAll(elementsToAdd); 133 } 134 135 public static Vector addAllUniqueToVector(Vector theVector, Vector elementsToAdd) { 136 for (Enumeration stream = elementsToAdd.elements(); stream.hasMoreElements();) { 137 Object element = stream.nextElement(); 138 if (!theVector.contains(element)) { 139 theVector.addElement(element); 140 } 141 } 142 143 return theVector; 144 } 145 146 149 public static Object [] arrayFromVector(Vector vector) { 150 Object [] result = new Object [vector.size()]; 151 for (int i = 0; i < vector.size(); i++) { 152 result[i] = vector.elementAt(i); 153 } 154 return result; 155 } 156 157 161 public static byte[] buildBytesFromHexString(String hex) { 162 String tmpString = (String )hex; 163 if ((tmpString.length() % 2) != 0) { 164 throw ConversionException.couldNotConvertToByteArray(hex); 165 } 166 byte[] bytes = new byte[tmpString.length() / 2]; 167 int byteIndex; 168 int strIndex; 169 byte digit1; 170 byte digit2; 171 for (byteIndex = bytes.length - 1, strIndex = tmpString.length() - 2; byteIndex >= 0; 172 byteIndex--, strIndex -= 2) { 173 digit1 = (byte)Character.digit(tmpString.charAt(strIndex), 16); 174 digit2 = (byte)Character.digit(tmpString.charAt(strIndex + 1), 16); 175 if ((digit1 == -1) || (digit2 == -1)) { 176 throw ConversionException.couldNotBeConverted(hex, ClassConstants.APBYTE); 177 } 178 bytes[byteIndex] = (byte)((digit1 * 16) + digit2); 179 } 180 return bytes; 181 } 182 183 187 public static Hashtable buildHashtableFromVector(Vector theVector) { 188 Hashtable toReturn = new Hashtable(theVector.size()); 189 190 Iterator iter = theVector.iterator(); 191 while (iter.hasNext()) { 192 Object next = iter.next(); 193 toReturn.put(next, next); 194 } 195 return toReturn; 196 } 197 198 202 public static String buildHexStringFromBytes(byte[] bytes) { 203 char[] hexArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 204 StringBuffer stringBuffer = new StringBuffer (); 205 int tempByte; 206 for (int byteIndex = 0; byteIndex < ((byte[])bytes).length; byteIndex++) { 207 tempByte = ((byte[])bytes)[byteIndex]; 208 if (tempByte < 0) { 209 tempByte = tempByte + 256; } 211 tempByte = (byte)(tempByte / 16); if (tempByte > 16) { 213 throw ConversionException.couldNotBeConverted(bytes, ClassConstants.STRING); 214 } 215 stringBuffer.append(hexArray[tempByte]); 216 217 tempByte = ((byte[])bytes)[byteIndex]; 218 if (tempByte < 0) { 219 tempByte = tempByte + 256; 220 } 221 tempByte = (byte)(tempByte % 16); if (tempByte > 16) { 223 throw ConversionException.couldNotBeConverted(bytes, ClassConstants.STRING); 224 } 225 stringBuffer.append(hexArray[tempByte]); 226 } 227 return stringBuffer.toString(); 228 } 229 230 234 public static Vector buildVectorFromHashtableElements(Hashtable hashtable) { 235 Vector vector = new Vector(hashtable.size()); 236 Enumeration enumeration = hashtable.elements(); 237 238 while (enumeration.hasMoreElements()) { 239 vector.addElement(enumeration.nextElement()); 240 } 241 242 return vector; 243 } 244 245 248 public static Vector buildVectorFromMapElements(Map map) { 249 Vector vector = new Vector(map.size()); 250 Iterator iterator = map.values().iterator(); 251 252 while (iterator.hasNext()) { 253 vector.addElement(iterator.next()); 254 } 255 256 return vector; 257 } 258 259 263 public static Vector buildVectorFromHashtableElements(IdentityHashtable hashtable) { 264 Vector vector = new Vector(hashtable.size()); 265 Enumeration enumeration = hashtable.elements(); 266 267 while (enumeration.hasMoreElements()) { 268 vector.addElement(enumeration.nextElement()); 269 } 270 271 return vector; 272 } 273 274 277 public static Calendar calendarFromUtilDate(java.util.Date date) { 278 Calendar calendar = Calendar.getInstance(); 279 calendar.setTime(date); 280 if (date instanceof Timestamp ) { 282 calendar.set(Calendar.MILLISECOND, ((Timestamp )date).getNanos() / 1000000); 283 } 284 return calendar; 285 } 286 287 293 public static boolean classImplementsInterface(Class aClass, Class anInterface) { 294 if (aClass == anInterface) { 296 return true; 297 } 298 299 Class [] interfaces = aClass.getInterfaces(); 300 301 for (int i = 0; i < interfaces.length; i++) { 303 if (interfaces[i] == anInterface) { 304 return true; 305 } 306 } 307 308 for (int i = 0; i < interfaces.length; i++) { 310 if (classImplementsInterface(interfaces[i], anInterface)) { 311 return true; 312 } 313 } 314 315 Class superClass = aClass.getSuperclass(); 317 if (superClass == null) { 318 return false; 319 } 320 return classImplementsInterface(superClass, anInterface); 321 } 322 323 328 public static boolean classIsSubclass(Class subClass, Class superClass) { 329 Class temp = subClass; 330 331 if (superClass == null) { 332 return false; 333 } 334 335 while (temp != null) { 336 if (temp == superClass) { 337 return true; 338 } 339 temp = temp.getSuperclass(); 340 } 341 return false; 342 } 343 344 public static boolean compareArrays(Object [] array1, Object [] array2) { 345 if (array1.length != array2.length) { 346 return false; 347 } 348 for (int index = 0; index < array1.length; index++) { 349 if (!array1[index].equals(array2[index])) { 351 return false; 352 } 353 } 354 return true; 355 } 356 357 363 public static boolean compareBigDecimals(java.math.BigDecimal one, java.math.BigDecimal two) { 364 if (one.scale() != two.scale()) { 365 double doubleOne = ((java.math.BigDecimal )one).doubleValue(); 366 double doubleTwo = ((java.math.BigDecimal )two).doubleValue(); 367 if ((doubleOne != Double.POSITIVE_INFINITY) && (doubleOne != Double.NEGATIVE_INFINITY) && (doubleTwo != Double.POSITIVE_INFINITY) && (doubleTwo != Double.NEGATIVE_INFINITY)) { 368 return doubleOne == doubleTwo; 369 } 370 } 371 return one.equals(two); 372 } 373 374 public static boolean compareByteArrays(byte[] array1, byte[] array2) { 375 if (array1.length != array2.length) { 376 return false; 377 } 378 for (int index = 0; index < array1.length; index++) { 379 if (array1[index] != array2[index]) { 380 return false; 381 } 382 } 383 return true; 384 } 385 386 public static boolean compareCharArrays(char[] array1, char[] array2) { 387 if (array1.length != array2.length) { 388 return false; 389 } 390 for (int index = 0; index < array1.length; index++) { 391 if (array1[index] != array2[index]) { 392 return false; 393 } 394 } 395 return true; 396 } 397 398 405 public static boolean areTypesAssignable(Vector types1, Vector types2) { 406 if ((types1 == null) || (types2 == null)) { 407 return false; 408 } 409 410 if (types1.size() == types2.size()) { 411 for (int i = 0; i < types1.size(); i++) { 412 Class type1 = (Class )types1.elementAt(i); 413 Class type2 = (Class )types2.elementAt(i); 414 415 if ((type1 != null) && (type2 != null)) { 417 if (!type1.isAssignableFrom(type2)) { 418 return false; 419 } 420 } 421 } 422 return true; 423 } 424 425 return false; 426 } 427 428 434 public static boolean compareHashtables(Hashtable hashtable1, Hashtable hashtable2) { 435 Enumeration enumtr; 436 Object element; 437 Hashtable clonedHashtable; 438 439 if (hashtable1.size() != hashtable2.size()) { 440 return false; 441 } 442 443 clonedHashtable = (Hashtable)hashtable2.clone(); 444 445 enumtr = hashtable1.elements(); 446 while (enumtr.hasMoreElements()) { 447 element = enumtr.nextElement(); 448 if (clonedHashtable.remove(element) == null) { 449 return false; 450 } 451 } 452 453 return clonedHashtable.isEmpty(); 454 } 455 456 461 public static boolean compareOrderedVectors(Vector vector1, Vector vector2) { 462 if (vector1 == vector2) { 463 return true; 464 } 465 if (vector1.size() != vector2.size()) { 466 return false; 467 } 468 for (int index = 0; index < vector1.size(); index++) { 469 Object element1 = vector1.elementAt(index); 470 Object element2 = vector2.elementAt(index); 471 if (element1 == null) { if (element2 != null) { 473 return false; 474 } 475 } else { 476 if (!element1.equals(element2)) { 477 return false; 478 } 479 } 480 } 481 return true; 482 } 483 484 491 public static boolean compareUnorderedVectors(Vector v1, Vector v2) { 492 if (v1 == v2) { 493 return true; 494 } 495 if (v1.size() != v2.size()) { 496 return false; 497 } 498 499 Vector v3 = (Vector)v2.clone(); 503 for (int i = 0; i < v1.size(); i++) { 504 Object e1 = v1.elementAt(i); 505 if (e1 == null) { if (!removeNullElement(v3)) { 508 return false; 509 } 510 } else { 511 if (!v3.removeElement(e1)) { 513 return false; 514 } 515 } 516 } 517 return true; 518 } 519 520 public static Hashtable concatenateHashtables(Hashtable first, Hashtable second) { 521 Hashtable concatenation; 522 Object key; 523 Object value; 524 525 concatenation = new Hashtable(first.size() + second.size() + 4); 526 527 for (Enumeration keys = first.keys(); keys.hasMoreElements();) { 528 key = keys.nextElement(); 529 value = first.get(key); 530 concatenation.put(key, value); 531 } 532 533 for (Enumeration keys = second.keys(); keys.hasMoreElements();) { 534 key = keys.nextElement(); 535 value = second.get(key); 536 concatenation.put(key, value); 537 } 538 539 return concatenation; 540 } 541 542 545 public static Map concatenateMaps(Map first, Map second) { 546 Map concatenation = new HashMap(first.size() + second.size() + 4); 547 548 for (Iterator keys = first.keySet().iterator(); keys.hasNext();) { 549 Object key = keys.next(); 550 Object value = first.get(key); 551 concatenation.put(key, value); 552 } 553 554 for (Iterator keys = second.keySet().iterator(); keys.hasNext();) { 555 Object key = keys.next(); 556 Object value = second.get(key); 557 concatenation.put(key, value); 558 } 559 560 return concatenation; 561 } 562 563 567 public static Vector concatenateUniqueVectors(Vector first, Vector second) { 568 Vector concatenation; 569 Object element; 570 571 concatenation = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(); 572 573 for (Enumeration stream = first.elements(); stream.hasMoreElements();) { 574 concatenation.addElement(stream.nextElement()); 575 } 576 577 for (Enumeration stream = second.elements(); stream.hasMoreElements();) { 578 element = stream.nextElement(); 579 if (!concatenation.contains(element)) { 580 concatenation.addElement(element); 581 } 582 } 583 584 return concatenation; 585 586 } 587 588 public static Vector concatenateVectors(Vector first, Vector second) { 589 Vector concatenation; 590 591 concatenation = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(); 592 593 for (Enumeration stream = first.elements(); stream.hasMoreElements();) { 594 concatenation.addElement(stream.nextElement()); 595 } 596 597 for (Enumeration stream = second.elements(); stream.hasMoreElements();) { 598 concatenation.addElement(stream.nextElement()); 599 } 600 601 return concatenation; 602 603 } 604 605 612 public static boolean containsNull(Vector v, int index) { 613 return indexOfNullElement(v, 0) != -1; 614 } 615 616 623 public static Vector copyVector(Vector originalVector, int startIndex, int stopIndex) throws ValidationException { 624 Vector newVector; 625 626 if (stopIndex < startIndex) { 627 return oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(); 628 } 629 630 if ((startIndex < 0) || (startIndex > originalVector.size())) { 631 throw ValidationException.startIndexOutOfRange(); 632 } 633 634 if ((stopIndex < 0) || (stopIndex > originalVector.size())) { 635 throw ValidationException.stopIndexOutOfRange(); 636 } 637 638 newVector = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(); 639 640 for (int index = startIndex; index < stopIndex; index++) { 641 newVector.addElement(originalVector.elementAt(index)); 642 } 643 644 return newVector; 645 } 646 647 651 public static String cr() { 652 if (CR == null) { 654 CR = System.getProperty("line.separator"); 655 } 656 return CR; 657 } 658 659 662 public static String currentWorkingDirectory() { 663 if (CURRENT_WORKING_DIRECTORY == null) { 665 CURRENT_WORKING_DIRECTORY = System.getProperty("user.dir"); 666 } 667 return CURRENT_WORKING_DIRECTORY; 668 } 669 670 673 public static String tempDirectory() { 674 if (TEMP_DIRECTORY == null) { 676 TEMP_DIRECTORY = System.getProperty("java.io.tmpdir"); 677 } 678 return TEMP_DIRECTORY; 679 } 680 681 688 public static java.sql.Date dateFromLong(Long longObject) { 689 return new java.sql.Date (longObject.longValue()); 690 } 691 692 699 public static java.sql.Date dateFromYearMonthDate(int year, int month, int day) { 700 Calendar localCalendar = allocateCalendar(); 702 localCalendar.clear(); 703 localCalendar.set(year, month, day, 0, 0, 0); 704 long millis = JavaPlatform.getTimeInMillis(localCalendar); 705 java.sql.Date date = new java.sql.Date (millis); 706 releaseCalendar(localCalendar); 707 return date; 708 } 709 710 721 public static java.sql.Date dateFromString(String dateString) throws ConversionException { 722 int year; 723 int month; 724 int day; 725 StringTokenizer dateStringTokenizer; 726 727 if (dateString.indexOf('/') != -1) { 728 dateStringTokenizer = new StringTokenizer(dateString, "/"); 729 } else if (dateString.indexOf('-') != -1) { 730 dateStringTokenizer = new StringTokenizer(dateString, "- "); 731 } else { 732 throw ConversionException.incorrectDateFormat(dateString); 733 } 734 735 try { 736 year = Integer.parseInt(dateStringTokenizer.nextToken()); 737 month = Integer.parseInt(dateStringTokenizer.nextToken()); 738 day = Integer.parseInt(dateStringTokenizer.nextToken()); 739 } catch (NumberFormatException exception) { 740 throw ConversionException.incorrectDateFormat(dateString); 741 } 742 743 month = month - 1; 745 746 return dateFromYearMonthDate(year, month, day); 747 } 748 749 756 public static java.sql.Date dateFromTimestamp(java.sql.Timestamp timestamp) { 757 return sqlDateFromUtilDate(timestamp); 758 } 759 760 763 public static boolean doesFileExist(String fileName) { 764 try { 765 new FileReader(fileName); 766 } catch (FileNotFoundException fnfException) { 767 return false; 768 } 769 770 return true; 771 772 } 773 774 777 public static String doubleSlashes(String path) { 778 StringBuffer buffer = new StringBuffer (path.length() + 5); 779 for (int index = 0; index < path.length(); index++) { 780 char charater = path.charAt(index); 781 buffer.append(charater); 782 if (charater == '\\') { 783 buffer.append('\\'); 784 } 785 } 786 787 return buffer.toString(); 788 } 789 790 793 public static String extractJarNameFromURL(java.net.URL url) { 794 String tempName = url.getFile(); 795 int start = tempName.indexOf("file:") + 5; 796 int end = tempName.indexOf("!/"); 797 return tempName.substring(start, end); 798 } 799 800 804 public static String fileSeparator() { 805 if (FILE_SEPARATOR == null) { 807 FILE_SEPARATOR = System.getProperty("file.separator"); 808 } 809 return FILE_SEPARATOR; 810 } 811 812 820 public static Field getField(Class javaClass, String fieldName) throws NoSuchFieldException { 821 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 822 try { 823 return (Field)AccessController.doPrivileged(new PrivilegedGetField(javaClass, fieldName, true)); 824 } catch (PrivilegedActionException exception) { 825 throw (NoSuchFieldException )exception.getException(); 826 } 827 } else { 828 return PrivilegedAccessHelper.getField(javaClass, fieldName, true); 829 } 830 } 831 832 841 public static Method getDeclaredMethod(Class javaClass, String methodName, Class [] methodParameterTypes) throws NoSuchMethodException { 842 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 843 try { 844 return (Method)AccessController.doPrivileged(new PrivilegedGetMethod(javaClass, methodName, methodParameterTypes, true)); 845 } catch (PrivilegedActionException exception) { 846 return null; 847 } 848 } else { 849 return PrivilegedAccessHelper.getMethod(javaClass, methodName, methodParameterTypes, true); 850 } 851 } 852 853 856 public static Object getInstanceFromClass(Class classFullName) { 857 if (classFullName == null) { 858 return null; 859 } 860 861 try { 862 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 863 try { 864 return AccessController.doPrivileged(new PrivilegedNewInstanceFromClass(classFullName)); 865 } catch (PrivilegedActionException exception) { 866 Exception throwableException = exception.getException(); 867 if (throwableException instanceof InstantiationException ) { 868 ValidationException exc = new ValidationException(); 869 exc.setInternalException(throwableException); 870 throw exc; 871 } else { 872 ValidationException exc = new ValidationException(); 873 exc.setInternalException(throwableException); 874 throw exc; 875 } 876 } 877 } else { 878 return PrivilegedAccessHelper.newInstanceFromClass(classFullName); 879 } 880 } catch (InstantiationException notInstantiatedException) { 881 ValidationException exception = new ValidationException(); 882 exception.setInternalException(notInstantiatedException); 883 throw exception; 884 } catch (IllegalAccessException notAccessedException) { 885 ValidationException exception = new ValidationException(); 886 exception.setInternalException(notAccessedException); 887 throw exception; 888 } 889 } 890 891 894 public static Object getNullWrapper() { 895 return nullWrapper; 896 } 897 898 901 public static Class getObjectClass(Class javaClass) { 902 return ConversionManager.getObjectClass(javaClass); 903 } 904 905 908 public static String getShortClassName(Class javaClass) { 909 return getShortClassName(javaClass.getName()); 910 } 911 912 915 public static String getShortClassName(String javaClassName) { 916 return javaClassName.substring(javaClassName.lastIndexOf('.') + 1); 917 } 918 919 922 public static String getShortClassName(Object object) { 923 return getShortClassName(object.getClass()); 924 } 925 926 929 public static String getPackageName(Class javaClass) { 930 String className = Helper.getShortClassName(javaClass); 931 return javaClass.getName().substring(0, (javaClass.getName().length() - (className.length() + 1))); 932 } 933 934 937 public static String getTabs(int noOfTabs) { 938 StringWriter writer = new StringWriter(); 939 for (int index = 0; index < noOfTabs; index++) { 940 writer.write("\t"); 941 } 942 return writer.toString(); 943 } 944 945 953 public static int indexOfNullElement(Vector v, int index) { 954 for (int i = index; i < v.size(); i++) { 955 if (v.elementAt(i) == null) { 956 return i; 957 } 958 } 959 return -1; 960 } 961 962 968 public static boolean isCollection(Object testObject) { 969 if (testObject instanceof Collection) { 971 return true; 972 } 973 974 return false; 976 } 977 978 982 public static boolean isPrimitiveWrapper(Class classInQuestion) { 983 return classInQuestion.equals(Character .class) || classInQuestion.equals(Boolean .class) || classInQuestion.equals(Byte .class) || classInQuestion.equals(Short .class) || classInQuestion.equals(Integer .class) || classInQuestion.equals(Long .class) || classInQuestion.equals(Float .class) || classInQuestion.equals(Double .class); 984 } 985 986 989 public static boolean isUpperCaseString(String s) { 990 char[] c = s.toCharArray(); 991 for (int i = 0; i < s.length(); i++) { 992 if (Character.isLowerCase(c[i])) { 993 return false; 994 } 995 } 996 return true; 997 } 998 999 1002 public static boolean isVowel(char c) { 1003 return (c == 'A') || (c == 'a') || (c == 'e') || (c == 'E') || (c == 'i') || (c == 'I') || (c == 'o') || (c == 'O') || (c == 'u') || (c == 'U'); 1004 } 1005 1006 1010 public static File[] listFilesIn(File directory) { 1011 if (directory.isDirectory()) { 1012 return directory.listFiles(); 1013 } else { 1014 return new File[0]; 1015 } 1016 } 1017 1018 1023 public static Vector makeVectorFromObject(Object theObject) { 1024 if (theObject instanceof Vector) { 1025 return ((Vector)theObject); 1026 } 1027 if (theObject instanceof Collection) { 1028 Vector returnVector = new Vector(((Collection)theObject).size()); 1029 Iterator iterator = ((Collection)theObject).iterator(); 1030 while (iterator.hasNext()) { 1031 returnVector.add(iterator.next()); 1032 } 1033 return returnVector; 1034 } 1035 1036 Vector returnVector = new Vector(); 1037 returnVector.addElement(theObject); 1038 return returnVector; 1039 } 1040 1041 1045 public static String pathSeparator() { 1046 if (PATH_SEPARATOR == null) { 1048 PATH_SEPARATOR = System.getProperty("path.separator"); 1049 } 1050 return PATH_SEPARATOR; 1051 } 1052 1053 1056 public static String printStackTraceToString(Throwable aThrowable) { 1057 StringWriter swriter = new StringWriter(); 1058 PrintWriter writer = new PrintWriter(swriter, true); 1059 aThrowable.printStackTrace(writer); 1060 writer.close(); 1061 return swriter.toString(); 1062 } 1063 1064 1067 public static String printTimeFromMilliseconds(long milliseconds) { 1068 if ((milliseconds > 1000) && (milliseconds < 60000)) { 1069 return (milliseconds / 1000) + "s"; 1070 } 1071 if (milliseconds > 60000) { 1072 return (milliseconds / 60000) + "min " + printTimeFromMilliseconds(milliseconds % 60000); 1073 } 1074 return milliseconds + "ms"; 1075 } 1076 1077 1080 public static String printVector(Vector vector) { 1081 StringWriter stringWriter = new StringWriter(); 1082 stringWriter.write("["); 1083 Enumeration enumtr = vector.elements(); 1084 stringWriter.write(String.valueOf(enumtr.nextElement())); 1085 while (enumtr.hasMoreElements()) { 1086 stringWriter.write(" "); 1087 stringWriter.write(String.valueOf(enumtr.nextElement())); 1088 } 1089 stringWriter.write("]"); 1090 return stringWriter.toString(); 1091 1092 } 1093 1094 public static Hashtable rehashHashtable(Hashtable table) { 1095 Hashtable rehashedTable = new Hashtable(table.size() + 2); 1096 1097 Enumeration values = table.elements(); 1098 for (Enumeration keys = table.keys(); keys.hasMoreElements();) { 1099 Object key = keys.nextElement(); 1100 Object value = values.nextElement(); 1101 rehashedTable.put(key, value); 1102 } 1103 1104 return rehashedTable; 1105 } 1106 1107 public static Map rehashMap(Map table) { 1108 HashMap rehashedTable = new HashMap(table.size() + 2); 1109 1110 Iterator values = table.values().iterator(); 1111 for (Iterator keys = table.keySet().iterator(); keys.hasNext();) { 1112 Object key = keys.next(); 1113 Object value = values.next(); 1114 rehashedTable.put(key, value); 1115 } 1116 1117 return rehashedTable; 1118 } 1119 1120 1124 public static String removeAllButAlphaNumericToFit(String s1, int maximumStringLength) { 1125 int s1Size = s1.length(); 1126 if (s1Size <= maximumStringLength) { 1127 return s1; 1128 } 1129 1130 StringBuffer buf = new StringBuffer (); 1132 int numberOfCharsToBeRemoved = s1.length() - maximumStringLength; 1133 int s1Index = 0; 1134 while ((numberOfCharsToBeRemoved > 0) && (s1Index < s1Size)) { 1135 char currentChar = s1.charAt(s1Index); 1136 if (Character.isLetterOrDigit(currentChar)) { 1137 buf.append(currentChar); 1138 } else { 1139 numberOfCharsToBeRemoved--; 1140 } 1141 s1Index++; 1142 } 1143 1144 while (s1Index < s1Size) { 1147 buf.append(s1.charAt(s1Index)); 1148 s1Index++; 1149 } 1150 1151 return buf.toString(); 1153 } 1154 1155 1159 public static String removeCharacterToFit(String s1, char aChar, int maximumStringLength) { 1160 int s1Size = s1.length(); 1161 if (s1Size <= maximumStringLength) { 1162 return s1; 1163 } 1164 1165 StringBuffer buf = new StringBuffer (); 1167 int numberOfCharsToBeRemoved = s1.length() - maximumStringLength; 1168 int s1Index = 0; 1169 while ((numberOfCharsToBeRemoved > 0) && (s1Index < s1Size)) { 1170 char currentChar = s1.charAt(s1Index); 1171 if (currentChar == aChar) { 1172 numberOfCharsToBeRemoved--; 1173 } else { 1174 buf.append(currentChar); 1175 } 1176 s1Index++; 1177 } 1178 1179 while (s1Index < s1Size) { 1182 buf.append(s1.charAt(s1Index)); 1183 s1Index++; 1184 } 1185 1186 return buf.toString(); 1188 } 1189 1190 1197 public static boolean removeNullElement(Vector v) { 1198 int indexOfNull = indexOfNullElement(v, 0); 1199 if (indexOfNull != -1) { 1200 v.removeElementAt(indexOfNull); 1201 return true; 1202 } 1203 return false; 1204 } 1205 1206 1210 public static String removeVowels(String s1) { 1211 StringBuffer buf = new StringBuffer (); 1213 int s1Size = s1.length(); 1214 int s1Index = 0; 1215 while (s1Index < s1Size) { 1216 char currentChar = s1.charAt(s1Index); 1217 if (!isVowel(currentChar)) { 1218 buf.append(currentChar); 1219 } 1220 s1Index++; 1221 } 1222 1223 return buf.toString(); 1225 } 1226 1227 1230 public static String replaceFirstSubString(String source, String subString, String replacement) { 1231 int index = source.indexOf(subString); 1232 1233 if (index >= 0) { 1234 return source.substring(0, index) + replacement + source.substring(index + subString.length()); 1235 } 1236 return null; 1237 } 1238 1239 public static Vector reverseVector(Vector theVector) { 1240 Vector tempVector = new Vector(theVector.size()); 1241 Object currentElement; 1242 1243 for (int i = theVector.size() - 1; i > -1; i--) { 1244 currentElement = theVector.elementAt(i); 1245 tempVector.addElement(currentElement); 1246 } 1247 1248 return tempVector; 1249 } 1250 1251 1257 public static String rightTrimString(String originalString) { 1258 int len = originalString.length(); 1259 while ((len > 0) && (originalString.charAt(len - 1) <= ' ')) { 1260 len--; 1261 } 1262 return originalString.substring(0, len); 1263 } 1264 1265 1270 public static String shortenStringsByRemovingVowelsToFit(String s1, String s2, int maximumStringLength) { 1271 int size = s1.length() + s2.length(); 1272 if (size <= maximumStringLength) { 1273 return s1 + s2; 1274 } 1275 1276 int s1Size = s1.length(); 1278 int s2Size = s2.length(); 1279 StringBuffer buf1 = new StringBuffer (); 1280 StringBuffer buf2 = new StringBuffer (); 1281 int numberOfCharsToBeRemoved = size - maximumStringLength; 1282 int s1Index = 0; 1283 int s2Index = 0; 1284 int modulo2 = 0; 1285 1286 while ((numberOfCharsToBeRemoved > 0) && !((s1Index >= s1Size) && (s2Index >= s2Size))) { 1288 if ((modulo2 % 2) == 0) { 1289 if (s1Index < s1Size) { 1291 if (isVowel(s1.charAt(s1Index))) { 1292 numberOfCharsToBeRemoved--; 1293 } else { 1294 buf1.append(s1.charAt(s1Index)); 1295 } 1296 s1Index++; 1297 } 1298 } else { 1299 if (s2Index < s2Size) { 1301 if (isVowel(s2.charAt(s2Index))) { 1302 numberOfCharsToBeRemoved--; 1303 } else { 1304 buf2.append(s2.charAt(s2Index)); 1305 } 1306 s2Index++; 1307 } 1308 } 1309 modulo2++; 1310 } 1311 1312 while (s1Index < s1Size) { 1315 buf1.append(s1.charAt(s1Index)); 1316 s1Index++; 1317 } 1318 while (s2Index < s2Size) { 1319 buf2.append(s2.charAt(s2Index)); 1320 s2Index++; 1321 } 1322 1323 return buf1.toString() + buf2.toString(); 1325 } 1326 1327 1330 public static java.sql.Date sqlDateFromUtilDate(java.util.Date utilDate) { 1331 Calendar calendar = allocateCalendar(); 1333 calendar.setTime(utilDate); 1334 java.sql.Date date = dateFromCalendar(calendar); 1335 releaseCalendar(calendar); 1336 return date; 1337 } 1338 1339 1342 public static String printDate(java.sql.Date date) { 1343 Calendar calendar = allocateCalendar(); 1345 calendar.setTime(date); 1346 String string = printDate(calendar); 1347 releaseCalendar(calendar); 1348 return string; 1349 } 1350 1351 1354 public static String printDate(Calendar calendar) { 1355 return printDate(calendar, true); 1356 } 1357 1358 1363 public static String printDate(Calendar calendar, boolean useLocalTime) { 1364 int year; 1365 int month; 1366 int day; 1367 if (useLocalTime && (!defaultTimeZone.equals(calendar.getTimeZone()))) { 1368 Calendar localCalendar = allocateCalendar(); 1370 JavaPlatform.setTimeInMillis(localCalendar, JavaPlatform.getTimeInMillis(calendar)); 1371 year = localCalendar.get(Calendar.YEAR); 1372 month = localCalendar.get(Calendar.MONTH) + 1; 1373 day = localCalendar.get(Calendar.DATE); 1374 releaseCalendar(localCalendar); 1375 } else { 1376 year = calendar.get(Calendar.YEAR); 1377 month = calendar.get(Calendar.MONTH) + 1; 1378 day = calendar.get(Calendar.DATE); 1379 } 1380 1381 char[] buf = "2000-00-00".toCharArray(); 1382 buf[0] = Character.forDigit(year / 1000, 10); 1383 buf[1] = Character.forDigit((year / 100) % 10, 10); 1384 buf[2] = Character.forDigit((year / 10) % 10, 10); 1385 buf[3] = Character.forDigit(year % 10, 10); 1386 buf[5] = Character.forDigit(month / 10, 10); 1387 buf[6] = Character.forDigit(month % 10, 10); 1388 buf[8] = Character.forDigit(day / 10, 10); 1389 buf[9] = Character.forDigit(day % 10, 10); 1390 1391 return new String (buf); 1392 } 1393 1394 1397 public static String printTime(java.sql.Time time) { 1398 Calendar calendar = allocateCalendar(); 1400 calendar.setTime(time); 1401 String string = printTime(calendar); 1402 releaseCalendar(calendar); 1403 return string; 1404 } 1405 1406 1409 public static String printTime(Calendar calendar) { 1410 return printTime(calendar, true); 1411 } 1412 1413 1418 public static String printTime(Calendar calendar, boolean useLocalTime) { 1419 int hour; 1420 int minute; 1421 int second; 1422 if (useLocalTime && (!defaultTimeZone.equals(calendar.getTimeZone()))) { 1423 Calendar localCalendar = allocateCalendar(); 1425 JavaPlatform.setTimeInMillis(localCalendar, JavaPlatform.getTimeInMillis(calendar)); 1426 hour = localCalendar.get(Calendar.HOUR_OF_DAY); 1427 minute = localCalendar.get(Calendar.MINUTE); 1428 second = localCalendar.get(Calendar.SECOND); 1429 releaseCalendar(localCalendar); 1430 } else { 1431 hour = calendar.get(Calendar.HOUR_OF_DAY); 1432 minute = calendar.get(Calendar.MINUTE); 1433 second = calendar.get(Calendar.SECOND); 1434 } 1435 String hourString; 1436 String minuteString; 1437 String secondString; 1438 if (hour < 10) { 1439 hourString = "0" + hour; 1440 } else { 1441 hourString = Integer.toString(hour); 1442 } 1443 if (minute < 10) { 1444 minuteString = "0" + minute; 1445 } else { 1446 minuteString = Integer.toString(minute); 1447 } 1448 if (second < 10) { 1449 secondString = "0" + second; 1450 } else { 1451 secondString = Integer.toString(second); 1452 } 1453 return (hourString + ":" + minuteString + ":" + secondString); 1454 } 1455 1456 1459 public static String printCalendar(Calendar calendar) { 1460 return printCalendar(calendar, true); 1461 } 1462 1463 1468 public static String printCalendar(Calendar calendar, boolean useLocalTime) { 1469 String millisString; 1470 1471 if (calendar.get(Calendar.MILLISECOND) == 0) { 1473 millisString = "0"; 1474 } else { 1475 millisString = buildZeroPrefixAndTruncTrailZeros(calendar.get(Calendar.MILLISECOND), 3); 1476 } 1477 1478 StringBuffer timestampBuf = new StringBuffer (); 1479 timestampBuf.append(printDate(calendar, useLocalTime)); 1480 timestampBuf.append(" "); 1481 timestampBuf.append(printTime(calendar, useLocalTime)); 1482 timestampBuf.append("."); 1483 timestampBuf.append(millisString); 1484 1485 return timestampBuf.toString(); 1486 } 1487 1488 1491 public static String printTimestamp(java.sql.Timestamp timestamp) { 1492 Calendar calendar = allocateCalendar(); 1494 calendar.setTime(timestamp); 1495 1496 String nanosString; 1497 1498 String yearZeros = "0000"; 1500 1501 if (timestamp.getNanos() == 0) { 1502 nanosString = "0"; 1503 } else { 1504 nanosString = buildZeroPrefixAndTruncTrailZeros(timestamp.getNanos(), 9); 1505 } 1506 1507 StringBuffer timestampBuf = new StringBuffer (); 1508 timestampBuf.append(printDate(calendar)); 1509 timestampBuf.append(" "); 1510 timestampBuf.append(printTime(calendar)); 1511 timestampBuf.append("."); 1512 timestampBuf.append(nanosString); 1513 1514 releaseCalendar(calendar); 1515 1516 return (timestampBuf.toString()); 1517 } 1518 1519 1524 public static String buildZeroPrefix(int number, int totalDigits) { 1525 String zeros = "000000000"; 1526 int absValue = (number < 0) ? (-number) : number; 1527 String numbString = Integer.toString(absValue); 1528 1529 numbString = zeros.substring(0, (totalDigits - numbString.length())) + numbString; 1531 1532 if (number < 0) { 1533 numbString = "-" + numbString; 1534 } else { 1535 numbString = "+" + numbString; 1536 } 1537 return numbString; 1538 } 1539 1540 1545 public static String buildZeroPrefixAndTruncTrailZeros(int number, int totalDigits) { 1546 String zeros = "000000000"; 1547 String numbString = Integer.toString(number); 1548 1549 numbString = zeros.substring(0, (totalDigits - numbString.length())) + numbString; 1551 char[] numbChar = new char[numbString.length()]; 1553 numbString.getChars(0, numbString.length(), numbChar, 0); 1554 int truncIndex = totalDigits - 1; 1555 while (numbChar[truncIndex] == '0') { 1556 truncIndex--; 1557 } 1558 return new String (numbChar, 0, truncIndex + 1); 1559 } 1560 1561 1564 public static String printTimestampWithoutNanos(java.sql.Timestamp timestamp) { 1565 Calendar calendar = allocateCalendar(); 1567 calendar.setTime(timestamp); 1568 String string = printCalendarWithoutNanos(calendar); 1569 releaseCalendar(calendar); 1570 return string; 1571 } 1572 1573 1576 public static String printCalendarWithoutNanos(Calendar calendar) { 1577 StringBuffer timestampBuf = new StringBuffer (); 1578 timestampBuf.append(printDate(calendar)); 1579 timestampBuf.append(" "); 1580 timestampBuf.append(printTime(calendar)); 1581 return timestampBuf.toString(); 1582 } 1583 1584 1587 public static java.sql.Date dateFromCalendar(Calendar calendar) { 1588 if (!defaultTimeZone.equals(calendar.getTimeZone())) { 1589 Calendar localCalendar = allocateCalendar(); 1591 JavaPlatform.setTimeInMillis(localCalendar, JavaPlatform.getTimeInMillis(calendar)); 1592 java.sql.Date date = dateFromYearMonthDate(localCalendar.get(Calendar.YEAR), localCalendar.get(Calendar.MONTH), localCalendar.get(Calendar.DATE)); 1593 releaseCalendar(localCalendar); 1594 return date; 1595 } 1596 return dateFromYearMonthDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE)); 1597 } 1598 1599 1602 public static void systemBug(String description) { 1603 } 1605 1606 1613 public static java.sql.Time timeFromDate(java.util.Date date) { 1614 Calendar calendar = allocateCalendar(); 1616 calendar.setTime(date); 1617 java.sql.Time time = timeFromCalendar(calendar); 1618 releaseCalendar(calendar); 1619 return time; 1620 } 1621 1622 1628 public static java.sql.Time timeFromLong(Long longObject) { 1629 return new java.sql.Time (longObject.longValue()); 1630 } 1631 1632 1638 public static java.sql.Time timeFromHourMinuteSecond(int hour, int minute, int second) { 1639 Calendar localCalendar = allocateCalendar(); 1641 localCalendar.clear(); 1642 localCalendar.set(1970, 0, 1, hour, minute, second); 1643 long millis = JavaPlatform.getTimeInMillis(localCalendar); 1644 java.sql.Time time = new java.sql.Time (millis); 1645 releaseCalendar(localCalendar); 1646 return time; 1647 } 1648 1649 1657 public static java.sql.Time timeFromString(String timeString) throws ConversionException { 1658 int hour; 1659 int minute; 1660 int second; 1661 String timePortion = timeString; 1662 1663 if (timeString.length() > 12) { 1664 timePortion = timeString.substring(11, 19); 1666 } 1667 1668 if ((timePortion.indexOf('-') == -1) && (timePortion.indexOf('/') == -1) && (timePortion.indexOf('.') == -1) && (timePortion.indexOf(':') == -1)) { 1669 throw ConversionException.incorrectTimeFormat(timePortion); 1670 } 1671 StringTokenizer timeStringTokenizer = new StringTokenizer(timePortion, " /:.-"); 1672 1673 try { 1674 hour = Integer.parseInt(timeStringTokenizer.nextToken()); 1675 minute = Integer.parseInt(timeStringTokenizer.nextToken()); 1676 second = Integer.parseInt(timeStringTokenizer.nextToken()); 1677 } catch (NumberFormatException exception) { 1678 throw ConversionException.incorrectTimeFormat(timeString); 1679 } 1680 1681 return timeFromHourMinuteSecond(hour, minute, second); 1682 } 1683 1684 1688 public static java.sql.Time timeFromTimestamp(java.sql.Timestamp timestamp) { 1689 return timeFromDate(timestamp); 1690 } 1691 1692 1695 public static java.sql.Time timeFromCalendar(Calendar calendar) { 1696 if (!defaultTimeZone.equals(calendar.getTimeZone())) { 1697 Calendar localCalendar = allocateCalendar(); 1699 JavaPlatform.setTimeInMillis(localCalendar, JavaPlatform.getTimeInMillis(calendar)); 1700 java.sql.Time date = timeFromHourMinuteSecond(localCalendar.get(Calendar.HOUR_OF_DAY), localCalendar.get(Calendar.MINUTE), localCalendar.get(Calendar.SECOND)); 1701 releaseCalendar(localCalendar); 1702 return date; 1703 } 1704 return timeFromHourMinuteSecond(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND)); 1705 } 1706 1707 1710 public static java.sql.Timestamp timestampFromCalendar(Calendar calendar) { 1711 return timestampFromLong(JavaPlatform.getTimeInMillis(calendar)); 1712 } 1713 1714 1717 public static java.sql.Timestamp timestampFromDate(java.util.Date date) { 1718 return timestampFromLong(date.getTime()); 1719 } 1720 1721 1727 public static java.sql.Timestamp timestampFromLong(Long millis) { 1728 return timestampFromLong(millis.longValue()); 1729 } 1730 1731 1737 public static java.sql.Timestamp timestampFromLong(long millis) { 1738 java.sql.Timestamp timestamp = new java.sql.Timestamp (millis); 1739 1740 if ((millis % 1000) > 0) { 1743 timestamp.setNanos((int)(millis % 1000) * 1000000); 1744 } else if ((millis % 1000) < 0) { 1745 timestamp.setNanos((int)(1000000000 - (Math.abs((millis % 1000) * 1000000)))); 1746 } 1747 return timestamp; 1748 } 1749 1750 1758 public static java.sql.Timestamp timestampFromString(String timestampString) throws ConversionException { 1759 if ((timestampString.indexOf('-') == -1) && (timestampString.indexOf('/') == -1) && (timestampString.indexOf('.') == -1) && (timestampString.indexOf(':') == -1)) { 1760 throw ConversionException.incorrectTimestampFormat(timestampString); 1761 } 1762 StringTokenizer timestampStringTokenizer = new StringTokenizer(timestampString, " /:.-"); 1763 1764 int year; 1765 int month; 1766 int day; 1767 int hour; 1768 int minute; 1769 int second; 1770 int nanos; 1771 try { 1772 year = Integer.parseInt(timestampStringTokenizer.nextToken()); 1773 month = Integer.parseInt(timestampStringTokenizer.nextToken()); 1774 day = Integer.parseInt(timestampStringTokenizer.nextToken()); 1775 try { 1776 hour = Integer.parseInt(timestampStringTokenizer.nextToken()); 1777 minute = Integer.parseInt(timestampStringTokenizer.nextToken()); 1778 second = Integer.parseInt(timestampStringTokenizer.nextToken()); 1779 } catch (java.util.NoSuchElementException endOfStringException) { 1780 hour = 0; 1782 minute = 0; 1783 second = 0; 1784 } 1785 } catch (NumberFormatException exception) { 1786 throw ConversionException.incorrectTimestampFormat(timestampString); 1787 } 1788 1789 try { 1790 String nanoToken = timestampStringTokenizer.nextToken(); 1791 nanos = Integer.parseInt(nanoToken); 1792 for (int times = 0; times < (9 - nanoToken.length()); times++) { 1793 nanos = nanos * 10; 1794 } 1795 } catch (java.util.NoSuchElementException endOfStringException) { 1796 nanos = 0; 1797 } catch (NumberFormatException exception) { 1798 throw ConversionException.incorrectTimestampFormat(timestampString); 1799 } 1800 1801 year = year - 1900; 1803 1804 month = month - 1; 1806 1807 java.sql.Timestamp timestamp; 1808 timestamp = new java.sql.Timestamp (year, month, day, hour, minute, second, nanos); 1811 return timestamp; 1812 } 1813 1814 1819 public static java.sql.Timestamp timestampFromYearMonthDateHourMinuteSecondNanos(int year, int month, int date, int hour, int minute, int second, int nanos) { 1820 return new java.sql.Timestamp (year - 1900, month, date, hour, minute, second, nanos); 1823 } 1824 1825 1828 public static void toDo(String description) { 1829 } 1831 1832 1842 public static String truncate(String originalString, int size) { 1843 if (originalString.length() <= size) { 1844 return originalString; 1846 } 1847 String vowels = "AaEeIiOoUu"; 1848 StringBuffer newStringBufferTmp = new StringBuffer (originalString.length()); 1849 1850 int counter = originalString.length() - size; 1852 for (int index = (originalString.length() - 1); index >= 0; index--) { 1853 if (vowels.indexOf(originalString.charAt(index)) == -1) { 1856 newStringBufferTmp.append(originalString.charAt(index)); 1857 } else { 1858 counter--; 1860 if (counter == 0) { 1861 StringBuffer newStringBuffer = new StringBuffer (size); 1865 newStringBuffer.append(originalString.substring(0, index)); 1866 newStringBuffer.append(newStringBufferTmp.reverse().toString()); 1870 return newStringBuffer.toString(); 1871 } 1872 } 1873 } 1874 1875 return newStringBufferTmp.reverse().toString().substring(0, size); 1877 } 1878 1879 1886 public static java.util.Date utilDateFromLong(Long longObject) { 1887 return new java.util.Date (longObject.longValue()); 1888 } 1889 1890 1896 public static java.util.Date utilDateFromSQLDate(java.sql.Date sqlDate) { 1897 return new java.util.Date (sqlDate.getTime()); 1898 } 1899 1900 1906 public static java.util.Date utilDateFromTime(java.sql.Time time) { 1907 return new java.util.Date (time.getTime()); 1908 } 1909 1910 1916 public static java.util.Date utilDateFromTimestamp(java.sql.Timestamp timestampObject) { 1917 long time = timestampObject.getTime(); 1921 boolean appendNanos = ((time % 1000) == 0); 1922 if (appendNanos) { 1923 return new java.util.Date (time + (timestampObject.getNanos() / 1000000)); 1924 } else { 1925 return new java.util.Date (time); 1926 } 1927 } 1928 1929 1932 public static Vector vectorFromArray(Object [] array) { 1933 Vector result = new Vector(array.length); 1934 for (int i = 0; i < array.length; i++) { 1935 result.addElement(array[i]); 1936 } 1937 return result; 1938 } 1939 1940 1944 public static void writeHexString(byte[] bytes, Writer writer) throws IOException { 1945 writer.write(buildHexStringFromBytes(bytes)); 1946 } 1947} 1948 | Popular Tags |