author | nishjain |
Fri, 27 Jul 2018 14:20:07 +0530 | |
changeset 51257 | 979e349059eb |
parent 49524 | 171e0beb4ff1 |
child 51275 | b095f437af22 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49524 | 2 |
* Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
/* |
|
27 |
* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved |
|
28 |
* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved |
|
29 |
* |
|
30 |
* The original version of this source code and documentation is copyrighted |
|
31 |
* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These |
|
32 |
* materials are provided under terms of a License Agreement between Taligent |
|
33 |
* and Sun. This technology is protected by multiple US and International |
|
34 |
* patents. This notice and attribution to Taligent may not be removed. |
|
35 |
* Taligent is a registered trademark of Taligent, Inc. |
|
36 |
* |
|
37 |
*/ |
|
38 |
||
39 |
package java.text; |
|
40 |
||
41 |
import java.io.InvalidObjectException; |
|
42 |
import java.io.IOException; |
|
43 |
import java.io.ObjectInputStream; |
|
44 |
import java.util.Arrays; |
|
45 |
||
46 |
/** |
|
47 |
* A <code>ChoiceFormat</code> allows you to attach a format to a range of numbers. |
|
48 |
* It is generally used in a <code>MessageFormat</code> for handling plurals. |
|
49 |
* The choice is specified with an ascending list of doubles, where each item |
|
50 |
* specifies a half-open interval up to the next item: |
|
51 |
* <blockquote> |
|
52 |
* <pre> |
|
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
53 |
* X matches j if and only if limit[j] ≤ X < limit[j+1] |
2 | 54 |
* </pre> |
55 |
* </blockquote> |
|
56 |
* If there is no match, then either the first or last index is used, depending |
|
57 |
* on whether the number (X) is too low or too high. If the limit array is not |
|
58 |
* in ascending order, the results of formatting will be incorrect. ChoiceFormat |
|
59 |
* also accepts <code>\u221E</code> as equivalent to infinity(INF). |
|
60 |
* |
|
61 |
* <p> |
|
62 |
* <strong>Note:</strong> |
|
63 |
* <code>ChoiceFormat</code> differs from the other <code>Format</code> |
|
64 |
* classes in that you create a <code>ChoiceFormat</code> object with a |
|
65 |
* constructor (not with a <code>getInstance</code> style factory |
|
66 |
* method). The factory methods aren't necessary because <code>ChoiceFormat</code> |
|
67 |
* doesn't require any complex setup for a given locale. In fact, |
|
68 |
* <code>ChoiceFormat</code> doesn't implement any locale specific behavior. |
|
69 |
* |
|
70 |
* <p> |
|
71 |
* When creating a <code>ChoiceFormat</code>, you must specify an array of formats |
|
72 |
* and an array of limits. The length of these arrays must be the same. |
|
73 |
* For example, |
|
74 |
* <ul> |
|
75 |
* <li> |
|
76 |
* <em>limits</em> = {1,2,3,4,5,6,7}<br> |
|
77 |
* <em>formats</em> = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"} |
|
78 |
* <li> |
|
79 |
* <em>limits</em> = {0, 1, ChoiceFormat.nextDouble(1)}<br> |
|
80 |
* <em>formats</em> = {"no files", "one file", "many files"}<br> |
|
81 |
* (<code>nextDouble</code> can be used to get the next higher double, to |
|
82 |
* make the half-open interval.) |
|
83 |
* </ul> |
|
84 |
* |
|
85 |
* <p> |
|
86 |
* Here is a simple example that shows formatting and parsing: |
|
87 |
* <blockquote> |
|
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
88 |
* <pre>{@code |
2 | 89 |
* double[] limits = {1,2,3,4,5,6,7}; |
90 |
* String[] dayOfWeekNames = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}; |
|
91 |
* ChoiceFormat form = new ChoiceFormat(limits, dayOfWeekNames); |
|
92 |
* ParsePosition status = new ParsePosition(0); |
|
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
93 |
* for (double i = 0.0; i <= 8.0; ++i) { |
2 | 94 |
* status.setIndex(0); |
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
95 |
* System.out.println(i + " -> " + form.format(i) + " -> " |
2 | 96 |
* + form.parse(form.format(i),status)); |
97 |
* } |
|
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
98 |
* }</pre> |
2 | 99 |
* </blockquote> |
100 |
* Here is a more complex example, with a pattern format: |
|
101 |
* <blockquote> |
|
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
102 |
* <pre>{@code |
2 | 103 |
* double[] filelimits = {0,1,2}; |
104 |
* String[] filepart = {"are no files","is one file","are {2} files"}; |
|
105 |
* ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart); |
|
106 |
* Format[] testFormats = {fileform, null, NumberFormat.getInstance()}; |
|
107 |
* MessageFormat pattform = new MessageFormat("There {0} on {1}"); |
|
108 |
* pattform.setFormats(testFormats); |
|
109 |
* Object[] testArgs = {null, "ADisk", null}; |
|
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
110 |
* for (int i = 0; i < 4; ++i) { |
2 | 111 |
* testArgs[0] = new Integer(i); |
112 |
* testArgs[2] = testArgs[0]; |
|
113 |
* System.out.println(pattform.format(testArgs)); |
|
114 |
* } |
|
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
115 |
* }</pre> |
2 | 116 |
* </blockquote> |
117 |
* <p> |
|
118 |
* Specifying a pattern for ChoiceFormat objects is fairly straightforward. |
|
119 |
* For example: |
|
120 |
* <blockquote> |
|
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
121 |
* <pre>{@code |
2 | 122 |
* ChoiceFormat fmt = new ChoiceFormat( |
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
123 |
* "-1#is negative| 0#is zero or fraction | 1#is one |1.0<is 1+ |2#is two |2<is more than 2."); |
2 | 124 |
* System.out.println("Formatter Pattern : " + fmt.toPattern()); |
125 |
* |
|
126 |
* System.out.println("Format with -INF : " + fmt.format(Double.NEGATIVE_INFINITY)); |
|
127 |
* System.out.println("Format with -1.0 : " + fmt.format(-1.0)); |
|
128 |
* System.out.println("Format with 0 : " + fmt.format(0)); |
|
129 |
* System.out.println("Format with 0.9 : " + fmt.format(0.9)); |
|
130 |
* System.out.println("Format with 1.0 : " + fmt.format(1)); |
|
131 |
* System.out.println("Format with 1.5 : " + fmt.format(1.5)); |
|
132 |
* System.out.println("Format with 2 : " + fmt.format(2)); |
|
133 |
* System.out.println("Format with 2.1 : " + fmt.format(2.1)); |
|
134 |
* System.out.println("Format with NaN : " + fmt.format(Double.NaN)); |
|
135 |
* System.out.println("Format with +INF : " + fmt.format(Double.POSITIVE_INFINITY)); |
|
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
136 |
* }</pre> |
2 | 137 |
* </blockquote> |
138 |
* And the output result would be like the following: |
|
9226
d0aa5fcc8743
7036842: HTML tag mismatch in API doc for ChoiceFormat
peytoia
parents:
5506
diff
changeset
|
139 |
* <blockquote> |
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
140 |
* <pre>{@code |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
141 |
* Format with -INF : is negative |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
142 |
* Format with -1.0 : is negative |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
143 |
* Format with 0 : is zero or fraction |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
144 |
* Format with 0.9 : is zero or fraction |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
145 |
* Format with 1.0 : is one |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
146 |
* Format with 1.5 : is 1+ |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
147 |
* Format with 2 : is two |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
148 |
* Format with 2.1 : is more than 2. |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
149 |
* Format with NaN : is negative |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
150 |
* Format with +INF : is more than 2. |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
151 |
* }</pre> |
2 | 152 |
* </blockquote> |
153 |
* |
|
44844
b2b4d98404ba
8179364: update "<a name=" in java.base module to use id attribute
jjg
parents:
43011
diff
changeset
|
154 |
* <h3><a id="synchronization">Synchronization</a></h3> |
2 | 155 |
* |
156 |
* <p> |
|
157 |
* Choice formats are not synchronized. |
|
158 |
* It is recommended to create separate format instances for each thread. |
|
159 |
* If multiple threads access a format concurrently, it must be synchronized |
|
160 |
* externally. |
|
161 |
* |
|
162 |
* |
|
163 |
* @see DecimalFormat |
|
164 |
* @see MessageFormat |
|
165 |
* @author Mark Davis |
|
45434
4582657c7260
8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents:
44844
diff
changeset
|
166 |
* @since 1.1 |
2 | 167 |
*/ |
168 |
public class ChoiceFormat extends NumberFormat { |
|
169 |
||
170 |
// Proclaim serial compatibility with 1.1 FCS |
|
171 |
private static final long serialVersionUID = 1795184449645032964L; |
|
172 |
||
173 |
/** |
|
174 |
* Sets the pattern. |
|
175 |
* @param newPattern See the class description. |
|
43011
36edf207e84c
8169480: Inconsistencies across Format class hierarchy in their API spec and actual implementation of Exceptions
nishjain
parents:
37521
diff
changeset
|
176 |
* @exception NullPointerException if {@code newPattern} |
36edf207e84c
8169480: Inconsistencies across Format class hierarchy in their API spec and actual implementation of Exceptions
nishjain
parents:
37521
diff
changeset
|
177 |
* is {@code null} |
2 | 178 |
*/ |
179 |
public void applyPattern(String newPattern) { |
|
180 |
StringBuffer[] segments = new StringBuffer[2]; |
|
181 |
for (int i = 0; i < segments.length; ++i) { |
|
182 |
segments[i] = new StringBuffer(); |
|
183 |
} |
|
184 |
double[] newChoiceLimits = new double[30]; |
|
185 |
String[] newChoiceFormats = new String[30]; |
|
186 |
int count = 0; |
|
187 |
int part = 0; |
|
188 |
double startValue = 0; |
|
189 |
double oldStartValue = Double.NaN; |
|
190 |
boolean inQuote = false; |
|
191 |
for (int i = 0; i < newPattern.length(); ++i) { |
|
192 |
char ch = newPattern.charAt(i); |
|
193 |
if (ch=='\'') { |
|
194 |
// Check for "''" indicating a literal quote |
|
195 |
if ((i+1)<newPattern.length() && newPattern.charAt(i+1)==ch) { |
|
196 |
segments[part].append(ch); |
|
197 |
++i; |
|
198 |
} else { |
|
199 |
inQuote = !inQuote; |
|
200 |
} |
|
201 |
} else if (inQuote) { |
|
202 |
segments[part].append(ch); |
|
203 |
} else if (ch == '<' || ch == '#' || ch == '\u2264') { |
|
204 |
if (segments[0].length() == 0) { |
|
46893
4c5a4cd5b092
6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents:
45434
diff
changeset
|
205 |
throw new IllegalArgumentException("Each interval must" |
4c5a4cd5b092
6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents:
45434
diff
changeset
|
206 |
+ " contain a number before a format"); |
2 | 207 |
} |
46893
4c5a4cd5b092
6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents:
45434
diff
changeset
|
208 |
|
4c5a4cd5b092
6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents:
45434
diff
changeset
|
209 |
String tempBuffer = segments[0].toString(); |
4c5a4cd5b092
6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents:
45434
diff
changeset
|
210 |
if (tempBuffer.equals("\u221E")) { |
4c5a4cd5b092
6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents:
45434
diff
changeset
|
211 |
startValue = Double.POSITIVE_INFINITY; |
4c5a4cd5b092
6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents:
45434
diff
changeset
|
212 |
} else if (tempBuffer.equals("-\u221E")) { |
4c5a4cd5b092
6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents:
45434
diff
changeset
|
213 |
startValue = Double.NEGATIVE_INFINITY; |
4c5a4cd5b092
6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents:
45434
diff
changeset
|
214 |
} else { |
49510
a7777856ae98
8200364: Remove unnecessary boxing via primitive wrapper valueOf(String) methods
martin
parents:
47216
diff
changeset
|
215 |
startValue = Double.parseDouble(tempBuffer); |
2 | 216 |
} |
46893
4c5a4cd5b092
6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents:
45434
diff
changeset
|
217 |
|
2 | 218 |
if (ch == '<' && startValue != Double.POSITIVE_INFINITY && |
219 |
startValue != Double.NEGATIVE_INFINITY) { |
|
220 |
startValue = nextDouble(startValue); |
|
221 |
} |
|
222 |
if (startValue <= oldStartValue) { |
|
46893
4c5a4cd5b092
6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents:
45434
diff
changeset
|
223 |
throw new IllegalArgumentException("Incorrect order of" |
4c5a4cd5b092
6609718: [Fmt-Ch] uninformative exception in ChoiceFormat.applyPattern(String)
nishjain
parents:
45434
diff
changeset
|
224 |
+ " intervals, must be in ascending order"); |
2 | 225 |
} |
226 |
segments[0].setLength(0); |
|
227 |
part = 1; |
|
228 |
} else if (ch == '|') { |
|
229 |
if (count == newChoiceLimits.length) { |
|
230 |
newChoiceLimits = doubleArraySize(newChoiceLimits); |
|
231 |
newChoiceFormats = doubleArraySize(newChoiceFormats); |
|
232 |
} |
|
233 |
newChoiceLimits[count] = startValue; |
|
234 |
newChoiceFormats[count] = segments[1].toString(); |
|
235 |
++count; |
|
236 |
oldStartValue = startValue; |
|
237 |
segments[1].setLength(0); |
|
238 |
part = 0; |
|
239 |
} else { |
|
240 |
segments[part].append(ch); |
|
241 |
} |
|
242 |
} |
|
243 |
// clean up last one |
|
244 |
if (part == 1) { |
|
245 |
if (count == newChoiceLimits.length) { |
|
246 |
newChoiceLimits = doubleArraySize(newChoiceLimits); |
|
247 |
newChoiceFormats = doubleArraySize(newChoiceFormats); |
|
248 |
} |
|
249 |
newChoiceLimits[count] = startValue; |
|
250 |
newChoiceFormats[count] = segments[1].toString(); |
|
251 |
++count; |
|
252 |
} |
|
253 |
choiceLimits = new double[count]; |
|
254 |
System.arraycopy(newChoiceLimits, 0, choiceLimits, 0, count); |
|
255 |
choiceFormats = new String[count]; |
|
256 |
System.arraycopy(newChoiceFormats, 0, choiceFormats, 0, count); |
|
257 |
} |
|
258 |
||
259 |
/** |
|
260 |
* Gets the pattern. |
|
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
261 |
* |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
262 |
* @return the pattern string |
2 | 263 |
*/ |
264 |
public String toPattern() { |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
21278
diff
changeset
|
265 |
StringBuilder result = new StringBuilder(); |
2 | 266 |
for (int i = 0; i < choiceLimits.length; ++i) { |
267 |
if (i != 0) { |
|
268 |
result.append('|'); |
|
269 |
} |
|
270 |
// choose based upon which has less precision |
|
271 |
// approximate that by choosing the closest one to an integer. |
|
272 |
// could do better, but it's not worth it. |
|
273 |
double less = previousDouble(choiceLimits[i]); |
|
274 |
double tryLessOrEqual = Math.abs(Math.IEEEremainder(choiceLimits[i], 1.0d)); |
|
275 |
double tryLess = Math.abs(Math.IEEEremainder(less, 1.0d)); |
|
276 |
||
277 |
if (tryLessOrEqual < tryLess) { |
|
27957
24b4e6082f19
8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents:
25859
diff
changeset
|
278 |
result.append(choiceLimits[i]); |
2 | 279 |
result.append('#'); |
280 |
} else { |
|
281 |
if (choiceLimits[i] == Double.POSITIVE_INFINITY) { |
|
282 |
result.append("\u221E"); |
|
283 |
} else if (choiceLimits[i] == Double.NEGATIVE_INFINITY) { |
|
284 |
result.append("-\u221E"); |
|
285 |
} else { |
|
27957
24b4e6082f19
8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents:
25859
diff
changeset
|
286 |
result.append(less); |
2 | 287 |
} |
288 |
result.append('<'); |
|
289 |
} |
|
290 |
// Append choiceFormats[i], using quotes if there are special characters. |
|
291 |
// Single quotes themselves must be escaped in either case. |
|
292 |
String text = choiceFormats[i]; |
|
293 |
boolean needQuote = text.indexOf('<') >= 0 |
|
294 |
|| text.indexOf('#') >= 0 |
|
295 |
|| text.indexOf('\u2264') >= 0 |
|
296 |
|| text.indexOf('|') >= 0; |
|
297 |
if (needQuote) result.append('\''); |
|
298 |
if (text.indexOf('\'') < 0) result.append(text); |
|
299 |
else { |
|
300 |
for (int j=0; j<text.length(); ++j) { |
|
301 |
char c = text.charAt(j); |
|
302 |
result.append(c); |
|
303 |
if (c == '\'') result.append(c); |
|
304 |
} |
|
305 |
} |
|
306 |
if (needQuote) result.append('\''); |
|
307 |
} |
|
308 |
return result.toString(); |
|
309 |
} |
|
310 |
||
311 |
/** |
|
312 |
* Constructs with limits and corresponding formats based on the pattern. |
|
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
313 |
* |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
314 |
* @param newPattern the new pattern string |
49524 | 315 |
* @exception NullPointerException if {@code newPattern} is |
43011
36edf207e84c
8169480: Inconsistencies across Format class hierarchy in their API spec and actual implementation of Exceptions
nishjain
parents:
37521
diff
changeset
|
316 |
* {@code null} |
2 | 317 |
* @see #applyPattern |
318 |
*/ |
|
319 |
public ChoiceFormat(String newPattern) { |
|
320 |
applyPattern(newPattern); |
|
321 |
} |
|
322 |
||
323 |
/** |
|
324 |
* Constructs with the limits and the corresponding formats. |
|
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
325 |
* |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
326 |
* @param limits limits in ascending order |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
327 |
* @param formats corresponding format strings |
43011
36edf207e84c
8169480: Inconsistencies across Format class hierarchy in their API spec and actual implementation of Exceptions
nishjain
parents:
37521
diff
changeset
|
328 |
* @exception NullPointerException if {@code limits} or {@code formats} |
36edf207e84c
8169480: Inconsistencies across Format class hierarchy in their API spec and actual implementation of Exceptions
nishjain
parents:
37521
diff
changeset
|
329 |
* is {@code null} |
2 | 330 |
* @see #setChoices |
331 |
*/ |
|
332 |
public ChoiceFormat(double[] limits, String[] formats) { |
|
333 |
setChoices(limits, formats); |
|
334 |
} |
|
335 |
||
336 |
/** |
|
337 |
* Set the choices to be used in formatting. |
|
338 |
* @param limits contains the top value that you want |
|
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
339 |
* parsed with that format, and should be in ascending sorted order. When |
2 | 340 |
* formatting X, the choice will be the i, where |
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
341 |
* limit[i] ≤ X {@literal <} limit[i+1]. |
2 | 342 |
* If the limit array is not in ascending order, the results of formatting |
343 |
* will be incorrect. |
|
344 |
* @param formats are the formats you want to use for each limit. |
|
345 |
* They can be either Format objects or Strings. |
|
346 |
* When formatting with object Y, |
|
347 |
* if the object is a NumberFormat, then ((NumberFormat) Y).format(X) |
|
348 |
* is called. Otherwise Y.toString() is called. |
|
43011
36edf207e84c
8169480: Inconsistencies across Format class hierarchy in their API spec and actual implementation of Exceptions
nishjain
parents:
37521
diff
changeset
|
349 |
* @exception NullPointerException if {@code limits} or |
36edf207e84c
8169480: Inconsistencies across Format class hierarchy in their API spec and actual implementation of Exceptions
nishjain
parents:
37521
diff
changeset
|
350 |
* {@code formats} is {@code null} |
2 | 351 |
*/ |
352 |
public void setChoices(double[] limits, String formats[]) { |
|
353 |
if (limits.length != formats.length) { |
|
354 |
throw new IllegalArgumentException( |
|
355 |
"Array and limit arrays must be of the same length."); |
|
356 |
} |
|
14341
d06c0d29521c
8001209: Evaluate findbugs reprot for java.text.ChoiceFormat
peytoia
parents:
12848
diff
changeset
|
357 |
choiceLimits = Arrays.copyOf(limits, limits.length); |
d06c0d29521c
8001209: Evaluate findbugs reprot for java.text.ChoiceFormat
peytoia
parents:
12848
diff
changeset
|
358 |
choiceFormats = Arrays.copyOf(formats, formats.length); |
2 | 359 |
} |
360 |
||
361 |
/** |
|
362 |
* Get the limits passed in the constructor. |
|
363 |
* @return the limits. |
|
364 |
*/ |
|
365 |
public double[] getLimits() { |
|
14341
d06c0d29521c
8001209: Evaluate findbugs reprot for java.text.ChoiceFormat
peytoia
parents:
12848
diff
changeset
|
366 |
double[] newLimits = Arrays.copyOf(choiceLimits, choiceLimits.length); |
d06c0d29521c
8001209: Evaluate findbugs reprot for java.text.ChoiceFormat
peytoia
parents:
12848
diff
changeset
|
367 |
return newLimits; |
2 | 368 |
} |
369 |
||
370 |
/** |
|
371 |
* Get the formats passed in the constructor. |
|
372 |
* @return the formats. |
|
373 |
*/ |
|
374 |
public Object[] getFormats() { |
|
14341
d06c0d29521c
8001209: Evaluate findbugs reprot for java.text.ChoiceFormat
peytoia
parents:
12848
diff
changeset
|
375 |
Object[] newFormats = Arrays.copyOf(choiceFormats, choiceFormats.length); |
d06c0d29521c
8001209: Evaluate findbugs reprot for java.text.ChoiceFormat
peytoia
parents:
12848
diff
changeset
|
376 |
return newFormats; |
2 | 377 |
} |
378 |
||
379 |
// Overrides |
|
380 |
||
381 |
/** |
|
382 |
* Specialization of format. This method really calls |
|
383 |
* <code>format(double, StringBuffer, FieldPosition)</code> |
|
384 |
* thus the range of longs that are supported is only equal to |
|
385 |
* the range that can be stored by double. This will never be |
|
386 |
* a practical limitation. |
|
387 |
*/ |
|
388 |
public StringBuffer format(long number, StringBuffer toAppendTo, |
|
389 |
FieldPosition status) { |
|
390 |
return format((double)number, toAppendTo, status); |
|
391 |
} |
|
392 |
||
393 |
/** |
|
394 |
* Returns pattern with formatted double. |
|
18156 | 395 |
* @param number number to be formatted and substituted. |
2 | 396 |
* @param toAppendTo where text is appended. |
397 |
* @param status ignore no useful status is returned. |
|
43011
36edf207e84c
8169480: Inconsistencies across Format class hierarchy in their API spec and actual implementation of Exceptions
nishjain
parents:
37521
diff
changeset
|
398 |
* @exception NullPointerException if {@code toAppendTo} |
36edf207e84c
8169480: Inconsistencies across Format class hierarchy in their API spec and actual implementation of Exceptions
nishjain
parents:
37521
diff
changeset
|
399 |
* is {@code null} |
2 | 400 |
*/ |
401 |
public StringBuffer format(double number, StringBuffer toAppendTo, |
|
402 |
FieldPosition status) { |
|
403 |
// find the number |
|
404 |
int i; |
|
405 |
for (i = 0; i < choiceLimits.length; ++i) { |
|
406 |
if (!(number >= choiceLimits[i])) { |
|
407 |
// same as number < choiceLimits, except catchs NaN |
|
408 |
break; |
|
409 |
} |
|
410 |
} |
|
411 |
--i; |
|
412 |
if (i < 0) i = 0; |
|
413 |
// return either a formatted number, or a string |
|
414 |
return toAppendTo.append(choiceFormats[i]); |
|
415 |
} |
|
416 |
||
417 |
/** |
|
418 |
* Parses a Number from the input text. |
|
419 |
* @param text the source text. |
|
420 |
* @param status an input-output parameter. On input, the |
|
421 |
* status.index field indicates the first character of the |
|
422 |
* source text that should be parsed. On exit, if no error |
|
21278 | 423 |
* occurred, status.index is set to the first unparsed character |
2 | 424 |
* in the source text. On exit, if an error did occur, |
425 |
* status.index is unchanged and status.errorIndex is set to the |
|
426 |
* first index of the character that caused the parse to fail. |
|
427 |
* @return A Number representing the value of the number parsed. |
|
43011
36edf207e84c
8169480: Inconsistencies across Format class hierarchy in their API spec and actual implementation of Exceptions
nishjain
parents:
37521
diff
changeset
|
428 |
* @exception NullPointerException if {@code status} is {@code null} |
36edf207e84c
8169480: Inconsistencies across Format class hierarchy in their API spec and actual implementation of Exceptions
nishjain
parents:
37521
diff
changeset
|
429 |
* or if {@code text} is {@code null} and the list of |
36edf207e84c
8169480: Inconsistencies across Format class hierarchy in their API spec and actual implementation of Exceptions
nishjain
parents:
37521
diff
changeset
|
430 |
* choice strings is not empty. |
2 | 431 |
*/ |
432 |
public Number parse(String text, ParsePosition status) { |
|
433 |
// find the best number (defined as the one with the longest parse) |
|
434 |
int start = status.index; |
|
435 |
int furthest = start; |
|
436 |
double bestNumber = Double.NaN; |
|
437 |
double tempNumber = 0.0; |
|
438 |
for (int i = 0; i < choiceFormats.length; ++i) { |
|
439 |
String tempString = choiceFormats[i]; |
|
440 |
if (text.regionMatches(start, tempString, 0, tempString.length())) { |
|
441 |
status.index = start + tempString.length(); |
|
442 |
tempNumber = choiceLimits[i]; |
|
443 |
if (status.index > furthest) { |
|
444 |
furthest = status.index; |
|
445 |
bestNumber = tempNumber; |
|
446 |
if (furthest == text.length()) break; |
|
447 |
} |
|
448 |
} |
|
449 |
} |
|
450 |
status.index = furthest; |
|
451 |
if (status.index == start) { |
|
452 |
status.errorIndex = furthest; |
|
453 |
} |
|
37521
b6e0f285c998
8145468: update java.lang APIs with new deprecations
smarks
parents:
30655
diff
changeset
|
454 |
return Double.valueOf(bestNumber); |
2 | 455 |
} |
456 |
||
457 |
/** |
|
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
458 |
* Finds the least double greater than {@code d}. |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
459 |
* If {@code NaN}, returns same value. |
2 | 460 |
* <p>Used to make half-open intervals. |
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
461 |
* |
51257
979e349059eb
8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents:
49524
diff
changeset
|
462 |
* @implNote This is equivalent to calling |
979e349059eb
8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents:
49524
diff
changeset
|
463 |
* {@link Math#nextUp(double) Math.nextUp(d)} |
979e349059eb
8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents:
49524
diff
changeset
|
464 |
* |
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
465 |
* @param d the reference value |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
466 |
* @return the least double value greather than {@code d} |
2 | 467 |
* @see #previousDouble |
468 |
*/ |
|
469 |
public static final double nextDouble (double d) { |
|
51257
979e349059eb
8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents:
49524
diff
changeset
|
470 |
return Math.nextUp(d); |
2 | 471 |
} |
472 |
||
473 |
/** |
|
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
474 |
* Finds the greatest double less than {@code d}. |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
475 |
* If {@code NaN}, returns same value. |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
476 |
* |
51257
979e349059eb
8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents:
49524
diff
changeset
|
477 |
* @implNote This is equivalent to calling |
979e349059eb
8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents:
49524
diff
changeset
|
478 |
* {@link Math#nextDown(double) Math.nextDown(d)} |
979e349059eb
8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents:
49524
diff
changeset
|
479 |
* |
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
480 |
* @param d the reference value |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
481 |
* @return the greatest double value less than {@code d} |
2 | 482 |
* @see #nextDouble |
483 |
*/ |
|
484 |
public static final double previousDouble (double d) { |
|
51257
979e349059eb
8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents:
49524
diff
changeset
|
485 |
return Math.nextDown(d); |
2 | 486 |
} |
487 |
||
488 |
/** |
|
489 |
* Overrides Cloneable |
|
490 |
*/ |
|
491 |
public Object clone() |
|
492 |
{ |
|
493 |
ChoiceFormat other = (ChoiceFormat) super.clone(); |
|
494 |
// for primitives or immutables, shallow clone is enough |
|
12848 | 495 |
other.choiceLimits = choiceLimits.clone(); |
496 |
other.choiceFormats = choiceFormats.clone(); |
|
2 | 497 |
return other; |
498 |
} |
|
499 |
||
500 |
/** |
|
501 |
* Generates a hash code for the message format object. |
|
502 |
*/ |
|
503 |
public int hashCode() { |
|
504 |
int result = choiceLimits.length; |
|
505 |
if (choiceFormats.length > 0) { |
|
506 |
// enough for reasonable distribution |
|
507 |
result ^= choiceFormats[choiceFormats.length-1].hashCode(); |
|
508 |
} |
|
509 |
return result; |
|
510 |
} |
|
511 |
||
512 |
/** |
|
30655 | 513 |
* Equality comparison between two |
2 | 514 |
*/ |
515 |
public boolean equals(Object obj) { |
|
516 |
if (obj == null) return false; |
|
517 |
if (this == obj) // quick check |
|
518 |
return true; |
|
519 |
if (getClass() != obj.getClass()) |
|
520 |
return false; |
|
521 |
ChoiceFormat other = (ChoiceFormat) obj; |
|
522 |
return (Arrays.equals(choiceLimits, other.choiceLimits) |
|
523 |
&& Arrays.equals(choiceFormats, other.choiceFormats)); |
|
524 |
} |
|
525 |
||
526 |
/** |
|
527 |
* After reading an object from the input stream, do a simple verification |
|
528 |
* to maintain class invariants. |
|
529 |
* @throws InvalidObjectException if the objects read from the stream is invalid. |
|
530 |
*/ |
|
531 |
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { |
|
532 |
in.defaultReadObject(); |
|
533 |
if (choiceLimits.length != choiceFormats.length) { |
|
534 |
throw new InvalidObjectException( |
|
535 |
"limits and format arrays of different length."); |
|
536 |
} |
|
537 |
} |
|
538 |
||
539 |
// ===============privates=========================== |
|
540 |
||
541 |
/** |
|
542 |
* A list of lower bounds for the choices. The formatter will return |
|
543 |
* <code>choiceFormats[i]</code> if the number being formatted is greater than or equal to |
|
544 |
* <code>choiceLimits[i]</code> and less than <code>choiceLimits[i+1]</code>. |
|
545 |
* @serial |
|
546 |
*/ |
|
547 |
private double[] choiceLimits; |
|
548 |
||
549 |
/** |
|
550 |
* A list of choice strings. The formatter will return |
|
551 |
* <code>choiceFormats[i]</code> if the number being formatted is greater than or equal to |
|
552 |
* <code>choiceLimits[i]</code> and less than <code>choiceLimits[i+1]</code>. |
|
553 |
* @serial |
|
554 |
*/ |
|
555 |
private String[] choiceFormats; |
|
556 |
||
557 |
static final long SIGN = 0x8000000000000000L; |
|
558 |
static final long EXPONENT = 0x7FF0000000000000L; |
|
559 |
static final long POSITIVEINFINITY = 0x7FF0000000000000L; |
|
560 |
||
561 |
/** |
|
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
562 |
* Finds the least double greater than {@code d} (if {@code positive} is |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
563 |
* {@code true}), or the greatest double less than {@code d} (if |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
564 |
* {@code positive} is {@code false}). |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
565 |
* If {@code NaN}, returns same value. |
2 | 566 |
* |
51257
979e349059eb
8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents:
49524
diff
changeset
|
567 |
* @implNote This is equivalent to calling |
979e349059eb
8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents:
49524
diff
changeset
|
568 |
* {@code positive ? Math.nextUp(d) : Math.nextDown(d)} |
19054
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
569 |
* |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
570 |
* @param d the reference value |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
571 |
* @param positive {@code true} if the least double is desired; |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
572 |
* {@code false} otherwise |
a64012cb49d6
8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents:
18156
diff
changeset
|
573 |
* @return the least or greater double value |
2 | 574 |
*/ |
575 |
public static double nextDouble (double d, boolean positive) { |
|
51257
979e349059eb
8021322: [Fmt-Ch] Implementation of ChoiceFormat math methods should delegate to java.lang.Math methods
nishjain
parents:
49524
diff
changeset
|
576 |
return positive ? Math.nextUp(d) : Math.nextDown(d); |
2 | 577 |
} |
578 |
||
579 |
private static double[] doubleArraySize(double[] array) { |
|
580 |
int oldSize = array.length; |
|
581 |
double[] newArray = new double[oldSize * 2]; |
|
582 |
System.arraycopy(array, 0, newArray, 0, oldSize); |
|
583 |
return newArray; |
|
584 |
} |
|
585 |
||
586 |
private String[] doubleArraySize(String[] array) { |
|
587 |
int oldSize = array.length; |
|
588 |
String[] newArray = new String[oldSize * 2]; |
|
589 |
System.arraycopy(array, 0, newArray, 0, oldSize); |
|
590 |
return newArray; |
|
591 |
} |
|
592 |
||
593 |
} |