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.impl;
 27 
 28 import oracle.code.json.JsonNumber;
 29 
 30 import java.math.BigDecimal;
 31 import java.math.BigInteger;
 32 import java.util.Locale;
 33 
 34 /**
 35  * JsonNumber implementation class
 36  */
 37 public final class JsonNumberImpl implements JsonNumber {
 38 
 39     private final char[] doc;
 40     private final int startOffset;
 41     private final int endOffset;
 42     private Number theNumber;
 43 
 44     public JsonNumberImpl(Number num) {
 45         if (num == null ||
 46             num instanceof Double d && (d.isNaN() || d.isInfinite())) {
 47             throw new IllegalArgumentException("Not a valid JSON number");
 48         }
 49         theNumber = num;
 50         // unused
 51         startOffset = -1;
 52         endOffset = -1;
 53         doc = null;
 54     }
 55 
 56     public JsonNumberImpl(char[] doc, int start, int end) {
 57         this.doc = doc;
 58         startOffset = start;
 59         endOffset = end;
 60     }
 61 
 62     @Override
 63     public Number toNumber() {
 64         var n = theNumber;
 65         if (n == null) {
 66             n = theNumber = computeNumber();
 67         }
 68         return n;
 69     }
 70 
 71     private Number computeNumber() {
 72         var str = toString();
 73         // Check if integral (Java literal format)
 74         boolean integerOnly = true;
 75         for (int index = 0; index < str.length(); index++) {
 76             char c = str.charAt(index);
 77             if (c == '.' || c == 'e' || c == 'E') {
 78                 integerOnly = false;
 79                 break;
 80             }
 81         }
 82         if (integerOnly) {
 83             try {
 84                 return Long.parseLong(str);
 85             } catch (NumberFormatException _) {
 86                 return new BigInteger(str);
 87             }
 88         } else {
 89             var db = Double.parseDouble(str);
 90             if (Double.isInfinite(db)) {
 91                 return toBigDecimal();
 92             } else {
 93                 return db;
 94             }
 95         }
 96     }
 97 
 98     @Override
 99     public BigDecimal toBigDecimal() {
100         return new BigDecimal(toString());
101     }
102 
103     @Override
104     public String toString() {
105         return new String(doc, startOffset, endOffset - startOffset);
106     }
107 
108     @Override
109     public boolean equals(Object o) {
110         return o instanceof JsonNumber ojn &&
111                 toString().compareToIgnoreCase(ojn.toString()) == 0;
112     }
113 
114     @Override
115     public int hashCode() {
116         return toString().toLowerCase(Locale.ROOT).hashCode();
117     }
118 }