[6756] | 1 | /*
|
---|
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
---|
| 3 | *
|
---|
| 4 | * Copyright (c) 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 org.glassfish.json;
|
---|
| 42 |
|
---|
| 43 | import javax.json.JsonNumber;
|
---|
| 44 | import java.math.BigDecimal;
|
---|
| 45 | import java.math.BigInteger;
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
| 48 | * JsonNumber impl. Subclasses provide optimized implementations
|
---|
| 49 | * when backed by int, long, BigDecimal
|
---|
| 50 | *
|
---|
| 51 | * @author Jitendra Kotamraju
|
---|
| 52 | */
|
---|
| 53 | abstract class JsonNumberImpl implements JsonNumber {
|
---|
| 54 |
|
---|
| 55 | static JsonNumber getJsonNumber(int num) {
|
---|
| 56 | return new JsonIntNumber(num);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | static JsonNumber getJsonNumber(long num) {
|
---|
| 60 | return new JsonLongNumber(num);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | static JsonNumber getJsonNumber(BigInteger value) {
|
---|
| 64 | return new JsonBigDecimalNumber(new BigDecimal(value));
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | static JsonNumber getJsonNumber(double value) {
|
---|
| 68 | //bigDecimal = new BigDecimal(value);
|
---|
| 69 | // This is the preferred way to convert double to BigDecimal
|
---|
| 70 | return new JsonBigDecimalNumber(BigDecimal.valueOf(value));
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | static JsonNumber getJsonNumber(BigDecimal value) {
|
---|
| 74 | return new JsonBigDecimalNumber(value);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | // Optimized JsonNumber impl for int numbers.
|
---|
| 78 | private static final class JsonIntNumber extends JsonNumberImpl {
|
---|
| 79 | private final int num;
|
---|
| 80 | private BigDecimal bigDecimal; // assigning it lazily on demand
|
---|
| 81 |
|
---|
| 82 | JsonIntNumber(int num) {
|
---|
| 83 | this.num = num;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | @Override
|
---|
| 87 | public boolean isIntegral() {
|
---|
| 88 | return true;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | @Override
|
---|
| 92 | public int intValue() {
|
---|
| 93 | return num;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | @Override
|
---|
| 97 | public int intValueExact() {
|
---|
| 98 | return num;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | @Override
|
---|
| 102 | public long longValue() {
|
---|
| 103 | return num;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | @Override
|
---|
| 107 | public long longValueExact() {
|
---|
| 108 | return num;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | @Override
|
---|
| 112 | public double doubleValue() {
|
---|
| 113 | return num;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | @Override
|
---|
| 117 | public BigDecimal bigDecimalValue() {
|
---|
| 118 | // reference assignments are atomic. At the most some more temp
|
---|
| 119 | // BigDecimal objects are created
|
---|
| 120 | BigDecimal bd = bigDecimal;
|
---|
| 121 | if (bd == null) {
|
---|
| 122 | bigDecimal = bd = new BigDecimal(num);
|
---|
| 123 | }
|
---|
| 124 | return bd;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | @Override
|
---|
| 128 | public String toString() {
|
---|
| 129 | return Integer.toString(num);
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | // Optimized JsonNumber impl for long numbers.
|
---|
| 134 | private static final class JsonLongNumber extends JsonNumberImpl {
|
---|
| 135 | private final long num;
|
---|
| 136 | private BigDecimal bigDecimal; // assigning it lazily on demand
|
---|
| 137 |
|
---|
| 138 | JsonLongNumber(long num) {
|
---|
| 139 | this.num = num;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | @Override
|
---|
| 143 | public boolean isIntegral() {
|
---|
| 144 | return true;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | @Override
|
---|
| 148 | public long longValue() {
|
---|
| 149 | return num;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | @Override
|
---|
| 153 | public long longValueExact() {
|
---|
| 154 | return num;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | @Override
|
---|
| 158 | public double doubleValue() {
|
---|
| 159 | return num;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | @Override
|
---|
| 163 | public BigDecimal bigDecimalValue() {
|
---|
| 164 | // reference assignments are atomic. At the most some more temp
|
---|
| 165 | // BigDecimal objects are created
|
---|
| 166 | BigDecimal bd = bigDecimal;
|
---|
| 167 | if (bd == null) {
|
---|
| 168 | bigDecimal = bd = new BigDecimal(num);
|
---|
| 169 | }
|
---|
| 170 | return bd;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | @Override
|
---|
| 174 | public String toString() {
|
---|
| 175 | return Long.toString(num);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | // JsonNumber impl using BigDecimal numbers.
|
---|
| 181 | private static final class JsonBigDecimalNumber extends JsonNumberImpl {
|
---|
| 182 | private final BigDecimal bigDecimal;
|
---|
| 183 |
|
---|
| 184 | JsonBigDecimalNumber(BigDecimal value) {
|
---|
| 185 | this.bigDecimal = value;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | @Override
|
---|
| 189 | public BigDecimal bigDecimalValue() {
|
---|
| 190 | return bigDecimal;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | @Override
|
---|
| 196 | public boolean isIntegral() {
|
---|
| 197 | return bigDecimalValue().scale() == 0;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | @Override
|
---|
| 201 | public int intValue() {
|
---|
| 202 | return bigDecimalValue().intValue();
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | @Override
|
---|
| 206 | public int intValueExact() {
|
---|
| 207 | return bigDecimalValue().intValueExact();
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | @Override
|
---|
| 211 | public long longValue() {
|
---|
| 212 | return bigDecimalValue().longValue();
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | @Override
|
---|
| 216 | public long longValueExact() {
|
---|
| 217 | return bigDecimalValue().longValueExact();
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | @Override
|
---|
| 221 | public double doubleValue() {
|
---|
| 222 | return bigDecimalValue().doubleValue();
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | @Override
|
---|
| 226 | public BigInteger bigIntegerValue() {
|
---|
| 227 | return bigDecimalValue().toBigInteger();
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | @Override
|
---|
| 231 | public BigInteger bigIntegerValueExact() {
|
---|
| 232 | return bigDecimalValue().toBigIntegerExact();
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | @Override
|
---|
| 236 | public ValueType getValueType() {
|
---|
| 237 | return ValueType.NUMBER;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | @Override
|
---|
| 241 | public int hashCode() {
|
---|
| 242 | return bigDecimalValue().hashCode();
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | @Override
|
---|
| 246 | public boolean equals(Object obj) {
|
---|
| 247 | if (!(obj instanceof JsonNumber)) {
|
---|
| 248 | return false;
|
---|
| 249 | }
|
---|
| 250 | JsonNumber other = (JsonNumber)obj;
|
---|
| 251 | return bigDecimalValue().equals(other.bigDecimalValue());
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | @Override
|
---|
| 255 | public String toString() {
|
---|
| 256 | return bigDecimalValue().toString();
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | }
|
---|
| 260 |
|
---|