1 /*
 2  * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.  Oracle designates this
 8  * particular file as subject to the "Classpath" exception as provided
 9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package oracle.code.json;
27 
28 /**
29  * The interface that represents JSON number.
30  * <p>
31  * A {@code JsonNumber} can be produced by {@link Json#parse(String)}.
32  * Alternatively, {@link #of(double)} and its overload can be used to obtain
33  * a {@code JsonNumber} from a {@code Number}.
34  * When a JSON number is parsed, a {@code JsonNumber} object is created
35  * regardless of its precision or magnitude as long as the syntax is valid.
36  * The parsed string representation is retrieved from {@link #toString()}.
37  *
38  */
39 public sealed interface JsonNumber extends JsonValue permits JsonNumberImpl {
40 
41     /**
42      * {@return the {@code Number} value represented by this
43      * {@code JsonNumber}}
44      *
45      * @implNote The returned value's type is {@code Double} for floating point
46      * numbers. For integer numbers, it is either {@code Integer}, {@code Long},
47      * or {@code Double}. The return value is derived from the respective
48      * {@code Number} subclass {@code valueOf(String)} methods, where the {@code String}
49      * corresponds to the {@link #toString()} of this {@code JsonNumber}.
50      *
51      * @throws NumberFormatException if the string representation of this
52      *          {@code JsonNumber} cannot be converted to a {@code Number}.
53      * @see Double##decimalToBinaryConversion Decimal &harr; Binary Conversion Issues
54      */
55     Number value();
56 
57     /**
58      * {@return the {@code JsonNumber} created from the given
59      * {@code double}}
60      *
61      * @implNote If the given {@code double} is equivalent to {@code +/-infinity}
62      * or {@code NaN}, this method will throw an {@code IllegalArgumentException}.
63      *
64      * @param num the given {@code double}.
65      * @throws IllegalArgumentException if the given {@code num} is out
66      *          of the accepted range.
67      */
68     static JsonNumber of(double num) {
69         // non-integral types
70         if (Double.isNaN(num) || Double.isInfinite(num)) {
71             throw new IllegalArgumentException("Not a valid JSON number");
72         }
73         return new JsonNumberImpl(num);
74     }
75 
76     /**
77      * {@return the {@code JsonNumber} created from the given
78      * {@code long}}
79      *
80      * @param num the given {@code long}.
81      */
82     static JsonNumber of(long num) {
83         // integral types
84         return new JsonNumberImpl(num);
85     }
86 }