| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package org.eclipse.parsson; |
| 18 |
|
| 19 |
import jakarta.json.JsonNumber; |
| 20 |
import java.math.BigDecimal; |
| 21 |
import java.math.BigInteger; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
abstract class JsonNumberImpl implements JsonNumber { |
| 30 |
|
| 31 |
private int hashCode; |
| 32 |
private final int bigIntegerScaleLimit; |
| 33 |
|
| 34 |
JsonNumberImpl(int bigIntegerScaleLimit) { |
| 35 |
this.bigIntegerScaleLimit = bigIntegerScaleLimit; |
| 36 |
} |
| 37 |
|
| 38 |
static JsonNumber getJsonNumber(int num, int bigIntegerScaleLimit) { |
| 39 |
return new JsonIntNumber(num, bigIntegerScaleLimit); |
| 40 |
} |
| 41 |
|
| 42 |
static JsonNumber getJsonNumber(long num, int bigIntegerScaleLimit) { |
| 43 |
return new JsonLongNumber(num, bigIntegerScaleLimit); |
| 44 |
} |
| 45 |
|
| 46 |
static JsonNumber getJsonNumber(BigInteger value, int bigIntegerScaleLimit) { |
| 47 |
if (value == null) { |
| 48 |
throw new NullPointerException("Value is null"); |
| 49 |
} |
| 50 |
return new JsonBigDecimalNumber(new BigDecimal(value), bigIntegerScaleLimit); |
| 51 |
} |
| 52 |
|
| 53 |
static JsonNumber getJsonNumber(double value, int bigIntegerScaleLimit) { |
| 54 |
|
| 55 |
|
| 56 |
return new JsonBigDecimalNumber(BigDecimal.valueOf(value), bigIntegerScaleLimit); |
| 57 |
} |
| 58 |
|
| 59 |
static JsonNumber getJsonNumber(BigDecimal value, int bigIntegerScaleLimit) { |
| 60 |
if (value == null) { |
| 61 |
throw new NullPointerException("Value is null"); |
| 62 |
} |
| 63 |
return new JsonBigDecimalNumber(value, bigIntegerScaleLimit); |
| 64 |
} |
| 65 |
|
| 66 |
static JsonNumber getJsonNumber(Number value, int bigIntegerScaleLimit) { |
| 67 |
if (value == null) { |
| 68 |
throw new NullPointerException("Value is null"); |
| 69 |
} |
| 70 |
if (value instanceof Integer) { |
| 71 |
return getJsonNumber(value.intValue(), bigIntegerScaleLimit); |
| 72 |
} else if (value instanceof Long) { |
| 73 |
return getJsonNumber(value.longValue(), bigIntegerScaleLimit); |
| 74 |
} else if (value instanceof Double) { |
| 75 |
return getJsonNumber(value.doubleValue(), bigIntegerScaleLimit); |
| 76 |
} else if (value instanceof BigInteger) { |
| 77 |
return getJsonNumber((BigInteger) value, bigIntegerScaleLimit); |
| 78 |
} else if (value instanceof BigDecimal) { |
| 79 |
return getJsonNumber((BigDecimal) value, bigIntegerScaleLimit); |
| 80 |
} else { |
| 81 |
return new JsonNumberNumber(value, bigIntegerScaleLimit); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
private static final class JsonNumberNumber extends JsonNumberImpl { |
| 86 |
|
| 87 |
private final Number num; |
| 88 |
private BigDecimal bigDecimal; |
| 89 |
|
| 90 |
JsonNumberNumber(Number num, int bigIntegerScaleLimit) { |
| 91 |
super(bigIntegerScaleLimit); |
| 92 |
this.num = num; |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public Number numberValue() { |
| 97 |
return num; |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public BigDecimal bigDecimalValue() { |
| 102 |
BigDecimal bd = bigDecimal; |
| 103 |
if (bd == null) { |
| 104 |
bigDecimal = bd = new BigDecimal(num.toString()); |
| 105 |
} |
| 106 |
return bd; |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
private static final class JsonIntNumber extends JsonNumberImpl { |
| 113 |
private final int num; |
| 114 |
private BigDecimal bigDecimal; |
| 115 |
|
| 116 |
JsonIntNumber(int num, int bigIntegerScaleLimit) { |
| 117 |
super(bigIntegerScaleLimit); |
| 118 |
this.num = num; |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public boolean isIntegral() { |
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public int intValue() { |
| 128 |
return num; |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public int intValueExact() { |
| 133 |
return num; |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public long longValue() { |
| 138 |
return num; |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public long longValueExact() { |
| 143 |
return num; |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public double doubleValue() { |
| 148 |
return num; |
| 149 |
} |
| 150 |
|
| 151 |
@Override |
| 152 |
public BigDecimal bigDecimalValue() { |
| 153 |
|
| 154 |
|
| 155 |
BigDecimal bd = bigDecimal; |
| 156 |
if (bd == null) { |
| 157 |
bigDecimal = bd = new BigDecimal(num); |
| 158 |
} |
| 159 |
return bd; |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public Number numberValue() { |
| 164 |
return num; |
| 165 |
} |
| 166 |
|
| 167 |
@Override |
| 168 |
public String toString() { |
| 169 |
return Integer.toString(num); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
private static final class JsonLongNumber extends JsonNumberImpl { |
| 175 |
private final long num; |
| 176 |
private BigDecimal bigDecimal; |
| 177 |
|
| 178 |
JsonLongNumber(long num, int bigIntegerScaleLimit) { |
| 179 |
super(bigIntegerScaleLimit); |
| 180 |
this.num = num; |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public boolean isIntegral() { |
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
@Override |
| 189 |
public int intValue() { |
| 190 |
return (int) num; |
| 191 |
} |
| 192 |
|
| 193 |
@Override |
| 194 |
public int intValueExact() { |
| 195 |
return Math.toIntExact(num); |
| 196 |
} |
| 197 |
|
| 198 |
@Override |
| 199 |
public long longValue() { |
| 200 |
return num; |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public long longValueExact() { |
| 205 |
return num; |
| 206 |
} |
| 207 |
|
| 208 |
@Override |
| 209 |
public double doubleValue() { |
| 210 |
return num; |
| 211 |
} |
| 212 |
|
| 213 |
@Override |
| 214 |
public BigDecimal bigDecimalValue() { |
| 215 |
|
| 216 |
|
| 217 |
BigDecimal bd = bigDecimal; |
| 218 |
if (bd == null) { |
| 219 |
bigDecimal = bd = new BigDecimal(num); |
| 220 |
} |
| 221 |
return bd; |
| 222 |
} |
| 223 |
|
| 224 |
@Override |
| 225 |
public Number numberValue() { |
| 226 |
return num; |
| 227 |
} |
| 228 |
|
| 229 |
@Override |
| 230 |
public String toString() { |
| 231 |
return Long.toString(num); |
| 232 |
} |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
private static final class JsonBigDecimalNumber extends JsonNumberImpl { |
| 238 |
private final BigDecimal bigDecimal; |
| 239 |
|
| 240 |
JsonBigDecimalNumber(BigDecimal value, int bigIntegerScaleLimit) { |
| 241 |
super(bigIntegerScaleLimit); |
| 242 |
this.bigDecimal = value; |
| 243 |
} |
| 244 |
|
| 245 |
@Override |
| 246 |
public BigDecimal bigDecimalValue() { |
| 247 |
return bigDecimal; |
| 248 |
} |
| 249 |
|
| 250 |
@Override |
| 251 |
public Number numberValue() { |
| 252 |
return bigDecimalValue(); |
| 253 |
} |
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
@Override |
| 258 |
public boolean isIntegral() { |
| 259 |
return bigDecimalValue().scale() == 0; |
| 260 |
} |
| 261 |
|
| 262 |
@Override |
| 263 |
public int intValue() { |
| 264 |
return bigDecimalValue().intValue(); |
| 265 |
} |
| 266 |
|
| 267 |
@Override |
| 268 |
public int intValueExact() { |
| 269 |
return bigDecimalValue().intValueExact(); |
| 270 |
} |
| 271 |
|
| 272 |
@Override |
| 273 |
public long longValue() { |
| 274 |
return bigDecimalValue().longValue(); |
| 275 |
} |
| 276 |
|
| 277 |
@Override |
| 278 |
public long longValueExact() { |
| 279 |
return bigDecimalValue().longValueExact(); |
| 280 |
} |
| 281 |
|
| 282 |
@Override |
| 283 |
public double doubleValue() { |
| 284 |
return bigDecimalValue().doubleValue(); |
| 285 |
} |
| 286 |
|
| 287 |
@Override |
| 288 |
public BigInteger bigIntegerValue() { |
| 289 |
BigDecimal bd = bigDecimalValue(); |
| 290 |
if (Math.abs(bd.scale()) <= bigIntegerScaleLimit) { |
| 291 |
return bd.toBigInteger(); |
| 292 |
} |
| 293 |
throw new UnsupportedOperationException( |
| 294 |
JsonMessages.NUMBER_SCALE_LIMIT_EXCEPTION(bd.scale(), bigIntegerScaleLimit)); |
| 295 |
} |
| 296 |
|
| 297 |
@Override |
| 298 |
public BigInteger bigIntegerValueExact() { |
| 299 |
BigDecimal bd = bigDecimalValue(); |
| 300 |
if (Math.abs(bd.scale()) <= bigIntegerScaleLimit) { |
| 301 |
return bd.toBigIntegerExact(); |
| 302 |
} |
| 303 |
throw new UnsupportedOperationException( |
| 304 |
JsonMessages.NUMBER_SCALE_LIMIT_EXCEPTION(bd.scale(), bigIntegerScaleLimit)); |
| 305 |
} |
| 306 |
|
| 307 |
@Override |
| 308 |
public ValueType getValueType() { |
| 309 |
return ValueType.NUMBER; |
| 310 |
} |
| 311 |
|
| 312 |
@Override |
| 313 |
public int hashCode() { |
| 314 |
if (hashCode == 0) { |
| 315 |
hashCode = bigDecimalValue().hashCode(); |
| 316 |
} |
| 317 |
return hashCode; |
| 318 |
} |
| 319 |
|
| 320 |
@Override |
| 321 |
public boolean equals(Object obj) { |
| 322 |
if (this == obj){ |
| 323 |
return true; |
| 324 |
} |
| 325 |
if (!(obj instanceof JsonNumber)) { |
| 326 |
return false; |
| 327 |
} |
| 328 |
JsonNumber other = (JsonNumber)obj; |
| 329 |
return bigDecimalValue().equals(other.bigDecimalValue()); |
| 330 |
} |
| 331 |
|
| 332 |
@Override |
| 333 |
public String toString() { |
| 334 |
return bigDecimalValue().toString(); |
| 335 |
} |
| 336 |
|
| 337 |
} |