1 package com.fasterxml.jackson.databind.node; 2 3 import java.io.IOException; 4 import java.math.BigDecimal; 5 import java.math.BigInteger; 6 7 import com.fasterxml.jackson.core.*; 8 import com.fasterxml.jackson.databind.*; 9 10 /** 11 * Numeric node that contains values that do not fit in simple 12 * integer (int, long) or floating point (double) values. 13 */ 14 @SuppressWarnings("serial") 15 public class DecimalNode 16 extends NumericNode 17 { 18 public static final DecimalNode ZERO = new DecimalNode(BigDecimal.ZERO); 19 20 private final static BigDecimal MIN_INTEGER = BigDecimal.valueOf(Integer.MIN_VALUE); 21 private final static BigDecimal MAX_INTEGER = BigDecimal.valueOf(Integer.MAX_VALUE); 22 private final static BigDecimal MIN_LONG = BigDecimal.valueOf(Long.MIN_VALUE); 23 private final static BigDecimal MAX_LONG = BigDecimal.valueOf(Long.MAX_VALUE); 24 25 final protected BigDecimal _value; 26 27 /* 28 /********************************************************** 29 /* Construction 30 /********************************************************** 31 */ 32 DecimalNode(BigDecimal v)33 public DecimalNode(BigDecimal v) { _value = v; } 34 valueOf(BigDecimal d)35 public static DecimalNode valueOf(BigDecimal d) { return new DecimalNode(d); } 36 37 /* 38 /********************************************************** 39 /* BaseJsonNode extended API 40 /********************************************************** 41 */ 42 asToken()43 @Override public JsonToken asToken() { return JsonToken.VALUE_NUMBER_FLOAT; } 44 45 @Override numberType()46 public JsonParser.NumberType numberType() { return JsonParser.NumberType.BIG_DECIMAL; } 47 48 /* 49 /********************************************************** 50 /* Overrridden JsonNode methods 51 /********************************************************** 52 */ 53 54 @Override isFloatingPointNumber()55 public boolean isFloatingPointNumber() { return true; } 56 57 @Override isBigDecimal()58 public boolean isBigDecimal() { return true; } 59 canConvertToInt()60 @Override public boolean canConvertToInt() { 61 return (_value.compareTo(MIN_INTEGER) >= 0) && (_value.compareTo(MAX_INTEGER) <= 0); 62 } canConvertToLong()63 @Override public boolean canConvertToLong() { 64 return (_value.compareTo(MIN_LONG) >= 0) && (_value.compareTo(MAX_LONG) <= 0); 65 } 66 67 @Override numberValue()68 public Number numberValue() { return _value; } 69 70 @Override shortValue()71 public short shortValue() { return _value.shortValue(); } 72 73 @Override intValue()74 public int intValue() { return _value.intValue(); } 75 76 @Override longValue()77 public long longValue() { return _value.longValue(); } 78 79 80 @Override bigIntegerValue()81 public BigInteger bigIntegerValue() { return _value.toBigInteger(); } 82 83 @Override floatValue()84 public float floatValue() { return _value.floatValue(); } 85 86 @Override doubleValue()87 public double doubleValue() { return _value.doubleValue(); } 88 89 @Override decimalValue()90 public BigDecimal decimalValue() { return _value; } 91 92 @Override asText()93 public String asText() { 94 return _value.toString(); 95 } 96 97 @Override serialize(JsonGenerator jgen, SerializerProvider provider)98 public final void serialize(JsonGenerator jgen, SerializerProvider provider) 99 throws IOException, JsonProcessingException 100 { 101 // 07-Jul-2013, tatu: Should be handled by propagating setting to JsonGenerator 102 // so this should not be needed: 103 /* 104 if (provider.isEnabled(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN)) { 105 if (!(jgen instanceof TokenBuffer)) { // [Issue#232] 106 jgen.writeNumber(((BigDecimal) _value).toPlainString()); 107 return; 108 } 109 } 110 */ 111 jgen.writeNumber(_value); 112 } 113 114 @Override equals(Object o)115 public boolean equals(Object o) 116 { 117 if (o == this) return true; 118 if (o == null) return false; 119 if (o instanceof DecimalNode) { 120 return ((DecimalNode) o)._value.compareTo(_value) == 0; 121 } 122 return false; 123 } 124 125 @Override hashCode()126 public int hashCode() { return Double.valueOf(doubleValue()).hashCode(); } 127 } 128