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 simple 32-bit integer values. 14 */ 15 @SuppressWarnings("serial") 16 public class IntNode 17 extends NumericNode 18 { 19 // // // Let's cache small set of common value 20 21 final static int MIN_CANONICAL = -1; 22 final static int MAX_CANONICAL = 10; 23 24 private final static IntNode[] CANONICALS; 25 static { 26 int count = MAX_CANONICAL - MIN_CANONICAL + 1; 27 CANONICALS = new IntNode[count]; 28 for (int i = 0; i < count; ++i) { 29 CANONICALS[i] = new IntNode(MIN_CANONICAL + i); 30 } 31 } 32 33 /** 34 * Integer value this node contains 35 */ 36 protected final int _value; 37 38 /* 39 ************************************************ 40 * Construction 41 ************************************************ 42 */ 43 IntNode(int v)44 public IntNode(int v) { _value = v; } 45 valueOf(int i)46 public static IntNode valueOf(int i) { 47 if (i > MAX_CANONICAL || i < MIN_CANONICAL) return new IntNode(i); 48 return CANONICALS[i - MIN_CANONICAL]; 49 } 50 51 /* 52 /********************************************************** 53 /* BaseJsonNode extended API 54 /********************************************************** 55 */ 56 asToken()57 @Override public JsonToken asToken() { return JsonToken.VALUE_NUMBER_INT; } 58 59 @Override numberType()60 public JsonParser.NumberType numberType() { return JsonParser.NumberType.INT; } 61 62 /* 63 /********************************************************** 64 /* Overrridden JsonNode methods 65 /********************************************************** 66 */ 67 68 @Override isIntegralNumber()69 public boolean isIntegralNumber() { return true; } 70 71 @Override isInt()72 public boolean isInt() { return true; } 73 canConvertToInt()74 @Override public boolean canConvertToInt() { return true; } canConvertToLong()75 @Override public boolean canConvertToLong() { return true; } 76 77 @Override numberValue()78 public Number numberValue() { 79 return Integer.valueOf(_value); 80 } 81 82 @Override shortValue()83 public short shortValue() { return (short) _value; } 84 85 @Override intValue()86 public int intValue() { return _value; } 87 88 @Override longValue()89 public long longValue() { return (long) _value; } 90 91 @Override floatValue()92 public float floatValue() { return (float) _value; } 93 94 @Override doubleValue()95 public double doubleValue() { return (double) _value; } 96 97 98 @Override decimalValue()99 public BigDecimal decimalValue() { return BigDecimal.valueOf(_value); } 100 101 @Override bigIntegerValue()102 public BigInteger bigIntegerValue() { return BigInteger.valueOf(_value); } 103 104 @Override asText()105 public String asText() { 106 return NumberOutput.toString(_value); 107 } 108 109 @Override asBoolean(boolean defaultValue)110 public boolean asBoolean(boolean defaultValue) { 111 return _value != 0; 112 } 113 114 @Override serialize(JsonGenerator jg, SerializerProvider provider)115 public final void serialize(JsonGenerator jg, SerializerProvider provider) 116 throws IOException, JsonProcessingException 117 { 118 jg.writeNumber(_value); 119 } 120 121 @Override equals(Object o)122 public boolean equals(Object o) 123 { 124 if (o == this) return true; 125 if (o == null) return false; 126 if (o instanceof IntNode) { 127 return ((IntNode) o)._value == _value; 128 } 129 return false; 130 } 131 132 @Override hashCode()133 public int hashCode() { return _value; } 134 } 135