• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.core.io.NumberOutput;
9 import com.fasterxml.jackson.databind.SerializerProvider;
10 
11 
12 /**
13  * Numeric node that contains 64-bit ("double precision")
14  * floating point values simple 32-bit integer values.
15  */
16 @SuppressWarnings("serial")
17 public class DoubleNode
18     extends NumericNode
19 {
20     protected final double _value;
21 
22     /*
23     /**********************************************************
24     /* Construction
25     /**********************************************************
26      */
27 
DoubleNode(double v)28     public DoubleNode(double v) { _value = v; }
29 
valueOf(double v)30     public static DoubleNode valueOf(double v) { return new DoubleNode(v); }
31 
32     /*
33     /**********************************************************
34     /* BaseJsonNode extended API
35     /**********************************************************
36      */
37 
asToken()38     @Override public JsonToken asToken() { return JsonToken.VALUE_NUMBER_FLOAT; }
39 
40     @Override
numberType()41     public JsonParser.NumberType numberType() { return JsonParser.NumberType.DOUBLE; }
42 
43     /*
44     /**********************************************************
45     /* Overrridden JsonNode methods
46     /**********************************************************
47      */
48 
49     @Override
isFloatingPointNumber()50     public boolean isFloatingPointNumber() { return true; }
51 
52     @Override
isDouble()53     public boolean isDouble() { return true; }
54 
canConvertToInt()55     @Override public boolean canConvertToInt() {
56         return (_value >= Integer.MIN_VALUE && _value <= Integer.MAX_VALUE);
57     }
canConvertToLong()58     @Override public boolean canConvertToLong() {
59         return (_value >= Long.MIN_VALUE && _value <= Long.MAX_VALUE);
60     }
61 
62     @Override
numberValue()63     public Number numberValue() {
64         return Double.valueOf(_value);
65     }
66 
67     @Override
shortValue()68     public short shortValue() { return (short) _value; }
69 
70     @Override
intValue()71     public int intValue() { return (int) _value; }
72 
73     @Override
longValue()74     public long longValue() { return (long) _value; }
75 
76     @Override
floatValue()77     public float floatValue() { return (float) _value; }
78 
79     @Override
doubleValue()80     public double doubleValue() { return _value; }
81 
82     @Override
decimalValue()83     public BigDecimal decimalValue() { return BigDecimal.valueOf(_value); }
84 
85     @Override
bigIntegerValue()86     public BigInteger bigIntegerValue() {
87         return decimalValue().toBigInteger();
88     }
89 
90     @Override
asText()91     public String asText() {
92         return NumberOutput.toString(_value);
93     }
94 
95     // @since 2.9
96     @Override
isNaN()97     public boolean isNaN() {
98         return Double.isNaN(_value) || Double.isInfinite(_value);
99     }
100 
101     @Override
serialize(JsonGenerator g, SerializerProvider provider)102     public final void serialize(JsonGenerator g, SerializerProvider provider) throws IOException {
103         g.writeNumber(_value);
104     }
105 
106     @Override
equals(Object o)107     public boolean equals(Object o)
108     {
109         if (o == this) return true;
110         if (o == null) return false;
111         if (o instanceof DoubleNode) {
112             // We must account for NaNs: NaN does not equal NaN, therefore we have
113             // to use Double.compare().
114             final double otherValue = ((DoubleNode) o)._value;
115             return Double.compare(_value, otherValue) == 0;
116         }
117         return false;
118     }
119 
120     @Override
hashCode()121     public int hashCode()
122     {
123         // same as hashCode Double.class uses
124         long l = Double.doubleToLongBits(_value);
125         return ((int) l) ^ (int) (l >> 32);
126 
127     }
128 }
129