[6756] | 1 | /*
|
---|
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
---|
| 3 | *
|
---|
| 4 | * Copyright (c) 2011-2013 Oracle and/or its affiliates. All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * The contents of this file are subject to the terms of either the GNU
|
---|
| 7 | * General Public License Version 2 only ("GPL") or the Common Development
|
---|
| 8 | * and Distribution License("CDDL") (collectively, the "License"). You
|
---|
| 9 | * may not use this file except in compliance with the License. You can
|
---|
| 10 | * obtain a copy of the License at
|
---|
| 11 | * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
|
---|
| 12 | * or packager/legal/LICENSE.txt. See the License for the specific
|
---|
| 13 | * language governing permissions and limitations under the License.
|
---|
| 14 | *
|
---|
| 15 | * When distributing the software, include this License Header Notice in each
|
---|
| 16 | * file and include the License file at packager/legal/LICENSE.txt.
|
---|
| 17 | *
|
---|
| 18 | * GPL Classpath Exception:
|
---|
| 19 | * Oracle designates this particular file as subject to the "Classpath"
|
---|
| 20 | * exception as provided by Oracle in the GPL Version 2 section of the License
|
---|
| 21 | * file that accompanied this code.
|
---|
| 22 | *
|
---|
| 23 | * Modifications:
|
---|
| 24 | * If applicable, add the following below the License Header, with the fields
|
---|
| 25 | * enclosed by brackets [] replaced by your own identifying information:
|
---|
| 26 | * "Portions Copyright [year] [name of copyright owner]"
|
---|
| 27 | *
|
---|
| 28 | * Contributor(s):
|
---|
| 29 | * If you wish your version of this file to be governed by only the CDDL or
|
---|
| 30 | * only the GPL Version 2, indicate your decision by adding "[Contributor]
|
---|
| 31 | * elects to include this software in this distribution under the [CDDL or GPL
|
---|
| 32 | * Version 2] license." If you don't indicate a single choice of license, a
|
---|
| 33 | * recipient has the option to distribute your version of this file under
|
---|
| 34 | * either the CDDL, the GPL Version 2 or to extend the choice of license to
|
---|
| 35 | * its licensees as provided above. However, if you add GPL Version 2 code
|
---|
| 36 | * and therefore, elected the GPL Version 2 license, then the option applies
|
---|
| 37 | * only if the new code is made subject to such option by the copyright
|
---|
| 38 | * holder.
|
---|
| 39 | */
|
---|
| 40 |
|
---|
| 41 | package javax.json;
|
---|
| 42 |
|
---|
| 43 | import java.math.BigDecimal;
|
---|
| 44 | import java.math.BigInteger;
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * An immutable JSON number value.
|
---|
| 48 | *
|
---|
| 49 | * <p>
|
---|
| 50 | * Implementations may use a {@link BigDecimal} object to store the numeric
|
---|
| 51 | * value internally.
|
---|
| 52 | * The {@code BigDecimal} object can be constructed from the following types:
|
---|
| 53 | * {@link BigDecimal#BigDecimal(int) <code>int</code>},
|
---|
| 54 | * {@link BigDecimal#BigDecimal(long) <code>long</code>},
|
---|
| 55 | * {@link BigDecimal#BigDecimal(BigInteger) <code>BigInteger</code>},
|
---|
| 56 | * {@link BigDecimal#valueOf(double) <code>double</code>}, and
|
---|
| 57 | * {@link BigDecimal#BigDecimal(String) <code>String</code>}.
|
---|
| 58 | * Some of the method semantics in this class are defined using the
|
---|
| 59 | * {@code BigDecimal} semantics.
|
---|
| 60 | *
|
---|
| 61 | * @author Jitendra Kotamraju
|
---|
| 62 | */
|
---|
| 63 | public interface JsonNumber extends JsonValue {
|
---|
| 64 |
|
---|
| 65 | /**
|
---|
| 66 | * Returns true if this JSON number is a integral number. This method
|
---|
| 67 | * semantics are defined using {@code bigDecimalValue().scale()}. If the
|
---|
| 68 | * scale is zero, then it is considered integral type. This integral type
|
---|
| 69 | * information can be used to invoke an appropriate accessor method to
|
---|
| 70 | * obtain a numeric value as in the following example:
|
---|
| 71 | *
|
---|
| 72 | * <pre>
|
---|
| 73 | * <code>
|
---|
| 74 | * JsonNumber num = ...
|
---|
| 75 | * if (num.isIntegral()) {
|
---|
| 76 | * num.longValue(); // or other methods to get integral value
|
---|
| 77 | * } else {
|
---|
| 78 | * num.doubleValue(); // or other methods to get decimal number value
|
---|
| 79 | * }
|
---|
| 80 | * </code>
|
---|
| 81 | * </pre>
|
---|
| 82 | *
|
---|
| 83 | * @return true if this number is a integral number, otherwise false
|
---|
| 84 | */
|
---|
| 85 | boolean isIntegral();
|
---|
| 86 |
|
---|
| 87 | /**
|
---|
| 88 | * Returns this JSON number as an {@code int}. Note that this conversion
|
---|
| 89 | * can lose information about the overall magnitude and precision of the
|
---|
| 90 | * number value as well as return a result with the opposite sign.
|
---|
| 91 | *
|
---|
| 92 | * @return an {@code int} representation of the JSON number
|
---|
| 93 | * @see java.math.BigDecimal#intValue()
|
---|
| 94 | */
|
---|
| 95 | int intValue();
|
---|
| 96 |
|
---|
| 97 | /**
|
---|
| 98 | * Returns this JSON number as an {@code int}.
|
---|
| 99 | *
|
---|
| 100 | * @return an {@code int} representation of the JSON number
|
---|
| 101 | * @throws ArithmeticException if the number has a nonzero fractional
|
---|
| 102 | * part or if it does not fit in an {@code int}
|
---|
| 103 | * @see java.math.BigDecimal#intValueExact()
|
---|
| 104 | */
|
---|
| 105 | int intValueExact();
|
---|
| 106 |
|
---|
| 107 | /**
|
---|
| 108 | * Returns this JSON number as a {@code long}. Note that this conversion
|
---|
| 109 | * can lose information about the overall magnitude and precision of the
|
---|
| 110 | * number value as well as return a result with the opposite sign.
|
---|
| 111 | *
|
---|
| 112 | * @return a {@code long} representation of the JSON number.
|
---|
| 113 | * @see java.math.BigDecimal#longValue()
|
---|
| 114 | */
|
---|
| 115 | long longValue();
|
---|
| 116 |
|
---|
| 117 | /**
|
---|
| 118 | * Returns this JSON number as a {@code long}.
|
---|
| 119 | *
|
---|
| 120 | * @return a {@code long} representation of the JSON number
|
---|
| 121 | * @throws ArithmeticException if the number has a non-zero fractional
|
---|
| 122 | * part or if it does not fit in a {@code long}
|
---|
| 123 | * @see java.math.BigDecimal#longValueExact()
|
---|
| 124 | */
|
---|
| 125 | long longValueExact();
|
---|
| 126 |
|
---|
| 127 | /**
|
---|
| 128 | * Returns this JSON number as a {@link BigInteger} object. This is a
|
---|
| 129 | * a convenience method for {@code bigDecimalValue().toBigInteger()}.
|
---|
| 130 | * Note that this conversion can lose information about the overall
|
---|
| 131 | * magnitude and precision of the number value as well as return a result
|
---|
| 132 | * with the opposite sign.
|
---|
| 133 | *
|
---|
| 134 | * @return a {@code BigInteger} representation of the JSON number.
|
---|
| 135 | * @see java.math.BigDecimal#toBigInteger()
|
---|
| 136 | */
|
---|
| 137 | BigInteger bigIntegerValue();
|
---|
| 138 |
|
---|
| 139 | /**
|
---|
| 140 | * Returns this JSON number as a {@link BigDecimal} object. This is a
|
---|
| 141 | * convenience method for {@code bigDecimalValue().toBigIntegerExact()}.
|
---|
| 142 | *
|
---|
| 143 | * @return a {@link BigInteger} representation of the JSON number
|
---|
| 144 | * @throws ArithmeticException if the number has a nonzero fractional part
|
---|
| 145 | * @see java.math.BigDecimal#toBigIntegerExact()
|
---|
| 146 | */
|
---|
| 147 | BigInteger bigIntegerValueExact();
|
---|
| 148 |
|
---|
| 149 | /**
|
---|
| 150 | * Returns this JSON number as a {@code double}. This is a
|
---|
| 151 | * a convenience method for {@code bigDecimalValue().doubleValue()}.
|
---|
| 152 | * Note that this conversion can lose information about the overall
|
---|
| 153 | * magnitude and precision of the number value as well as return a result
|
---|
| 154 | * with the opposite sign.
|
---|
| 155 | *
|
---|
| 156 | * @return a {@code double} representation of the JSON number
|
---|
| 157 | * @see java.math.BigDecimal#doubleValue()
|
---|
| 158 | */
|
---|
| 159 | double doubleValue();
|
---|
| 160 |
|
---|
| 161 | /**
|
---|
| 162 | * Returns this JSON number as a {@link BigDecimal} object.
|
---|
| 163 | *
|
---|
| 164 | * @return a {@link BigDecimal} representation of the JSON number
|
---|
| 165 | */
|
---|
| 166 | BigDecimal bigDecimalValue();
|
---|
| 167 |
|
---|
| 168 | /**
|
---|
| 169 | * Returns a JSON text representation of the JSON number. The
|
---|
| 170 | * representation is equivalent to {@link BigDecimal#toString()}.
|
---|
| 171 | *
|
---|
| 172 | * @return JSON text representation of the number
|
---|
| 173 | */
|
---|
| 174 | @Override
|
---|
| 175 | String toString();
|
---|
| 176 |
|
---|
| 177 | /**
|
---|
| 178 | * Compares the specified object with this {@code JsonNumber} object for
|
---|
| 179 | * equality. Returns {@code true} if and only if the type of the specified
|
---|
| 180 | * object is also {@code JsonNumber} and their {@link #bigDecimalValue()}
|
---|
| 181 | * objects are <i>equal</i>
|
---|
| 182 | *
|
---|
| 183 | * @param obj the object to be compared for equality with
|
---|
| 184 | * this {@code JsonNumber}
|
---|
| 185 | * @return {@code true} if the specified object is equal to this
|
---|
| 186 | * {@code JsonNumber}
|
---|
| 187 | */
|
---|
| 188 | @Override
|
---|
| 189 | boolean equals(Object obj);
|
---|
| 190 |
|
---|
| 191 | /**
|
---|
| 192 | * Returns the hash code value for this {@code JsonNumber} object. The
|
---|
| 193 | * hash code of a {@code JsonNumber} object is defined as the hash code of
|
---|
| 194 | * its {@link #bigDecimalValue()} object.
|
---|
| 195 | *
|
---|
| 196 | * @return the hash code value for this {@code JsonNumber} object
|
---|
| 197 | */
|
---|
| 198 | @Override
|
---|
| 199 | int hashCode();
|
---|
| 200 |
|
---|
| 201 | }
|
---|