| 1 | /* |
| 2 | * Copyright (c) 2011, 2021 Oracle and/or its affiliates. All rights reserved. |
| 3 | * |
| 4 | * This program and the accompanying materials are made available under the |
| 5 | * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | * http://www.eclipse.org/legal/epl-2.0. |
| 7 | * |
| 8 | * This Source Code may also be made available under the following Secondary |
| 9 | * Licenses when the conditions for such availability set forth in the |
| 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | * version 2 with the GNU Classpath Exception, which is available at |
| 12 | * https://www.gnu.org/software/classpath/license.html. |
| 13 | * |
| 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | */ |
| 16 | |
| 17 | package jakarta.json; |
| 18 | |
| 19 | import java.math.BigDecimal; |
| 20 | import java.math.BigInteger; |
| 21 | |
| 22 | /** |
| 23 | * An immutable JSON number value. |
| 24 | * |
| 25 | * <p> |
| 26 | * Implementations may use a {@link BigDecimal} object to store the numeric |
| 27 | * value internally. |
| 28 | * The {@code BigDecimal} object can be constructed from the following types: |
| 29 | * <code>int</code> {@link BigDecimal#BigDecimal(int)}, |
| 30 | * <code>long</code> {@link BigDecimal#BigDecimal(long)}, |
| 31 | * <code>BigInteger</code> {@link BigDecimal#BigDecimal(BigInteger)}, |
| 32 | * <code>double</code> {@link BigDecimal#valueOf(double)}, and |
| 33 | * <code>String</code> {@link BigDecimal#BigDecimal(String)}. |
| 34 | * Some of the method semantics in this class are defined using the |
| 35 | * {@code BigDecimal} semantics. |
| 36 | */ |
| 37 | public interface JsonNumber extends JsonValue { |
| 38 | |
| 39 | /** |
| 40 | * Returns true if this JSON number is a integral number. This method |
| 41 | * semantics are defined using {@code bigDecimalValue().scale()}. If the |
| 42 | * scale is zero, then it is considered integral type. This integral type |
| 43 | * information can be used to invoke an appropriate accessor method to |
| 44 | * obtain a numeric value as in the following example: |
| 45 | * |
| 46 | * <pre> |
| 47 | * <code> |
| 48 | * JsonNumber num = ... |
| 49 | * if (num.isIntegral()) { |
| 50 | * num.longValue(); // or other methods to get integral value |
| 51 | * } else { |
| 52 | * num.doubleValue(); // or other methods to get decimal number value |
| 53 | * } |
| 54 | * </code> |
| 55 | * </pre> |
| 56 | * |
| 57 | * @return true if this number is a integral number, otherwise false |
| 58 | */ |
| 59 | boolean isIntegral(); |
| 60 | |
| 61 | /** |
| 62 | * Returns this JSON number as an {@code int}. Note that this conversion |
| 63 | * can lose information about the overall magnitude and precision of the |
| 64 | * number value as well as return a result with the opposite sign. |
| 65 | * |
| 66 | * @return an {@code int} representation of the JSON number |
| 67 | * @see java.math.BigDecimal#intValue() |
| 68 | */ |
| 69 | int intValue(); |
| 70 | |
| 71 | /** |
| 72 | * Returns this JSON number as an {@code int}. |
| 73 | * |
| 74 | * @return an {@code int} representation of the JSON number |
| 75 | * @throws ArithmeticException if the number has a nonzero fractional |
| 76 | * part or if it does not fit in an {@code int} |
| 77 | * @see java.math.BigDecimal#intValueExact() |
| 78 | */ |
| 79 | int intValueExact(); |
| 80 | |
| 81 | /** |
| 82 | * Returns this JSON number as a {@code long}. Note that this conversion |
| 83 | * can lose information about the overall magnitude and precision of the |
| 84 | * number value as well as return a result with the opposite sign. |
| 85 | * |
| 86 | * @return a {@code long} representation of the JSON number. |
| 87 | * @see java.math.BigDecimal#longValue() |
| 88 | */ |
| 89 | long longValue(); |
| 90 | |
| 91 | /** |
| 92 | * Returns this JSON number as a {@code long}. |
| 93 | * |
| 94 | * @return a {@code long} representation of the JSON number |
| 95 | * @throws ArithmeticException if the number has a non-zero fractional |
| 96 | * part or if it does not fit in a {@code long} |
| 97 | * @see java.math.BigDecimal#longValueExact() |
| 98 | */ |
| 99 | long longValueExact(); |
| 100 | |
| 101 | /** |
| 102 | * Returns this JSON number as a {@link BigInteger} object. This is a |
| 103 | * a convenience method for {@code bigDecimalValue().toBigInteger()}. |
| 104 | * Note that this conversion can lose information about the overall |
| 105 | * magnitude and precision of the number value as well as return a result |
| 106 | * with the opposite sign. |
| 107 | * |
| 108 | * @return a {@code BigInteger} representation of the JSON number. |
| 109 | * @see java.math.BigDecimal#toBigInteger() |
| 110 | */ |
| 111 | BigInteger bigIntegerValue(); |
| 112 | |
| 113 | /** |
| 114 | * Returns this JSON number as a {@link BigInteger} object. This is a |
| 115 | * convenience method for {@code bigDecimalValue().toBigIntegerExact()}. |
| 116 | * |
| 117 | * @return a {@link BigInteger} representation of the JSON number |
| 118 | * @throws ArithmeticException if the number has a nonzero fractional part |
| 119 | * @see java.math.BigDecimal#toBigIntegerExact() |
| 120 | */ |
| 121 | BigInteger bigIntegerValueExact(); |
| 122 | |
| 123 | /** |
| 124 | * Returns this JSON number as a {@code double}. This is a |
| 125 | * a convenience method for {@code bigDecimalValue().doubleValue()}. |
| 126 | * Note that this conversion can lose information about the overall |
| 127 | * magnitude and precision of the number value as well as return a result |
| 128 | * with the opposite sign. |
| 129 | * |
| 130 | * @return a {@code double} representation of the JSON number |
| 131 | * @see java.math.BigDecimal#doubleValue() |
| 132 | */ |
| 133 | double doubleValue(); |
| 134 | |
| 135 | /** |
| 136 | * Returns this JSON number as a {@link BigDecimal} object. |
| 137 | * |
| 138 | * @return a {@link BigDecimal} representation of the JSON number |
| 139 | */ |
| 140 | BigDecimal bigDecimalValue(); |
| 141 | |
| 142 | /** |
| 143 | * Returns this JSON number as a {@link Number} object. |
| 144 | * |
| 145 | * @return a {@link Number} representation of the JSON number |
| 146 | * |
| 147 | * @since 1.1 |
| 148 | */ |
| 149 | default Number numberValue() { |
| 150 | throw new UnsupportedOperationException(); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Returns a JSON text representation of the JSON number. The |
| 155 | * representation is equivalent to {@link BigDecimal#toString()}. |
| 156 | * |
| 157 | * @return JSON text representation of the number |
| 158 | */ |
| 159 | @Override |
| 160 | String toString(); |
| 161 | |
| 162 | /** |
| 163 | * Compares the specified object with this {@code JsonNumber} object for |
| 164 | * equality. Returns {@code true} if and only if the type of the specified |
| 165 | * object is also {@code JsonNumber} and their {@link #bigDecimalValue()} |
| 166 | * objects are <i>equal</i> |
| 167 | * |
| 168 | * @param obj the object to be compared for equality with |
| 169 | * this {@code JsonNumber} |
| 170 | * @return {@code true} if the specified object is equal to this |
| 171 | * {@code JsonNumber} |
| 172 | */ |
| 173 | @Override |
| 174 | boolean equals(Object obj); |
| 175 | |
| 176 | /** |
| 177 | * Returns the hash code value for this {@code JsonNumber} object. The |
| 178 | * hash code of a {@code JsonNumber} object is defined as the hash code of |
| 179 | * its {@link #bigDecimalValue()} object. |
| 180 | * |
| 181 | * @return the hash code value for this {@code JsonNumber} object |
| 182 | */ |
| 183 | @Override |
| 184 | int hashCode(); |
| 185 | |
| 186 | } |