1 23 package com.sun.enterprise.tools.jsfext.util; 24 25 import java.math.BigDecimal ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 29 30 169 public class TypeConverter extends Object { 170 171 174 private TypeConverter() { 175 super(); 176 } 177 178 183 public static Map getTypeConversions() { 184 return typeConversions; 185 } 186 187 192 public static void registerTypeConversion(Object key, TypeConversion conversion) { 193 typeConversions.put(key, conversion); 194 } 195 196 230 public static Object asType(Object typeKey, Object value) { 231 if (value == null) { 232 return null; 233 } 234 235 240 241 if (typeKey == null) { 242 return value; 243 } 244 245 if (typeKey instanceof Class && ((Class ) typeKey) != Object .class) { 247 if (((Class ) typeKey).isInstance(value)) { 248 return value; 249 } 250 } 251 252 TypeConversion conversion = (TypeConversion) typeConversions.get(typeKey); 254 255 if (conversion != null) { 257 return conversion.convertValue(value); 258 } else { 259 throw new IllegalArgumentException ("Could not find type conversion for " 260 + "type \"" + typeKey + "\" (value = \"" + value + "\""); 261 } 262 } 263 264 267 public static byte asByte(Object value) { 268 return asByte(value, (byte) 0); 269 } 270 271 274 public static byte asByte(Object value, byte defaultValue) { 275 value = asType(Byte .class, value); 276 if (value != null) { 277 return ((Byte ) value).byteValue(); 278 } 279 return defaultValue; 280 } 281 282 285 public static short asShort(Object value) { 286 return asShort(value, (short) 0); 287 } 288 289 292 public static short asShort(Object value, short defaultValue) { 293 value = asType(Short .class, value); 294 if (value != null) { 295 return ((Short ) value).shortValue(); 296 } 297 return defaultValue; 298 } 299 300 303 public static int asInt(Object value) { 304 return asInt(value, 0); 305 } 306 307 310 public static int asInt(Object value, int defaultValue) { 311 value = asType(Integer .class, value); 312 if (value != null) { 313 return ((Integer ) value).intValue(); 314 } 315 return defaultValue; 316 } 317 318 321 public static long asLong(Object value) { 322 return asLong(value, 0L); 323 } 324 325 328 public static long asLong(Object value, long defaultValue) { 329 value = asType(Long .class, value); 330 if (value != null) { 331 return ((Long ) value).longValue(); 332 } 333 return defaultValue; 334 } 335 336 339 public static float asFloat(Object value) { 340 return asFloat(value, 0F); 341 } 342 343 346 public static float asFloat(Object value, float defaultValue) { 347 value = asType(Float .class, value); 348 if (value != null) { 349 return ((Float ) value).floatValue(); 350 } 351 return defaultValue; 352 } 353 354 357 public static double asDouble(Object value) { 358 return asDouble(value, 0D); 359 } 360 361 364 public static double asDouble(Object value, double defaultValue) { 365 value = asType(Double .class, value); 366 if (value != null) { 367 return ((Double ) value).doubleValue(); 368 } 369 return defaultValue; 370 } 371 372 375 public static char asChar(Object value) { 376 return asChar(value, (char) 0); 377 } 378 379 382 public static char asChar(Object value, char defaultValue) { 383 value = asType(Character .class, value); 384 if (value != null) { 385 return ((Character ) value).charValue(); 386 } 387 return defaultValue; 388 } 389 390 393 public static boolean asBoolean(Object value) { 394 return asBoolean(value, false); 395 } 396 397 400 public static boolean asBoolean(Object value, boolean defaultValue) { 401 value = asType(Boolean .class, value); 402 if (value != null) { 403 return ((Boolean ) value).booleanValue(); 404 } 405 return defaultValue; 406 } 407 408 411 public static String asString(Object value) { 412 return (String ) asType(String .class, value); 413 } 414 415 418 public static String asString(Object value, String defaultValue) { 419 value = asType(String .class, value); 420 if (value != null) { 421 return (String ) value; 422 } 423 return defaultValue; 424 } 425 426 427 431 434 public static class UnknownTypeConversion implements TypeConversion { 435 436 439 public Object convertValue(Object value) { 440 return value; 441 } 442 } 443 444 447 public static class StringTypeConversion implements TypeConversion { 448 449 452 public Object convertValue(Object value) { 453 if (value == null) { 454 return null; 455 } 456 457 if (value.getClass().isArray()) { 458 if (value.getClass().getComponentType() == Byte.TYPE) { 460 value = new String ((byte[]) value); 461 } else if (value.getClass().getComponentType() == Character.TYPE) { 462 value = new String ((char[]) value); 463 } 464 } else if (!(value instanceof String )) { 465 value = value.toString(); 466 } 467 return value; 468 } 469 } 470 471 474 public static class IntegerTypeConversion implements TypeConversion { 475 476 479 public Object convertValue(Object value) { 480 if (value == null) { 481 return null; 482 } 483 484 if (!(value instanceof Integer )) { 485 String v = value.toString(); 486 if (v.trim().length() == 0) { 487 value = null; 488 } else { 489 value = new Integer (v); 490 } 491 } 492 493 return value; 494 } 495 } 496 497 500 public static class DoubleTypeConversion implements TypeConversion { 501 502 505 public Object convertValue(Object value) { 506 if (value == null) { 507 return null; 508 } 509 510 if (!(value instanceof Double )) { 511 String v = value.toString(); 512 if (v.trim().length() == 0) { 513 value = null; 514 } else { 515 value = new Double (v); 516 } 517 } 518 519 return value; 520 } 521 } 522 523 526 public static class BooleanTypeConversion implements TypeConversion { 527 528 531 public Object convertValue(Object value) { 532 if (value == null) { 533 return null; 534 } 535 536 if (!(value instanceof Boolean )) { 537 String v = value.toString(); 538 if (v.trim().length() == 0) { 539 value = null; 540 } else { 541 value = Boolean.valueOf(v); 542 } 543 } 544 545 return value; 546 } 547 } 548 549 552 public static class LongTypeConversion implements TypeConversion { 553 554 557 public Object convertValue(Object value) { 558 if (value == null) { 559 return null; 560 } 561 562 if (!(value instanceof Long )) { 563 String v = value.toString(); 564 if (v.trim().length() == 0) { 565 value = null; 566 } else { 567 value = new Long (v); 568 } 569 } 570 571 return value; 572 } 573 } 574 575 578 public static class FloatTypeConversion implements TypeConversion { 579 580 583 public Object convertValue(Object value) { 584 if (value == null) { 585 return null; 586 } 587 588 if (!(value instanceof Float )) { 589 String v = value.toString(); 590 if (v.trim().length() == 0) { 591 value = null; 592 } else { 593 value = new Float (v); 594 } 595 } 596 597 return value; 598 } 599 } 600 601 604 public static class ShortTypeConversion implements TypeConversion { 605 606 609 public Object convertValue(Object value) { 610 if (value == null) { 611 return null; 612 } 613 614 if (!(value instanceof Short )) { 615 String v = value.toString(); 616 if (v.trim().length() == 0) { 617 value = null; 618 } else { 619 value = new Short (v); 620 } 621 } 622 623 return value; 624 } 625 } 626 627 630 public static class BigDecimalTypeConversion implements TypeConversion { 631 632 635 public Object convertValue(Object value) { 636 if (value == null) { 637 return null; 638 } 639 640 if (!(value instanceof BigDecimal )) { 641 String v = value.toString(); 642 if (v.trim().length() == 0) { 643 value = null; 644 } else { 645 value = new BigDecimal (v); 646 } 647 } 648 649 return value; 650 } 651 } 652 653 656 public static class ByteTypeConversion implements TypeConversion { 657 658 661 public Object convertValue(Object value) { 662 if (value == null) { 663 return null; 664 } 665 666 if (!(value instanceof Byte )) { 667 String v = value.toString(); 668 if (v.trim().length() == 0) { 669 value = null; 670 } else { 671 value = new Byte (v); 672 } 673 } 674 675 return value; 676 } 677 } 678 679 682 public static class CharacterTypeConversion implements TypeConversion { 683 684 687 public Object convertValue(Object value) { 688 if (value == null) { 689 return null; 690 } 691 692 if (!(value instanceof Character )) { 693 String v = value.toString(); 694 if (v.trim().length() == 0) { 695 value = null; 696 } else { 697 value = new Character (v.charAt(0)); 698 } 699 } 700 701 return value; 702 } 703 } 704 705 708 public static class SqlDateTypeConversion implements TypeConversion { 709 712 public Object convertValue(Object value) { 713 if (value == null) { 714 return null; 715 } 716 717 if (!(value instanceof java.sql.Date )) { 718 String v = value.toString(); 719 if (v.trim().length() == 0) { 720 value = null; 721 } else { 722 value = java.sql.Date.valueOf(v); 724 } 725 } 726 727 return value; 728 } 729 } 730 731 734 public static class SqlTimeTypeConversion implements TypeConversion { 735 736 739 public Object convertValue(Object value) { 740 if (value == null) { 741 return null; 742 } 743 744 if (!(value instanceof java.sql.Time )) { 745 String v = value.toString(); 746 if (v.trim().length() == 0) { 747 value = null; 748 } else { 749 value = java.sql.Time.valueOf(v); 751 } 752 } 753 754 return value; 755 } 756 } 757 758 759 762 public static class SqlTimestampTypeConversion implements TypeConversion { 763 764 767 public Object convertValue(Object value) { 768 if (value == null) { 769 return null; 770 } 771 772 if (!(value instanceof java.sql.Timestamp )) { 773 String v = value.toString(); 774 if (v.trim().length() == 0) { 775 value = null; 776 } else { 777 value = java.sql.Timestamp.valueOf(v); 780 } 781 } 782 783 return value; 784 } 785 } 786 787 788 791 public static class ObjectTypeConversion implements TypeConversion { 792 public Object convertValue(Object value) { 793 828 829 return value; 830 } 831 } 832 833 834 835 836 840 private static class TestSuperclass extends Object { 841 } 842 843 private static class Test extends TestSuperclass { 844 public static void main(String [] args) { 845 if (!(TypeConverter.asString(new Integer (12)) instanceof String )) { 846 throw new Error (); 847 } 848 849 if (!(TypeConverter.asType(Integer .class, "12") instanceof Integer )) { 850 throw new Error (); 851 } 852 853 if (!(TypeConverter.asType(Long .class, "12") instanceof Long )) { 854 throw new Error (); 855 } 856 857 if (!(TypeConverter.asType(Float .class, "12.0") instanceof Float )) { 858 throw new Error (); 859 } 860 861 if (!(TypeConverter.asType(Double .class, "12.0") instanceof Double )) { 862 throw new Error (); 863 } 864 865 if (!(TypeConverter.asType(Short .class, "12") instanceof Short )) { 866 throw new Error (); 867 } 868 869 if (!(TypeConverter.asType(BigDecimal .class, "12") instanceof BigDecimal )) { 870 throw new Error (); 871 } 872 873 if (!(TypeConverter.asType(Boolean .class, "true") instanceof Boolean )) { 874 throw new Error (); 875 } 876 877 if (!(TypeConverter.asType(Byte .class, "12") instanceof Byte )) { 878 throw new Error (); 879 } 880 881 if (!(TypeConverter.asType(Character .class, "1") instanceof Character )) { 882 throw new Error (); 883 } 884 885 System.out.println("Test passed."); 886 } 887 } 888 889 890 891 892 896 private static final Map typeConversions = new HashMap (); 897 898 899 public static final String TYPE_UNKNOWN = "null"; 900 901 902 public static final String TYPE_OBJECT = "object"; 903 904 905 public static final String TYPE_STRING = "string"; 906 907 908 public static final String TYPE_INT = "int"; 909 910 911 public static final String TYPE_INTEGER = "integer"; 912 913 914 public static final String TYPE_LONG = "long"; 915 916 917 public static final String TYPE_FLOAT = "float"; 918 919 920 public static final String TYPE_DOUBLE = "double"; 921 922 923 public static final String TYPE_SHORT = "short"; 924 925 926 public static final String TYPE_BOOLEAN = "boolean"; 927 928 929 public static final String TYPE_BYTE = "byte"; 930 931 932 public static final String TYPE_CHAR = "char"; 933 934 935 public static final String TYPE_CHARACTER = "character"; 936 937 938 public static final String TYPE_BIG_DECIMAL = "bigdecimal"; 939 940 941 public static final String TYPE_SQL_DATE = "sqldate"; 942 943 944 public static final String TYPE_SQL_TIME = "sqltime"; 945 946 947 public static final String TYPE_SQL_TIMESTAMP = "sqltimestamp"; 948 949 public static final TypeConversion UNKNOWN_TYPE_CONVERSION = 950 new UnknownTypeConversion(); 951 public static final TypeConversion OBJECT_TYPE_CONVERSION = 952 new ObjectTypeConversion(); 953 public static final TypeConversion STRING_TYPE_CONVERSION = 954 new StringTypeConversion(); 955 public static final TypeConversion INTEGER_TYPE_CONVERSION = 956 new IntegerTypeConversion(); 957 public static final TypeConversion DOUBLE_TYPE_CONVERSION = 958 new DoubleTypeConversion(); 959 public static final TypeConversion BOOLEAN_TYPE_CONVERSION = 960 new BooleanTypeConversion(); 961 public static final TypeConversion LONG_TYPE_CONVERSION = 962 new LongTypeConversion(); 963 public static final TypeConversion FLOAT_TYPE_CONVERSION = 964 new FloatTypeConversion(); 965 public static final TypeConversion SHORT_TYPE_CONVERSION = 966 new ShortTypeConversion(); 967 public static final TypeConversion BIG_DECIMAL_TYPE_CONVERSION = 968 new BigDecimalTypeConversion(); 969 public static final TypeConversion BYTE_TYPE_CONVERSION = 970 new ByteTypeConversion(); 971 public static final TypeConversion CHARACTER_TYPE_CONVERSION = 972 new CharacterTypeConversion(); 973 public static final TypeConversion SQL_DATE_TYPE_CONVERSION = 974 new SqlDateTypeConversion(); 975 public static final TypeConversion SQL_TIME_TYPE_CONVERSION = 976 new SqlTimeTypeConversion(); 977 public static final TypeConversion SQL_TIMESTAMP_TYPE_CONVERSION = 978 new SqlTimestampTypeConversion(); 979 980 981 982 983 987 static { 988 registerTypeConversion(Object .class, OBJECT_TYPE_CONVERSION); 990 registerTypeConversion(String .class, STRING_TYPE_CONVERSION); 991 registerTypeConversion(Integer .class, INTEGER_TYPE_CONVERSION); 992 registerTypeConversion(Integer.TYPE, INTEGER_TYPE_CONVERSION); 993 registerTypeConversion(Double .class, DOUBLE_TYPE_CONVERSION); 994 registerTypeConversion(Double.TYPE, DOUBLE_TYPE_CONVERSION); 995 registerTypeConversion(Boolean .class, BOOLEAN_TYPE_CONVERSION); 996 registerTypeConversion(Boolean.TYPE, BOOLEAN_TYPE_CONVERSION); 997 registerTypeConversion(Long .class, LONG_TYPE_CONVERSION); 998 registerTypeConversion(Long.TYPE, LONG_TYPE_CONVERSION); 999 registerTypeConversion(Float .class, FLOAT_TYPE_CONVERSION); 1000 registerTypeConversion(Float.TYPE, FLOAT_TYPE_CONVERSION); 1001 registerTypeConversion(Short .class, SHORT_TYPE_CONVERSION); 1002 registerTypeConversion(Short.TYPE, SHORT_TYPE_CONVERSION); 1003 registerTypeConversion(BigDecimal .class, BIG_DECIMAL_TYPE_CONVERSION); 1004 registerTypeConversion(Byte .class, BYTE_TYPE_CONVERSION); 1005 registerTypeConversion(Byte.TYPE, BYTE_TYPE_CONVERSION); 1006 registerTypeConversion(Character .class, CHARACTER_TYPE_CONVERSION); 1007 registerTypeConversion(Character.TYPE, CHARACTER_TYPE_CONVERSION); 1008 registerTypeConversion(java.sql.Date .class, SQL_DATE_TYPE_CONVERSION); 1009 registerTypeConversion(java.sql.Time .class, SQL_TIME_TYPE_CONVERSION); 1010 registerTypeConversion(java.sql.Timestamp .class, SQL_TIMESTAMP_TYPE_CONVERSION); 1011 1012 registerTypeConversion(Object .class.getName(), OBJECT_TYPE_CONVERSION); 1014 registerTypeConversion(String .class.getName(), STRING_TYPE_CONVERSION); 1015 registerTypeConversion(Integer .class.getName(), INTEGER_TYPE_CONVERSION); 1016 registerTypeConversion(Double .class.getName(), DOUBLE_TYPE_CONVERSION); 1017 registerTypeConversion(Boolean .class.getName(), BOOLEAN_TYPE_CONVERSION); 1018 registerTypeConversion(Long .class.getName(), LONG_TYPE_CONVERSION); 1019 registerTypeConversion(Float .class.getName(), FLOAT_TYPE_CONVERSION); 1020 registerTypeConversion(Short .class.getName(), SHORT_TYPE_CONVERSION); 1021 registerTypeConversion(BigDecimal .class.getName(), BIG_DECIMAL_TYPE_CONVERSION); 1022 registerTypeConversion(Byte .class.getName(), BYTE_TYPE_CONVERSION); 1023 registerTypeConversion(Character .class.getName(), CHARACTER_TYPE_CONVERSION); 1024 registerTypeConversion(java.sql.Date .class.getName(), SQL_DATE_TYPE_CONVERSION); 1025 registerTypeConversion(java.sql.Time .class.getName(), SQL_TIME_TYPE_CONVERSION); 1026 registerTypeConversion(java.sql.Timestamp .class.getName(), SQL_TIMESTAMP_TYPE_CONVERSION); 1027 1028 registerTypeConversion(TYPE_UNKNOWN, UNKNOWN_TYPE_CONVERSION); 1030 registerTypeConversion(TYPE_OBJECT, OBJECT_TYPE_CONVERSION); 1031 registerTypeConversion(TYPE_STRING, STRING_TYPE_CONVERSION); 1032 registerTypeConversion(TYPE_INT, INTEGER_TYPE_CONVERSION); 1033 registerTypeConversion(TYPE_INTEGER, INTEGER_TYPE_CONVERSION); 1034 registerTypeConversion(TYPE_DOUBLE, DOUBLE_TYPE_CONVERSION); 1035 registerTypeConversion(TYPE_BOOLEAN, BOOLEAN_TYPE_CONVERSION); 1036 registerTypeConversion(TYPE_LONG, LONG_TYPE_CONVERSION); 1037 registerTypeConversion(TYPE_FLOAT, FLOAT_TYPE_CONVERSION); 1038 registerTypeConversion(TYPE_SHORT, SHORT_TYPE_CONVERSION); 1039 registerTypeConversion(TYPE_BIG_DECIMAL, BIG_DECIMAL_TYPE_CONVERSION); 1040 registerTypeConversion(TYPE_BYTE, BYTE_TYPE_CONVERSION); 1041 registerTypeConversion(TYPE_CHAR, CHARACTER_TYPE_CONVERSION); 1042 registerTypeConversion(TYPE_CHARACTER, CHARACTER_TYPE_CONVERSION); 1043 registerTypeConversion(TYPE_SQL_DATE, SQL_DATE_TYPE_CONVERSION); 1044 registerTypeConversion(TYPE_SQL_TIME, SQL_TIME_TYPE_CONVERSION); 1045 registerTypeConversion(TYPE_SQL_TIMESTAMP, SQL_TIMESTAMP_TYPE_CONVERSION); 1046 } 1047} 1048 | Popular Tags |