1 34 35 package com.micronova.util.codec; 36 37 import java.text.*; 38 import java.util.*; 39 import java.net.*; 40 import java.io.*; 41 import javax.mail.*; 42 import javax.mail.internet.*; 43 44 import com.micronova.util.*; 45 46 47 48 public class CodecType extends Codec 49 { 50 51 52 public static final Object isDouble(Object object) 53 { 54 return TypeUtil.isDouble(object); 55 } 56 57 58 59 public static final Object isFloat(Object object) 60 { 61 return TypeUtil.isFloat(object); 62 } 63 64 65 66 public static final Object isLong(Object object) 67 { 68 return TypeUtil.isLong(object); 69 } 70 71 72 73 public static final Object isInteger(Object object) 74 { 75 return TypeUtil.isInteger(object); 76 } 77 78 79 80 public static Object isShort(Object object) 81 { 82 return TypeUtil.isShort(object); 83 } 84 85 86 87 public static final Object isString(Object object) 88 { 89 return TypeUtil.isString(object); 90 } 91 92 93 94 public static final Object isByte(Object object) 95 { 96 return TypeUtil.isByte(object); 97 } 98 99 100 101 public static final Object isNestedMap(Object object) 102 { 103 return TypeUtil.isNestedMap(object); 104 } 105 106 107 108 public static final Object isBoolean(Object object) 109 { 110 return TypeUtil.isBoolean(object); 111 } 112 113 114 115 public static final Object isCharacter(Object object) 116 { 117 return TypeUtil.isCharacter(object); 118 } 119 120 121 122 public static final Object isCalendar(Object object, Object patternSpec, Object localeSpec, Object timeZoneSpec) 123 { 124 if (object != null) 125 { 126 try 127 { 128 Locale locale = TypeUtil.isLocale(localeSpec); 129 String pattern = TypeUtil.isString(patternSpec); 130 TimeZone timeZone = TypeUtil.isTimeZone(timeZoneSpec); 131 132 Date date = TypeUtil.isDate(object, pattern, locale, timeZone); 133 134 Calendar calendar = Calendar.getInstance(locale); 135 136 calendar.setTime(date); 137 138 object = calendar; 139 } 140 catch (Exception e) 141 { 142 object = null; 143 } 144 } 145 146 return object; 147 } 148 149 150 151 public static final Object isCalendar(Object object, Object patternSpec, Object localeSpec) 152 { 153 return isCalendar(object, patternSpec, localeSpec, null); 154 } 155 156 157 158 public static final Object isCalendar(Object object, Object patternSpec) 159 { 160 return isCalendar(object, patternSpec, null); 161 } 162 163 164 165 public static final Object isCalendar(Object object) 166 { 167 return isCalendar(object, null, null); 168 } 169 170 171 172 public static final Object isDate(Object object, Object patternSpec, Object localeSpec, Object timeZoneSpec) 173 { 174 if (object != null) 175 { 176 try 177 { 178 Locale locale = TypeUtil.isLocale(localeSpec); 179 String pattern = TypeUtil.isString(patternSpec); 180 TimeZone timeZone = TypeUtil.isTimeZone(timeZoneSpec); 181 182 object = TypeUtil.isDate(object, pattern, locale, timeZone); 183 } 184 catch (Exception e) 185 { 186 object = null; 187 } 188 } 189 190 return object; 191 } 192 193 194 195 public static final Object isDate(Object object, Object patternSpec, Object localeSpec) 196 { 197 return isDate(object, patternSpec, localeSpec, null); 198 } 199 200 201 202 public static final Object isDate(Object object, Object patternSpec) 203 { 204 return isDate(object, patternSpec, null); 205 } 206 207 208 209 public static final Object isDate(Object object) 210 { 211 return isDate(object, null, null); 212 } 213 214 215 216 public static final Object isNumber(Object object, Object patternSpec, Object localeSpec) 217 { 218 if (object != null) 219 { 220 try 221 { 222 Locale locale = TypeUtil.isLocale(localeSpec); 223 String pattern = TypeUtil.isString(patternSpec); 224 225 object = TypeUtil.isNumber(object, pattern, locale); 226 } 227 catch (Exception e) 228 { 229 object = null; 230 } 231 } 232 233 return object; 234 } 235 236 237 238 public static final Object isNumber(Object object, Object patternSpec) 239 { 240 return isNumber(object, patternSpec, null); 241 } 242 243 244 245 public static final Object isNumber(Object object) 246 { 247 return isNumber(object, null, null); 248 } 249 250 251 252 public static final Object isSQLDate(Object object) 253 { 254 if (object instanceof Date) 255 { 256 object = new java.sql.Date (((Date)object).getTime()); 257 } 258 else 259 { 260 Long longObject = (Long )isLong(object); 261 262 if (longObject != null) 263 { 264 object = new java.sql.Date (longObject.longValue()); 265 } 266 else 267 { 268 object = null; 269 } 270 } 271 272 return object; 273 } 274 275 public static final Object isSQLDate(Object object, Object patternSpec, Object localeSpec) 276 { 277 return isSQLDate(isDate(object, patternSpec, localeSpec)); 278 } 279 280 public static final Object isSQLDate(Object object, Object patternSpec) 281 { 282 return isSQLDate(isDate(object, patternSpec)); 283 } 284 285 286 287 public static final Object isSQLTime(Object object) 288 { 289 if (object instanceof Date) 290 { 291 object = new java.sql.Time (((Date)object).getTime()); 292 } 293 else 294 { 295 Long longObject = (Long )isLong(object); 296 297 if (longObject != null) 298 { 299 object = new java.sql.Time (longObject.longValue()); 300 } 301 else 302 { 303 object = null; 304 } 305 } 306 307 return object; 308 } 309 310 public static final Object isSQLTime(Object object, Object patternSpec, Object localeSpec) 311 { 312 return isSQLTime(isDate(object, patternSpec, localeSpec)); 313 } 314 315 public static final Object isSQLTime(Object object, Object patternSpec) 316 { 317 return isSQLTime(isDate(object, patternSpec)); 318 } 319 320 321 322 public static final Object isSQLTimestamp(Object object) 323 { 324 if (object instanceof Date) 325 { 326 object = new java.sql.Timestamp (((Date)object).getTime()); 327 } 328 else 329 { 330 Long longObject = (Long )isLong(object); 331 332 if (longObject != null) 333 { 334 object = new java.sql.Timestamp (longObject.longValue()); 335 } 336 else 337 { 338 object = null; 339 } 340 } 341 342 return object; 343 } 344 345 public static final Object isSQLTimestamp(Object object, Object patternSpec, Object localeSpec) 346 { 347 return isSQLTimestamp(isDate(object, patternSpec, localeSpec)); 348 } 349 350 public static final Object isSQLTimestamp(Object object, Object patternSpec) 351 { 352 return isSQLTimestamp(isDate(object, patternSpec)); 353 } 354 355 356 357 public static final Object numericValueOf(Object object) 358 { 359 if (object != null) 360 { 361 Character c = TypeUtil.isCharacter(object); 362 363 object = new Long (Character.getNumericValue(c.charValue())); 364 } 365 366 return object; 367 } 368 369 370 371 public static final Object isURI(Object object) 372 { 373 return TypeUtil.isURI(object); 374 } 375 376 377 378 public static final Object isURL(Object object, Object context) 379 { 380 return TypeUtil.isURL(object, TypeUtil.isURL(context)); 381 } 382 383 384 public static final Object isURL(Object object) 385 { 386 return TypeUtil.isURL(object); 387 } 388 389 390 391 public static final Object isFile(Object object) 392 { 393 return TypeUtil.isFile(object, null); 394 } 395 396 397 398 public static final Object isFile(Object object, Object parent) 399 { 400 return TypeUtil.isFile(object, parent); 401 } 402 403 404 405 public static final Object isLocale(Object object) 406 { 407 if (object != null) 408 { 409 object = TypeUtil.isLocale(object); 410 } 411 412 return object; 413 } 414 415 416 417 public static final Object isTimeZone(Object object) 418 { 419 if (object != null) 420 { 421 object = TypeUtil.isTimeZone(object); 422 } 423 424 return object; 425 } 426 427 428 429 public static final Object unicodeBlockOf(Object object) 430 { 431 if (object != null) 432 { 433 Character c = TypeUtil.isCharacter(object); 434 435 object = Character.UnicodeBlock.of(c.charValue()); 436 } 437 438 return object; 439 } 440 441 442 443 public static final Object charAsNumber(Object object) 444 { 445 if (object != null) 446 { 447 Character c = TypeUtil.isCharacter(object); 448 449 object = new Long ((long)c.charValue()); 450 } 451 452 return object; 453 } 454 455 456 457 public static final Object numberAsChar(Object object) 458 { 459 if (object != null) 460 { 461 Long l = TypeUtil.isLong(object); 462 463 object = new Character ((char)(l.longValue())); 464 } 465 466 return object; 467 } 468 469 470 471 public static Object isArray(Object object) 472 { 473 return TypeUtil.isArray(object); 474 } 475 476 477 478 public static Object isList(Object object) 479 { 480 return TypeUtil.isList(object); 481 } 482 483 484 485 public static Object isStringArray(Object object) 486 { 487 return TypeUtil.isStringArray(object); 488 } 489 490 491 492 public static Object isStringList(Object object, Object separatorSpec, Object escapeSpec) 493 { 494 return TypeUtil.isStringList(object, TypeUtil.isCharacter(separatorSpec).charValue(), TypeUtil.isCharacter(escapeSpec).charValue()); 495 } 496 497 public static final Character DEFAULTESCAPE = new Character ('\\'); 498 public static final Character DEFAULTSEPARATOR = new Character (','); 499 500 public static Object isStringList(Object object, Object separatorSpec) 501 { 502 return isStringList(object, separatorSpec, DEFAULTESCAPE); 503 } 504 505 public static Object isStringList(Object object) 506 { 507 return isStringList(object, DEFAULTSEPARATOR); 508 } 509 510 511 512 public static Object ifNull(Object object, Object ifObject, Object elseObject) 513 { 514 return ((object == null) ? ifObject : elseObject); 515 } 516 517 518 519 public static Object isNull(Object object) 520 { 521 return ifNull(object, Boolean.TRUE, null); 522 } 523 524 525 526 public static Object ifNull(Object object, Object ifObject) 527 { 528 return ifNull(object, ifObject, object); 529 } 530 531 532 533 public static Object isNotNull(Object object) 534 { 535 return ifNull(object, null, Boolean.TRUE); 536 } 537 538 539 540 public static Object ifNotNull(Object object, Object ifObject) 541 { 542 return ifNull(object, object, ifObject); 543 } 544 545 546 547 public static Object isClass(Object object, Object classSpec) 548 { 549 if (object != null) 550 { 551 try 552 { 553 Class classObject = null; 554 555 if (classSpec instanceof Class ) 556 { 557 classObject = (Class )classSpec; 558 } 559 else 560 { 561 classObject = TypeUtil.forName(classSpec.toString(), null); 562 } 563 564 object = TypeUtil.isClass(object, classObject); 565 } 566 catch (Exception e) 567 { 568 object = null; 569 } 570 } 571 572 return object; 573 } 574 575 576 577 public static Object forName(Object object) 578 { 579 if (object != null) 580 { 581 try 582 { 583 object = TypeUtil.forName(object.toString(), null); 584 } 585 catch (Exception e) 586 { 587 object = null; 588 } 589 } 590 591 return object; 592 } 593 594 595 596 public static final Object clone(Object object) throws Exception 597 { 598 if (object != null) 599 { 600 object = TypeUtil.invoke(object, null, "clone", null, null); 601 } 602 603 return object; 604 } 605 606 607 608 public static final Object length(Object object) 609 { 610 if (object != null) 611 { 612 int length = TypeUtil.length(object); 613 614 if (length >= 0) 615 { 616 return new Integer (length); 617 } 618 } 619 620 return null; 621 } 622 623 624 625 public static final Object clear(Object object) 626 { 627 if (object instanceof Collection) 628 { 629 ((Collection)object).clear(); 630 } 631 632 return object; 633 } 634 635 636 637 public static final Object reverse(Object object) 638 { 639 List list = TypeUtil.isList(object); 640 641 if (list != null) 642 { 643 Collections.reverse(list); 644 object = list; 645 } 646 647 return object; 648 } 649 650 651 652 public static final Object rotate(Object object, Object distance) 653 { 654 List list = TypeUtil.isList(object); 655 656 if (list != null) 657 { 658 Integer d = TypeUtil.isInteger(distance); 659 660 Collections.rotate(list, d.intValue()); 661 object = list; 662 } 663 664 return object; 665 } 666 667 668 669 public static final Object shuffle(Object object) 670 { 671 List list = TypeUtil.isList(object); 672 673 if (list != null) 674 { 675 Collections.shuffle(list); 676 object = list; 677 } 678 679 return object; 680 } 681 682 683 684 public static final Object add(Object object, Object added) 685 { 686 Collection collection = TypeUtil.isCollection(object); 687 688 if (collection != null) 689 { 690 collection.add(added); 691 object = collection; 692 } 693 694 return object; 695 } 696 697 698 699 public static final Object addAll(Object object, Object added) 700 { 701 Collection collection = TypeUtil.isCollection(object); 702 703 if (collection != null) 704 { 705 Collection addedCollection = TypeUtil.isCollection(added); 706 707 if (addedCollection != null) 708 { 709 collection.addAll(addedCollection); 710 object = collection; 711 } 712 } 713 714 return object; 715 } 716 717 718 719 public static final Object remove(Object object, Object added) 720 { 721 Collection collection = TypeUtil.isCollection(object); 722 723 if (collection != null) 724 { 725 collection.remove(added); 726 object = collection; 727 } 728 729 return object; 730 } 731 732 733 734 public static final Object removeAll(Object object, Object removed) 735 { 736 Collection collection = TypeUtil.isCollection(object); 737 738 if (collection != null) 739 { 740 Collection removedCollection = TypeUtil.isCollection(removed); 741 742 if (removedCollection != null) 743 { 744 collection.removeAll(removedCollection); 745 object = collection; 746 } 747 } 748 749 return object; 750 } 751 752 753 754 public static final Object retainAll(Object object, Object removed) 755 { 756 Collection collection = TypeUtil.isCollection(object); 757 758 if (collection != null) 759 { 760 Collection removedCollection = TypeUtil.isCollection(removed); 761 762 if (removedCollection != null) 763 { 764 collection.retainAll(removedCollection); 765 object = collection; 766 } 767 } 768 769 return object; 770 } 771 772 773 774 public static final Object ifContains(Object object, Object contained, Object ifObject, Object elseObject) 775 { 776 Collection collection = TypeUtil.isCollection(object); 777 778 boolean isContained = false; 779 780 if (collection != null) 781 { 782 isContained = collection.contains(contained); 783 } 784 785 if (isContained) 786 { 787 return ifObject; 788 } 789 else 790 { 791 return elseObject; 792 } 793 } 794 795 public static final Object ifContains(Object object, Object contained, Object ifObject) 796 { 797 return ifContains(object, contained, ifObject, null); 798 } 799 800 public static final Object ifContains(Object object, Object contained) 801 { 802 return ifContains(object, contained, object, null); 803 } 804 805 806 807 public static final Object ifContainsAll(Object object, Object contained, Object ifObject, Object elseObject) 808 { 809 Collection collection = TypeUtil.isCollection(object); 810 811 boolean isContained = false; 812 813 if (collection != null) 814 { 815 Collection containedCollection = TypeUtil.isCollection(contained); 816 817 if (containedCollection != null) 818 { 819 isContained = collection.containsAll(containedCollection); 820 } 821 } 822 823 if (isContained) 824 { 825 return ifObject; 826 } 827 else 828 { 829 return elseObject; 830 } 831 } 832 833 public static final Object ifContainsAll(Object object, Object contained, Object ifObject) 834 { 835 return ifContainsAll(object, contained, ifObject, null); 836 } 837 838 public static final Object ifContainsAll(Object object, Object contained) 839 { 840 return ifContainsAll(object, contained, object, null); 841 } 842 843 844 845 public static Object ifEqual(Object object, Object target, Object ifObject, Object elseObject) 846 { 847 boolean isEqual = false; 848 849 if (object == null) 850 { 851 if (target == null) 852 { 853 isEqual = true; 854 } 855 } 856 else 857 { 858 isEqual = object.equals(target); 859 } 860 861 if (isEqual) 862 { 863 return ifObject; 864 } 865 else 866 { 867 return elseObject; 868 } 869 } 870 871 872 873 public static Object isEqual(Object object, Object target) 874 { 875 return ifEqual(object, target, Boolean.TRUE, null); 876 } 877 878 879 880 public static Object isNotEqual(Object object, Object target) 881 { 882 return ifEqual(object, target, null, Boolean.TRUE); 883 } 884 885 886 887 public static Object ifEqual(Object object, Object target, Object ifObject) 888 { 889 return ifEqual(object, target, ifObject, object); 890 } 891 892 893 894 public static Object ifNotEqual(Object object, Object target, Object ifObject) 895 { 896 return ifEqual(object, target, object, ifObject); 897 } 898 899 900 901 public static final Object ifEmpty(Object object, Object ifObject, Object elseObject) 902 { 903 if (TypeUtil.isEmpty(object)) 904 { 905 return ifObject; 906 } 907 else 908 { 909 return elseObject; 910 } 911 } 912 913 914 915 public static final Object isEmpty(Object object) 916 { 917 return ifEmpty(object, Boolean.TRUE, null); 918 } 919 920 921 922 public static final Object isNotEmpty(Object object) 923 { 924 return ifEmpty(object, null, Boolean.TRUE); 925 } 926 927 928 929 public static final Object ifEmpty(Object object, Object ifObject) 930 { 931 return ifEmpty(object, ifObject, object); 932 } 933 934 935 936 public static final Object ifNotEmpty(Object object, Object ifObject) 937 { 938 return ifEmpty(object, object, ifObject); 939 } 940 941 942 943 public static final Object isInternetAddress(Object object) 944 { 945 InternetAddress address = null; 946 947 try 948 { 949 if (object instanceof InternetAddress) 950 { 951 address = (InternetAddress)object; 952 } 953 else 954 { 955 address = new InternetAddress(object.toString()); 956 } 957 958 address.validate(); 959 } 960 catch (Exception e) 961 { 962 address = null; 963 } 964 965 return address; 966 } 967 968 public static final Object indexOfSubList(Object source, Object target) throws Exception 969 { 970 List sourceList = TypeUtil.isList(source); 971 List targetList = TypeUtil.isList(target); 972 973 Integer returnValue = null; 974 975 if ((sourceList != null) && (targetList != null)) 976 { 977 returnValue = new Integer (Collections.indexOfSubList(sourceList, targetList)); 978 } 979 980 return returnValue; 981 } 982 983 public static final Object lastIndexOfSubList(Object source, Object target) throws Exception 984 { 985 List sourceList = TypeUtil.isList(source); 986 List targetList = TypeUtil.isList(target); 987 988 Integer returnValue = null; 989 990 if ((sourceList != null) && (targetList != null)) 991 { 992 returnValue = new Integer (Collections.lastIndexOfSubList(sourceList, targetList)); 993 } 994 995 return returnValue; 996 } 997 998 public static final Object subList(Object object, Object startObject, Object endObject) throws Exception 999 { 1000 Object returnValue = null; 1001 1002 List objectList = TypeUtil.isList(object); 1003 1004 if (objectList != null) 1005 { 1006 int start = 0; 1007 int end = 0; 1008 1009 Integer startInteger = TypeUtil.isInteger(startObject); 1010 Integer endInteger = TypeUtil.isInteger(endObject); 1011 1012 if (startInteger != null) 1013 { 1014 start = startInteger.intValue(); 1015 } 1016 1017 if (endInteger != null) 1018 { 1019 end = endInteger.intValue(); 1020 } 1021 1022 returnValue = TypeUtil.subList(objectList, start, end); 1023 } 1024 1025 return returnValue; 1026 } 1027 1028 public static final Object subList(Object object, Object startObject) throws Exception 1029 { 1030 return subList(object, startObject, null); 1031 } 1032 1033 1034 1035 public static final Object makeSynchronized(Object object) throws Exception 1036 { 1037 if (object != null) 1038 { 1039 if (object instanceof SortedSet) 1040 { 1041 object = Collections.synchronizedSortedSet((SortedSet)object); 1042 } 1043 else if (object instanceof SortedMap) 1044 { 1045 object = Collections.synchronizedSortedMap((SortedMap)object); 1046 } 1047 else if (object instanceof Set) 1048 { 1049 object = Collections.synchronizedSet((Set)object); 1050 } 1051 else if (object instanceof Map) 1052 { 1053 object = Collections.synchronizedMap((Map)object); 1054 } 1055 else if (object instanceof List) 1056 { 1057 object = Collections.synchronizedList((List)object); 1058 } 1059 else 1060 { 1061 object = Collections.synchronizedCollection((Collection)object); 1062 } 1063 } 1064 1065 return object; 1066 } 1067 1068 1069 1070 public static final Object makeUnmodifiable(Object object) throws Exception 1071 { 1072 if (object != null) 1073 { 1074 if (object instanceof SortedSet) 1075 { 1076 object = Collections.unmodifiableSortedSet((SortedSet)object); 1077 } 1078 else if (object instanceof SortedMap) 1079 { 1080 object = Collections.unmodifiableSortedMap((SortedMap)object); 1081 } 1082 else if (object instanceof Set) 1083 { 1084 object = Collections.unmodifiableSet((Set)object); 1085 } 1086 else if (object instanceof Map) 1087 { 1088 object = Collections.unmodifiableMap((Map)object); 1089 } 1090 else if (object instanceof List) 1091 { 1092 object = Collections.unmodifiableList((List)object); 1093 } 1094 else 1095 { 1096 object = Collections.unmodifiableCollection((Collection)object); 1097 } 1098 } 1099 1100 return object; 1101 } 1102 1103} 1104 | Popular Tags |