@@ -585,73 +585,140 @@ public static void writeBoolean(OutputStream output, boolean value) throws IOExc
585585
586586 public static class NumberConverter {
587587
588- public static byte toByte (Number value ) {
589- if (value .byteValue () == value .shortValue ()) {
590- return value .byteValue ();
588+ public static byte toByte (Object value ) {
589+ if (value instanceof Number ) {
590+ Number number = (Number ) value ;
591+ if (number .byteValue () == number .shortValue ()) {
592+ return number .byteValue ();
593+ } else {
594+ throw new ArithmeticException ("integer overflow: " + value + " cannot be presented as byte" );
595+ }
596+ } else if (value instanceof Boolean ) {
597+ return (byte ) ((Boolean ) value ? 1 : 0 );
598+ } else if (value instanceof String ) {
599+ return Byte .parseByte (value .toString ());
591600 } else {
592- throw new ArithmeticException ( "integer overflow: " + value + " cannot be presented as byte" );
601+ throw new IllegalArgumentException ( "Cannot convert " + value + " to byte value " );
593602 }
594603 }
595604
596- public static short toShort (Number value ) {
597- if (value .shortValue () == value .intValue ()) {
598- return value .shortValue ();
605+ public static short toShort (Object value ) {
606+ if (value instanceof Number ) {
607+ Number number = (Number ) value ;
608+ if (number .shortValue () == number .intValue ()) {
609+ return number .shortValue ();
610+ } else {
611+ throw new ArithmeticException ("integer overflow: " + value + " cannot be presented as short" );
612+ }
613+ } else if (value instanceof Boolean ) {
614+ return (short ) ((Boolean ) value ? 1 : 0 );
615+ } else if ( value instanceof String ) {
616+ return Short .parseShort (value .toString ());
599617 } else {
600- throw new ArithmeticException ( "integer overflow: " + value + " cannot be presented as short" );
618+ throw new IllegalArgumentException ( "Cannot convert " + value + " to short value " );
601619 }
602620 }
603621
604- public static int toInt (Number value ) {
605- if (value .intValue () == value .longValue ()) {
606- return value .intValue ();
622+ public static int toInt (Object value ) {
623+ if (value instanceof Number ) {
624+ Number number = (Number ) value ;
625+ if (number .intValue () == number .longValue ()) {
626+ return number .intValue ();
627+ } else {
628+ throw new ArithmeticException ("integer overflow: " + value + " cannot be presented as int" );
629+ }
630+ } else if (value instanceof Boolean ) {
631+ return (Boolean ) value ? 1 : 0 ;
632+ } else if (value instanceof String ) {
633+ return Integer .parseInt ((String ) value );
607634 } else {
608- throw new ArithmeticException ( "integer overflow: " + value + " cannot be presented as int" );
635+ throw new IllegalArgumentException ( "Cannot convert " + value + " to int value " );
609636 }
610637 }
611638
612- public static long toLong (Number value ) {
613- if (value .longValue () == value .doubleValue ()) {
614- return value .longValue ();
639+ public static long toLong (Object value ) {
640+ if (value instanceof Number ) {
641+ Number number = (Number ) value ;
642+ if (number .longValue () == number .doubleValue ()) {
643+ return number .longValue ();
644+ } else {
645+ throw new ArithmeticException ("integer overflow: " + value + " cannot be presented as long" );
646+ }
647+ } else if (value instanceof Boolean ) {
648+ return (Boolean ) value ? 1 : 0 ;
649+ } else if (value instanceof String ) {
650+ return Long .parseLong ((String ) value );
615651 } else {
616- throw new ArithmeticException ( "integer overflow: " + value + " cannot be presented as long" );
652+ throw new IllegalArgumentException ( "Cannot convert " + value + " to long value " );
617653 }
618654 }
619655
620-
621- public static BigInteger toBigInteger (Number value ) {
622- return value instanceof BigInteger ? (BigInteger ) value : BigInteger .valueOf (value .longValue ());
623- }
624-
625- public static BigInteger toBigInteger (String value ) {
626- return new BigInteger (value );
656+ public static BigInteger toBigInteger (Object value ) {
657+ if (value instanceof BigInteger ) {
658+ return (BigInteger ) value ;
659+ } else if (value instanceof Number ) {
660+ return BigInteger .valueOf (((Number ) value ).longValue ());
661+ } else if (value instanceof Boolean ) {
662+ return (Boolean ) value ? BigInteger .ONE : BigInteger .ZERO ;
663+ } else if (value instanceof String ) {
664+ return new BigInteger ((String ) value );
665+ } else {
666+ throw new IllegalArgumentException ("Cannot convert " + value + " to BigInteger value" );
667+ }
627668 }
628669
629- public static float toFloat (Number value ) {
670+ public static float toFloat (Object value ) {
630671 if (value instanceof Float ) {
631672 return (Float ) value ;
632- } else if (value .floatValue () == value .doubleValue ()) {
633- return value .floatValue ();
673+ } else if (value instanceof Number ) {
674+ Number number = (Number ) value ;
675+ if (number .floatValue () == number .doubleValue ()) {
676+ return number .floatValue ();
677+ } else {
678+ throw new ArithmeticException ("float overflow: " + value + " cannot be presented as float" );
679+ }
680+ } else if (value instanceof Boolean ) {
681+ return (Boolean ) value ? 1.0f : 0.0f ;
682+ } else if (value instanceof String ) {
683+ return Float .parseFloat ((String ) value );
634684 } else {
635- throw new ArithmeticException ( "float overflow: " + value + " cannot be presented as float" );
685+ throw new IllegalArgumentException ( "Cannot convert " + value + " to float value " );
636686 }
637687 }
638688
639- public static double toDouble (Number value ) {
689+ public static double toDouble (Object value ) {
640690 if (value instanceof Double ) {
641691 return (Double ) value ;
642- } else if (value .doubleValue () == value .floatValue ()) {
643- return value .doubleValue ();
692+ } else if (value instanceof Number ) {
693+ Number number = (Number ) value ;
694+ if (number .doubleValue () == number .floatValue ()) {
695+ return number .doubleValue ();
696+ } else {
697+ throw new ArithmeticException ("double overflow: " + value + " cannot be presented as double" );
698+ }
699+ } else if (value instanceof Boolean ) {
700+ return (Boolean ) value ? 1.0 : 0.0 ;
701+ } else if (value instanceof String ) {
702+ return Double .parseDouble ((String ) value );
644703 } else {
645- throw new ArithmeticException ( "double overflow: " + value + " cannot be presented as double" );
704+ throw new IllegalArgumentException ( "Cannot convert " + value + " to double value " );
646705 }
647706 }
648707
649- public static BigDecimal toBigDecimal (Number value ) {
650- return value instanceof BigDecimal ? (BigDecimal ) value : BigDecimal .valueOf (value .doubleValue ());
651- }
652-
653- public static BigDecimal toBigDecimal (String value ) {
654- return new BigDecimal (value );
708+ public static BigDecimal toBigDecimal (Object value ) {
709+ if (value instanceof BigDecimal ) {
710+ return (BigDecimal ) value ;
711+ } else if (value instanceof BigInteger ) {
712+ return new BigDecimal ((BigInteger ) value );
713+ } else if (value instanceof Number ) {
714+ return BigDecimal .valueOf (((Number ) value ).doubleValue ());
715+ } else if (value instanceof String ) {
716+ return new BigDecimal ((String ) value );
717+ } else if (value instanceof Boolean ) {
718+ return (Boolean ) value ? BigDecimal .ONE : BigDecimal .ZERO ;
719+ } else {
720+ throw new IllegalArgumentException ("Cannot convert " + value + " to BigDecimal value" );
721+ }
655722 }
656723 }
657724}
0 commit comments