1 | /*
|
---|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
---|
3 | *
|
---|
4 | * Copyright (c) 2013-2017 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://oss.oracle.com/licenses/CDDL+GPL-1.1
|
---|
12 | * or 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 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 Number numberValue() {
|
---|
129 | return num;
|
---|
130 | }
|
---|
131 |
|
---|
132 | @Override
|
---|
133 | public String toString() {
|
---|
134 | return Integer.toString(num);
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | // Optimized JsonNumber impl for long numbers.
|
---|
139 | private static final class JsonLongNumber extends JsonNumberImpl {
|
---|
140 | private final long num;
|
---|
141 | private BigDecimal bigDecimal; // assigning it lazily on demand
|
---|
142 |
|
---|
143 | JsonLongNumber(long num) {
|
---|
144 | this.num = num;
|
---|
145 | }
|
---|
146 |
|
---|
147 | @Override
|
---|
148 | public boolean isIntegral() {
|
---|
149 | return true;
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Override
|
---|
153 | public int intValue() {
|
---|
154 | return (int) num;
|
---|
155 | }
|
---|
156 |
|
---|
157 | @Override
|
---|
158 | public int intValueExact() {
|
---|
159 | return Math.toIntExact(num);
|
---|
160 | }
|
---|
161 |
|
---|
162 | @Override
|
---|
163 | public long longValue() {
|
---|
164 | return num;
|
---|
165 | }
|
---|
166 |
|
---|
167 | @Override
|
---|
168 | public long longValueExact() {
|
---|
169 | return num;
|
---|
170 | }
|
---|
171 |
|
---|
172 | @Override
|
---|
173 | public double doubleValue() {
|
---|
174 | return num;
|
---|
175 | }
|
---|
176 |
|
---|
177 | @Override
|
---|
178 | public BigDecimal bigDecimalValue() {
|
---|
179 | // reference assignments are atomic. At the most some more temp
|
---|
180 | // BigDecimal objects are created
|
---|
181 | BigDecimal bd = bigDecimal;
|
---|
182 | if (bd == null) {
|
---|
183 | bigDecimal = bd = new BigDecimal(num);
|
---|
184 | }
|
---|
185 | return bd;
|
---|
186 | }
|
---|
187 |
|
---|
188 | @Override
|
---|
189 | public Number numberValue() {
|
---|
190 | return num;
|
---|
191 | }
|
---|
192 |
|
---|
193 | @Override
|
---|
194 | public String toString() {
|
---|
195 | return Long.toString(num);
|
---|
196 | }
|
---|
197 |
|
---|
198 | }
|
---|
199 |
|
---|
200 | // JsonNumber impl using BigDecimal numbers.
|
---|
201 | private static final class JsonBigDecimalNumber extends JsonNumberImpl {
|
---|
202 | private final BigDecimal bigDecimal;
|
---|
203 |
|
---|
204 | JsonBigDecimalNumber(BigDecimal value) {
|
---|
205 | this.bigDecimal = value;
|
---|
206 | }
|
---|
207 |
|
---|
208 | @Override
|
---|
209 | public BigDecimal bigDecimalValue() {
|
---|
210 | return bigDecimal;
|
---|
211 | }
|
---|
212 |
|
---|
213 | @Override
|
---|
214 | public Number numberValue() {
|
---|
215 | return bigDecimalValue();
|
---|
216 | }
|
---|
217 |
|
---|
218 | }
|
---|
219 |
|
---|
220 | @Override
|
---|
221 | public boolean isIntegral() {
|
---|
222 | return bigDecimalValue().scale() == 0;
|
---|
223 | }
|
---|
224 |
|
---|
225 | @Override
|
---|
226 | public int intValue() {
|
---|
227 | return bigDecimalValue().intValue();
|
---|
228 | }
|
---|
229 |
|
---|
230 | @Override
|
---|
231 | public int intValueExact() {
|
---|
232 | return bigDecimalValue().intValueExact();
|
---|
233 | }
|
---|
234 |
|
---|
235 | @Override
|
---|
236 | public long longValue() {
|
---|
237 | return bigDecimalValue().longValue();
|
---|
238 | }
|
---|
239 |
|
---|
240 | @Override
|
---|
241 | public long longValueExact() {
|
---|
242 | return bigDecimalValue().longValueExact();
|
---|
243 | }
|
---|
244 |
|
---|
245 | @Override
|
---|
246 | public double doubleValue() {
|
---|
247 | return bigDecimalValue().doubleValue();
|
---|
248 | }
|
---|
249 |
|
---|
250 | @Override
|
---|
251 | public BigInteger bigIntegerValue() {
|
---|
252 | return bigDecimalValue().toBigInteger();
|
---|
253 | }
|
---|
254 |
|
---|
255 | @Override
|
---|
256 | public BigInteger bigIntegerValueExact() {
|
---|
257 | return bigDecimalValue().toBigIntegerExact();
|
---|
258 | }
|
---|
259 |
|
---|
260 | @Override
|
---|
261 | public ValueType getValueType() {
|
---|
262 | return ValueType.NUMBER;
|
---|
263 | }
|
---|
264 |
|
---|
265 | @Override
|
---|
266 | public int hashCode() {
|
---|
267 | return bigDecimalValue().hashCode();
|
---|
268 | }
|
---|
269 |
|
---|
270 | @Override
|
---|
271 | public boolean equals(Object obj) {
|
---|
272 | if (this == obj){
|
---|
273 | return true;
|
---|
274 | }
|
---|
275 | if (!(obj instanceof JsonNumber)) {
|
---|
276 | return false;
|
---|
277 | }
|
---|
278 | JsonNumber other = (JsonNumber)obj;
|
---|
279 | return bigDecimalValue().equals(other.bigDecimalValue());
|
---|
280 | }
|
---|
281 |
|
---|
282 | @Override
|
---|
283 | public String toString() {
|
---|
284 | return bigDecimalValue().toString();
|
---|
285 | }
|
---|
286 |
|
---|
287 | }
|
---|
288 |
|
---|