Debian Codegraph @

src/dr/inference/trace/NumberUtils.java

raw symbols SCIP
1 package dr.inference.trace;
2
3 /**
4 * Detect number type from a string.
5 * <code>isNumber</code> is modified from org.apache.commons.lang3.math.NumberUtils
6 * to handle NaN and Inf.
7 *
8 * @author Walter Xie
9 */
10 public class NumberUtils {
11
12 /**
13 * <p>Checks whether the <code>String</code> does not contain decimal point.</p>
14 * <p>
15 * <p><code>Null</code> and empty String will return <code>false</code>.</p>
16 * <p><code>Double.NaN</code>, <code>Double.NEGATIVE_INFINITY</code>,
17 * and <code>Double.POSITIVE_INFINITY</code> will return <code>true</code>.</p>
18 *
19 * @param str the <code>String</code> to check
20 * @return <code>true</code> if str contains only Unicode numeric
21 */
22 public static boolean hasDecimalPoint(String str) {
23 if (str == null || str.length() == 0)
24 return false;
25 if (str.equalsIgnoreCase(Double.toString(Double.NaN)) ||
26 str.equalsIgnoreCase(Double.toString(Double.NEGATIVE_INFINITY)) ||
27 str.equalsIgnoreCase(Double.toString(Double.POSITIVE_INFINITY)))
28 return true;
29 return str.contains(".");
30 }
31
32 /**
33 * <p>Checks whether the String a valid Java number.</p>
34 * <p>
35 * <p>Valid numbers include hexadecimal marked with the <code>0x</code>
36 * qualifier, scientific notation and numbers marked with a type
37 * qualifier (e.g. 123L).</p>
38 * <p>
39 * <p><code>Null</code> and empty String will return <code>false</code>.</p>
40 * <p><code>Double.NaN</code>, <code>Double.NEGATIVE_INFINITY</code>,
41 * and <code>Double.POSITIVE_INFINITY</code> will return <code>true</code>.</p>
42 *
43 * @param str the <code>String</code> to check
44 * @return <code>true</code> if the string is a correctly formatted number
45 */
46 public static boolean isNumber(String str) {
47 if (str == null || str.length() == 0) {
48 return false;
49 }
50 // modified
51 if (str.equalsIgnoreCase(Double.toString(Double.NaN)) ||
52 str.equalsIgnoreCase(Double.toString(Double.NEGATIVE_INFINITY)) ||
53 str.equalsIgnoreCase(Double.toString(Double.POSITIVE_INFINITY)))
54 return true;
55
56 char[] chars = str.toCharArray();
57 int sz = chars.length;
58 boolean hasExp = false;
59 boolean hasDecPoint = false;
60 boolean allowSigns = false;
61 boolean foundDigit = false;
62 // deal with any possible sign up front
63 int start = (chars[0] == '-') ? 1 : 0;
64 if (sz > start + 1 && chars[start] == '0' && chars[start + 1] == 'x') {
65 int i = start + 2;
66 if (i == sz) {
67 return false; // str == "0x"
68 }
69 // checking hex (it can't be anything else)
70 for (; i < chars.length; i++) {
71 if ((chars[i] < '0' || chars[i] > '9')
72 && (chars[i] < 'a' || chars[i] > 'f')
73 && (chars[i] < 'A' || chars[i] > 'F')) {
74 return false;
75 }
76 }
77 return true;
78 }
79 sz--; // don't want to loop to the last char, check it afterwords
80 // for type qualifiers
81 int i = start;
82 // loop to the next to last char or to the last char if we need another digit to
83 // make a valid number (e.g. chars[0..5] = "1234E")
84 while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) {
85 if (chars[i] >= '0' && chars[i] <= '9') {
86 foundDigit = true;
87 allowSigns = false;
88 } else if (chars[i] == '.') {
89 if (hasDecPoint || hasExp) {
90 // two decimal points or dec in exponent
91 return false;
92 }
93 hasDecPoint = true;
94 } else if (chars[i] == 'e' || chars[i] == 'E') {
95 // we've already taken care of hex.
96 if (hasExp) {
97 // two E's
98 return false;
99 }
100 if (!foundDigit) {
101 return false;
102 }
103 hasExp = true;
104 allowSigns = true;
105 } else if (chars[i] == '+' || chars[i] == '-') {
106 if (!allowSigns) {
107 return false;
108 }
109 allowSigns = false;
110 foundDigit = false; // we need a digit after the E
111 } else {
112 return false;
113 }
114 i++;
115 }
116 if (i < chars.length) {
117 if (chars[i] >= '0' && chars[i] <= '9') {
118 // no type qualifier, OK
119 return true;
120 }
121 if (chars[i] == 'e' || chars[i] == 'E') {
122 // can't have an E at the last byte
123 return false;
124 }
125 if (chars[i] == '.') {
126 if (hasDecPoint || hasExp) {
127 // two decimal points or dec in exponent
128 return false;
129 }
130 // single trailing decimal point after non-exponent is ok
131 return foundDigit;
132 }
133 if (!allowSigns
134 && (chars[i] == 'd'
135 || chars[i] == 'D'
136 || chars[i] == 'f'
137 || chars[i] == 'F')) {
138 return foundDigit;
139 }
140 if (chars[i] == 'l'
141 || chars[i] == 'L') {
142 // not allowing L with an exponent or decimal point
143 return foundDigit && !hasExp && !hasDecPoint;
144 }
145 // last character is illegal
146 return false;
147 }
148 // allowSigns is true iff the val ends in 'E'
149 // found digit it to make sure weird stuff like '.' and '1E-' doesn't pass
150 return !allowSigns && foundDigit;
151 }
152
153 }