1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.metricshub.wbem.sblim.cimclient.internal.uri;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 import java.util.regex.Matcher;
44 import java.util.regex.Pattern;
45 import org.metricshub.wbem.sblim.cimclient.internal.util.MOF;
46 import org.metricshub.wbem.sblim.cimclient.internal.util.Util;
47 import org.metricshub.wbem.sblim.cimclient.internal.util.WBEMConfiguration;
48
49
50
51
52 public class RealValue extends Value {
53 private double iValue;
54
55 private boolean iDoublePrec;
56
57 private static final Pattern WHITE_PAT = Pattern.compile("^.*[\\s\\n]+.*$");
58
59
60
61
62
63
64
65
66
67 private static Value parse(URIString pUriStr, boolean pDoublePrec, boolean pThrow) throws IllegalArgumentException {
68 URIString uriStr = pUriStr.deepCopy();
69
70 String strVal = uriStr.removeTill(',');
71 if (strVal == null) {
72 if (pThrow) {
73 String msg = "Empty real value!\n" + uriStr.markPosition();
74 throw new IllegalArgumentException(msg);
75 }
76 return null;
77 }
78
79 Matcher m = WHITE_PAT.matcher(strVal);
80 if (m.matches()) {
81 if (pThrow) {
82 String msg = "Illegal real format!\n" + pUriStr.markPosition();
83 throw new IllegalArgumentException(msg);
84 }
85 return null;
86 }
87 try {
88 if (WBEMConfiguration.getGlobalConfiguration().verifyJavaLangDoubleStrings()) {
89 if (Util.isBadDoubleString(strVal)) throw new IllegalArgumentException(
90 "Double value string hangs older JVMs!\n" + pUriStr.markPosition()
91 );
92 }
93 double val = Double.parseDouble(strVal);
94 pUriStr.set(uriStr);
95 return new RealValue(val, pDoublePrec);
96 } catch (NumberFormatException e) {
97 if (pThrow) {
98 String msg = "Illegal number format!\n" + pUriStr.markPosition() + "Nested message:\n" + e.getMessage();
99 throw new IllegalArgumentException(msg);
100 }
101 return null;
102 }
103 }
104
105
106
107
108
109
110
111 public static Value parse(URIString pUriStr) {
112 return parse(pUriStr, true, false);
113 }
114
115
116
117
118
119
120
121
122
123 public static Value parseFloat(URIString pUriStr) throws IllegalArgumentException {
124 return parse(pUriStr, false, true);
125 }
126
127
128
129
130
131
132
133
134
135 public static Value parseDouble(URIString pUriStr) throws IllegalArgumentException {
136 return parse(pUriStr, true, true);
137 }
138
139 private RealValue(double pValue, boolean pDoublePrec) {
140 this.iValue = pValue;
141 this.iDoublePrec = pDoublePrec;
142 }
143
144
145
146
147
148
149 public boolean isDouble() {
150 return this.iDoublePrec;
151 }
152
153
154
155
156
157
158 public float floatValue() {
159 return (float) this.iValue;
160 }
161
162
163
164
165
166
167 public double doubleValue() {
168 return this.iValue;
169 }
170
171
172
173
174 @Override
175 public String toString() {
176 return Double.toString(this.iValue);
177 }
178
179
180
181
182 @Override
183 public String getTypeInfo() {
184 if (this.iDoublePrec) return MOF.DT_REAL64;
185 return MOF.DT_REAL32;
186 }
187 }