88import java .math .BigInteger ;
99import java .util .ArrayList ;
1010import java .util .HashMap ;
11+ import java .util .HashSet ;
1112import java .util .List ;
1213import java .util .Map ;
1314import java .util .Objects ;
15+ import java .util .Set ;
1416import java .util .function .Function ;
1517
1618
@@ -38,14 +40,28 @@ public class NumberAsdfNode extends AsdfNodeBase {
3840 BIG_INTEGER_CONVERTERS .put (Byte .class , o -> BigInteger .valueOf ((Byte ) o ));
3941 }
4042
43+ private static final Set <Class <? extends Number >> PRIMITIVE_INTEGRAL_CLASSES = new HashSet <>();
44+ static {
45+ PRIMITIVE_INTEGRAL_CLASSES .add (Byte .class );
46+ PRIMITIVE_INTEGRAL_CLASSES .add (Short .class );
47+ PRIMITIVE_INTEGRAL_CLASSES .add (Integer .class );
48+ PRIMITIVE_INTEGRAL_CLASSES .add (Long .class );
49+ }
50+
51+ private static final Set <Class <? extends Number >> PRIMITIVE_DECIMAL_CLASSES = new HashSet <>();
52+ static {
53+ PRIMITIVE_DECIMAL_CLASSES .add (Float .class );
54+ PRIMITIVE_DECIMAL_CLASSES .add (Double .class );
55+ }
56+
4157 public static NumberAsdfNode of (final ScalarNode scalarNode , final Number value ) {
4258 return new NumberAsdfNode (scalarNode .getTag ().getValue (), scalarNode .getValue (), value );
4359 }
4460
4561 public static NumberAsdfNode of (final Number value ) {
46- if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long || value instanceof BigInteger ) {
62+ if (PRIMITIVE_INTEGRAL_CLASSES . contains ( value . getClass ()) || value . getClass (). equals ( BigInteger . class ) ) {
4763 return new NumberAsdfNode (Tag .INT .getValue (), value .toString (), value );
48- } else if (value instanceof Float || value instanceof Double || value instanceof BigDecimal ) {
64+ } else if (PRIMITIVE_DECIMAL_CLASSES . contains ( value . getClass ()) || value . getClass (). equals ( BigDecimal . class ) ) {
4965 return new NumberAsdfNode (Tag .FLOAT .getValue (), value .toString (), value );
5066 } else {
5167 throw new IllegalArgumentException ("Unexpected Number subclass: " + value .getClass ().getName ());
@@ -76,7 +92,7 @@ public String getTag() {
7692 public BigDecimal asBigDecimal () {
7793 final Function <Object , BigDecimal > converter = BIG_DECIMAL_CONVERTERS .get (value .getClass ());
7894 if (converter == null ) {
79- throw new RuntimeException ("Node cannot be represented as BigDecimal" );
95+ throw new IllegalStateException ("Node cannot be represented as BigDecimal" );
8096 }
8197 return converter .apply (value );
8298 }
@@ -85,14 +101,18 @@ public BigDecimal asBigDecimal() {
85101 public BigInteger asBigInteger () {
86102 final Function <Object , BigInteger > converter = BIG_INTEGER_CONVERTERS .get (value .getClass ());
87103 if (converter == null ) {
88- throw new RuntimeException ("Node cannot be represented as BigInteger" );
104+ throw new IllegalStateException ("Node cannot be represented as BigInteger" );
89105 }
90106 return converter .apply (value );
91107 }
92108
93109 @ Override
94110 public byte asByte () {
95- return value .byteValue ();
111+ if (value instanceof Byte ) {
112+ return (Byte )value ;
113+ } else {
114+ throw new IllegalStateException ("Node cannot be represented as byte" );
115+ }
96116 }
97117
98118 @ Override
@@ -107,12 +127,20 @@ public float asFloat() {
107127
108128 @ Override
109129 public int asInt () {
110- return value .intValue ();
130+ if (value instanceof Byte || value instanceof Short || value instanceof Integer ) {
131+ return value .intValue ();
132+ } else {
133+ throw new IllegalStateException ("Node cannot be represented as long" );
134+ }
111135 }
112136
113137 @ Override
114138 public long asLong () {
115- return value .longValue ();
139+ if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long ) {
140+ return value .longValue ();
141+ } else {
142+ throw new IllegalStateException ("Node cannot be represented as long" );
143+ }
116144 }
117145
118146 @ Override
@@ -122,7 +150,11 @@ public Number asNumber() {
122150
123151 @ Override
124152 public short asShort () {
125- return value .shortValue ();
153+ if (value instanceof Byte || value instanceof Short ) {
154+ return value .shortValue ();
155+ } else {
156+ throw new IllegalStateException ("Node cannot be represented as short" );
157+ }
126158 }
127159
128160 @ Override
0 commit comments