1 package io.jawk.jrt;
2
3 /*-
4 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5 * Jawk
6 * ჻჻჻჻჻჻
7 * Copyright (C) 2006 - 2026 MetricsHub
8 * ჻჻჻჻჻჻
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Lesser Public License for more details.
18 *
19 * You should have received a copy of the GNU General Lesser Public
20 * License along with this program. If not, see
21 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22 * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
23 */
24
25 import java.io.Serializable;
26 import java.math.BigDecimal;
27
28 /**
29 * An input-derived scalar: text that may also act as a number, following
30 * POSIX "numeric string" (strnum) semantics. Input fields, {@code getline}
31 * results, {@code split()} pieces, and similar input-originated values are
32 * represented as instances of this class so comparisons and gawk's
33 * {@code typeof()} can distinguish them from plain string constants.
34 * <p>
35 * Instances are serializable so they can be retained in persistent memory.
36 */
37 public final class StrNum implements Serializable {
38
39 private static final long serialVersionUID = 1L;
40
41 private final String value;
42 private final char decimalSeparator;
43 // Parse caches, recomputed on demand after deserialization
44 private transient Boolean numeric;
45 private transient Double numericValue;
46
47 StrNum(String value) {
48 this(value, '.');
49 }
50
51 StrNum(String value, char decimalSeparator) {
52 this.value = value == null ? "" : value;
53 this.decimalSeparator = decimalSeparator;
54 }
55
56 /**
57 * Returns whether this scalar's text parses as an AWK number, making it a
58 * strnum.
59 *
60 * @return {@code true} when the text is a valid AWK number
61 */
62 public boolean isNumber() {
63 if (numeric == null) {
64 numeric = Boolean.valueOf(JRT.isParseableNumber(value, decimalSeparator));
65 }
66 return numeric.booleanValue();
67 }
68
69 /**
70 * Returns this scalar's numeric value.
71 *
72 * @return the parsed numeric value
73 */
74 public double doubleValue() {
75 if (numericValue == null) {
76 numericValue = Double.valueOf(parseDoubleValue());
77 }
78 return numericValue.doubleValue();
79 }
80
81 private double parseDoubleValue() {
82 // Recognition ignores leading and trailing blanks, so parsing must
83 // strip them: the two move together.
84 String normalizedValue = JRT.normalizeNumberForComparison(JRT.trimWhitespace(value), decimalSeparator);
85 try {
86 return Double.parseDouble(normalizedValue);
87 } catch (NumberFormatException nfe) {
88 return new BigDecimal(normalizedValue).doubleValue();
89 }
90 }
91
92 @Override
93 public String toString() {
94 return value;
95 }
96 }