Print this page
rev 5696 : 6336885: RFE: Locale Data Deployment Enhancements
4609153: Provide locale data for Indic locales
5104387: Support for gl_ES locale (galician language)
6337471: desktop/system locale preferences support
7056139: (cal) SPI support for locale-dependent Calendar parameters
7058206: Provide CalendarData SPI for week params and display field value names
7073852: Support multiple scripts for digits and decimal symbols per locale
7079560: [Fmt-Da] Context dependent month names support in SimpleDateFormat
7171324: getAvailableLocales() of locale sensitive services should return the actual availability of locales
7151414: (cal) Support calendar type identification
7168528: LocaleServiceProvider needs to be aware of Locale extensions
7171372: (cal) locale's default Calendar should be created if unknown calendar is specified
Summary: JEP 127: Improve Locale Data Packaging and Adopt Unicode CLDR Data (part 1 w/o packaging changes. by Naoto Sato and Masayoshi Okutsu)
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/java/text/NumberFormat.java
+++ new/src/share/classes/java/text/NumberFormat.java
1 1 /*
2 2 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25
26 26 /*
27 27 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
28 28 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
29 29 *
30 30 * The original version of this source code and documentation is copyrighted
31 31 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
32 32 * materials are provided under terms of a License Agreement between Taligent
33 33 * and Sun. This technology is protected by multiple US and International
34 34 * patents. This notice and attribution to Taligent may not be removed.
35 35 * Taligent is a registered trademark of Taligent, Inc.
36 36 *
37 37 */
38 38
39 39 package java.text;
40 40
41 41 import java.io.InvalidObjectException;
42 42 import java.io.IOException;
43 43 import java.io.ObjectInputStream;
44 44 import java.io.ObjectOutputStream;
45 45 import java.math.BigInteger;
46 46 import java.math.RoundingMode;
↓ open down ↓ |
46 lines elided |
↑ open up ↑ |
47 47 import java.text.spi.NumberFormatProvider;
48 48 import java.util.Currency;
49 49 import java.util.HashMap;
50 50 import java.util.Hashtable;
51 51 import java.util.Locale;
52 52 import java.util.Map;
53 53 import java.util.ResourceBundle;
54 54 import java.util.concurrent.atomic.AtomicInteger;
55 55 import java.util.concurrent.atomic.AtomicLong;
56 56 import java.util.spi.LocaleServiceProvider;
57 -import sun.util.LocaleServiceProviderPool;
57 +import sun.util.locale.provider.LocaleProviderAdapter;
58 +import sun.util.locale.provider.LocaleServiceProviderPool;
58 59 import sun.util.resources.LocaleData;
59 60
60 61 /**
61 62 * <code>NumberFormat</code> is the abstract base class for all number
62 63 * formats. This class provides the interface for formatting and parsing
63 64 * numbers. <code>NumberFormat</code> also provides methods for determining
64 65 * which locales have number formats, and what their names are.
65 66 *
66 67 * <p>
67 68 * <code>NumberFormat</code> helps you to format and parse numbers for any locale.
68 69 * Your code can be completely independent of the locale conventions for
69 70 * decimal points, thousands-separators, or even the particular decimal
70 71 * digits used, or whether the number format is even decimal.
71 72 *
72 73 * <p>
73 74 * To format a number for the current Locale, use one of the factory
74 75 * class methods:
75 76 * <blockquote>
76 77 * <pre>
77 78 * myString = NumberFormat.getInstance().format(myNumber);
78 79 * </pre>
79 80 * </blockquote>
80 81 * If you are formatting multiple numbers, it is
81 82 * more efficient to get the format and use it multiple times so that
82 83 * the system doesn't have to fetch the information about the local
83 84 * language and country conventions multiple times.
84 85 * <blockquote>
85 86 * <pre>
86 87 * NumberFormat nf = NumberFormat.getInstance();
87 88 * for (int i = 0; i < myNumber.length; ++i) {
88 89 * output.println(nf.format(myNumber[i]) + "; ");
89 90 * }
90 91 * </pre>
91 92 * </blockquote>
92 93 * To format a number for a different Locale, specify it in the
93 94 * call to <code>getInstance</code>.
94 95 * <blockquote>
95 96 * <pre>
96 97 * NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
97 98 * </pre>
98 99 * </blockquote>
99 100 * You can also use a <code>NumberFormat</code> to parse numbers:
100 101 * <blockquote>
101 102 * <pre>
102 103 * myNumber = nf.parse(myString);
103 104 * </pre>
104 105 * </blockquote>
105 106 * Use <code>getInstance</code> or <code>getNumberInstance</code> to get the
106 107 * normal number format. Use <code>getIntegerInstance</code> to get an
107 108 * integer number format. Use <code>getCurrencyInstance</code> to get the
108 109 * currency number format. And use <code>getPercentInstance</code> to get a
109 110 * format for displaying percentages. With this format, a fraction like
110 111 * 0.53 is displayed as 53%.
111 112 *
112 113 * <p>
113 114 * You can also control the display of numbers with such methods as
114 115 * <code>setMinimumFractionDigits</code>.
115 116 * If you want even more control over the format or parsing,
116 117 * or want to give your users more control,
117 118 * you can try casting the <code>NumberFormat</code> you get from the factory methods
118 119 * to a <code>DecimalFormat</code>. This will work for the vast majority
119 120 * of locales; just remember to put it in a <code>try</code> block in case you
120 121 * encounter an unusual one.
121 122 *
122 123 * <p>
123 124 * NumberFormat and DecimalFormat are designed such that some controls
124 125 * work for formatting and others work for parsing. The following is
125 126 * the detailed description for each these control methods,
126 127 * <p>
127 128 * setParseIntegerOnly : only affects parsing, e.g.
128 129 * if true, "3456.78" -> 3456 (and leaves the parse position just after index 6)
129 130 * if false, "3456.78" -> 3456.78 (and leaves the parse position just after index 8)
130 131 * This is independent of formatting. If you want to not show a decimal point
131 132 * where there might be no digits after the decimal point, use
132 133 * setDecimalSeparatorAlwaysShown.
133 134 * <p>
134 135 * setDecimalSeparatorAlwaysShown : only affects formatting, and only where
135 136 * there might be no digits after the decimal point, such as with a pattern
136 137 * like "#,##0.##", e.g.,
137 138 * if true, 3456.00 -> "3,456."
138 139 * if false, 3456.00 -> "3456"
139 140 * This is independent of parsing. If you want parsing to stop at the decimal
140 141 * point, use setParseIntegerOnly.
141 142 *
142 143 * <p>
143 144 * You can also use forms of the <code>parse</code> and <code>format</code>
144 145 * methods with <code>ParsePosition</code> and <code>FieldPosition</code> to
145 146 * allow you to:
146 147 * <ul>
147 148 * <li> progressively parse through pieces of a string
148 149 * <li> align the decimal point and other areas
149 150 * </ul>
150 151 * For example, you can align numbers in two ways:
151 152 * <ol>
152 153 * <li> If you are using a monospaced font with spacing for alignment,
153 154 * you can pass the <code>FieldPosition</code> in your format call, with
154 155 * <code>field</code> = <code>INTEGER_FIELD</code>. On output,
155 156 * <code>getEndIndex</code> will be set to the offset between the
156 157 * last character of the integer and the decimal. Add
157 158 * (desiredSpaceCount - getEndIndex) spaces at the front of the string.
158 159 *
159 160 * <li> If you are using proportional fonts,
160 161 * instead of padding with spaces, measure the width
161 162 * of the string in pixels from the start to <code>getEndIndex</code>.
162 163 * Then move the pen by
163 164 * (desiredPixelWidth - widthToAlignmentPoint) before drawing the text.
164 165 * It also works where there is no decimal, but possibly additional
165 166 * characters at the end, e.g., with parentheses in negative
166 167 * numbers: "(12)" for -12.
167 168 * </ol>
168 169 *
169 170 * <h4><a name="synchronization">Synchronization</a></h4>
170 171 *
171 172 * <p>
172 173 * Number formats are generally not synchronized.
173 174 * It is recommended to create separate format instances for each thread.
174 175 * If multiple threads access a format concurrently, it must be synchronized
175 176 * externally.
176 177 *
177 178 * @see DecimalFormat
178 179 * @see ChoiceFormat
179 180 * @author Mark Davis
180 181 * @author Helena Shih
181 182 */
182 183 public abstract class NumberFormat extends Format {
183 184
184 185 /**
185 186 * Field constant used to construct a FieldPosition object. Signifies that
186 187 * the position of the integer part of a formatted number should be returned.
187 188 * @see java.text.FieldPosition
188 189 */
189 190 public static final int INTEGER_FIELD = 0;
190 191
191 192 /**
192 193 * Field constant used to construct a FieldPosition object. Signifies that
193 194 * the position of the fraction part of a formatted number should be returned.
194 195 * @see java.text.FieldPosition
195 196 */
196 197 public static final int FRACTION_FIELD = 1;
197 198
198 199 /**
199 200 * Sole constructor. (For invocation by subclass constructors, typically
200 201 * implicit.)
201 202 */
202 203 protected NumberFormat() {
203 204 }
204 205
205 206 /**
206 207 * Formats a number and appends the resulting text to the given string
207 208 * buffer.
208 209 * The number can be of any subclass of {@link java.lang.Number}.
209 210 * <p>
210 211 * This implementation extracts the number's value using
211 212 * {@link java.lang.Number#longValue()} for all integral type values that
212 213 * can be converted to <code>long</code> without loss of information,
213 214 * including <code>BigInteger</code> values with a
214 215 * {@link java.math.BigInteger#bitLength() bit length} of less than 64,
215 216 * and {@link java.lang.Number#doubleValue()} for all other types. It
216 217 * then calls
217 218 * {@link #format(long,java.lang.StringBuffer,java.text.FieldPosition)}
218 219 * or {@link #format(double,java.lang.StringBuffer,java.text.FieldPosition)}.
219 220 * This may result in loss of magnitude information and precision for
220 221 * <code>BigInteger</code> and <code>BigDecimal</code> values.
221 222 * @param number the number to format
222 223 * @param toAppendTo the <code>StringBuffer</code> to which the formatted
223 224 * text is to be appended
224 225 * @param pos On input: an alignment field, if desired.
↓ open down ↓ |
157 lines elided |
↑ open up ↑ |
225 226 * On output: the offsets of the alignment field.
226 227 * @return the value passed in as <code>toAppendTo</code>
227 228 * @exception IllegalArgumentException if <code>number</code> is
228 229 * null or not an instance of <code>Number</code>.
229 230 * @exception NullPointerException if <code>toAppendTo</code> or
230 231 * <code>pos</code> is null
231 232 * @exception ArithmeticException if rounding is needed with rounding
232 233 * mode being set to RoundingMode.UNNECESSARY
233 234 * @see java.text.FieldPosition
234 235 */
236 + @Override
235 237 public StringBuffer format(Object number,
236 238 StringBuffer toAppendTo,
237 239 FieldPosition pos) {
238 240 if (number instanceof Long || number instanceof Integer ||
239 241 number instanceof Short || number instanceof Byte ||
240 242 number instanceof AtomicInteger || number instanceof AtomicLong ||
241 243 (number instanceof BigInteger &&
242 244 ((BigInteger)number).bitLength() < 64)) {
243 245 return format(((Number)number).longValue(), toAppendTo, pos);
244 246 } else if (number instanceof Number) {
245 247 return format(((Number)number).doubleValue(), toAppendTo, pos);
246 248 } else {
247 249 throw new IllegalArgumentException("Cannot format given Object as a Number");
248 250 }
249 251 }
250 252
251 253 /**
252 254 * Parses text from a string to produce a <code>Number</code>.
253 255 * <p>
254 256 * The method attempts to parse text starting at the index given by
255 257 * <code>pos</code>.
256 258 * If parsing succeeds, then the index of <code>pos</code> is updated
257 259 * to the index after the last character used (parsing does not necessarily
258 260 * use all characters up to the end of the string), and the parsed
259 261 * number is returned. The updated <code>pos</code> can be used to
260 262 * indicate the starting point for the next call to this method.
261 263 * If an error occurs, then the index of <code>pos</code> is not
262 264 * changed, the error index of <code>pos</code> is set to the index of
263 265 * the character where the error occurred, and null is returned.
264 266 * <p>
↓ open down ↓ |
20 lines elided |
↑ open up ↑ |
265 267 * See the {@link #parse(String, ParsePosition)} method for more information
266 268 * on number parsing.
267 269 *
268 270 * @param source A <code>String</code>, part of which should be parsed.
269 271 * @param pos A <code>ParsePosition</code> object with index and error
270 272 * index information as described above.
271 273 * @return A <code>Number</code> parsed from the string. In case of
272 274 * error, returns null.
273 275 * @exception NullPointerException if <code>pos</code> is null.
274 276 */
277 + @Override
275 278 public final Object parseObject(String source, ParsePosition pos) {
276 279 return parse(source, pos);
277 280 }
278 281
279 282 /**
280 283 * Specialization of format.
281 284 * @exception ArithmeticException if rounding is needed with rounding
282 285 * mode being set to RoundingMode.UNNECESSARY
283 286 * @see java.text.Format#format
284 287 */
285 288 public final String format(double number) {
286 289 return format(number, new StringBuffer(),
287 290 DontCareFieldPosition.INSTANCE).toString();
288 291 }
289 292
290 293 /**
291 294 * Specialization of format.
292 295 * @exception ArithmeticException if rounding is needed with rounding
293 296 * mode being set to RoundingMode.UNNECESSARY
294 297 * @see java.text.Format#format
295 298 */
296 299 public final String format(long number) {
297 300 return format(number, new StringBuffer(),
298 301 DontCareFieldPosition.INSTANCE).toString();
299 302 }
300 303
301 304 /**
302 305 * Specialization of format.
303 306 * @exception ArithmeticException if rounding is needed with rounding
304 307 * mode being set to RoundingMode.UNNECESSARY
305 308 * @see java.text.Format#format
306 309 */
307 310 public abstract StringBuffer format(double number,
308 311 StringBuffer toAppendTo,
309 312 FieldPosition pos);
310 313
311 314 /**
312 315 * Specialization of format.
313 316 * @exception ArithmeticException if rounding is needed with rounding
314 317 * mode being set to RoundingMode.UNNECESSARY
315 318 * @see java.text.Format#format
316 319 */
317 320 public abstract StringBuffer format(long number,
318 321 StringBuffer toAppendTo,
319 322 FieldPosition pos);
320 323
321 324 /**
322 325 * Returns a Long if possible (e.g., within the range [Long.MIN_VALUE,
323 326 * Long.MAX_VALUE] and with no decimals), otherwise a Double.
324 327 * If IntegerOnly is set, will stop at a decimal
325 328 * point (or equivalent; e.g., for rational numbers "1 2/3", will stop
326 329 * after the 1).
327 330 * Does not throw an exception; if no object can be parsed, index is
328 331 * unchanged!
329 332 * @see java.text.NumberFormat#isParseIntegerOnly
330 333 * @see java.text.Format#parseObject
331 334 */
332 335 public abstract Number parse(String source, ParsePosition parsePosition);
333 336
334 337 /**
335 338 * Parses text from the beginning of the given string to produce a number.
336 339 * The method may not use the entire text of the given string.
337 340 * <p>
338 341 * See the {@link #parse(String, ParsePosition)} method for more information
339 342 * on number parsing.
340 343 *
341 344 * @param source A <code>String</code> whose beginning should be parsed.
342 345 * @return A <code>Number</code> parsed from the string.
343 346 * @exception ParseException if the beginning of the specified string
344 347 * cannot be parsed.
345 348 */
346 349 public Number parse(String source) throws ParseException {
347 350 ParsePosition parsePosition = new ParsePosition(0);
348 351 Number result = parse(source, parsePosition);
349 352 if (parsePosition.index == 0) {
350 353 throw new ParseException("Unparseable number: \"" + source + "\"",
351 354 parsePosition.errorIndex);
352 355 }
353 356 return result;
354 357 }
355 358
356 359 /**
357 360 * Returns true if this format will parse numbers as integers only.
358 361 * For example in the English locale, with ParseIntegerOnly true, the
359 362 * string "1234." would be parsed as the integer value 1234 and parsing
360 363 * would stop at the "." character. Of course, the exact format accepted
361 364 * by the parse operation is locale dependant and determined by sub-classes
362 365 * of NumberFormat.
363 366 */
364 367 public boolean isParseIntegerOnly() {
365 368 return parseIntegerOnly;
366 369 }
367 370
368 371 /**
369 372 * Sets whether or not numbers should be parsed as integers only.
370 373 * @see #isParseIntegerOnly
371 374 */
372 375 public void setParseIntegerOnly(boolean value) {
373 376 parseIntegerOnly = value;
374 377 }
375 378
376 379 //============== Locale Stuff =====================
377 380
378 381 /**
379 382 * Returns a general-purpose number format for the current default locale.
380 383 * This is the same as calling
381 384 * {@link #getNumberInstance() getNumberInstance()}.
382 385 */
383 386 public final static NumberFormat getInstance() {
384 387 return getInstance(Locale.getDefault(Locale.Category.FORMAT), NUMBERSTYLE);
385 388 }
386 389
387 390 /**
388 391 * Returns a general-purpose number format for the specified locale.
389 392 * This is the same as calling
390 393 * {@link #getNumberInstance(java.util.Locale) getNumberInstance(inLocale)}.
391 394 */
392 395 public static NumberFormat getInstance(Locale inLocale) {
393 396 return getInstance(inLocale, NUMBERSTYLE);
394 397 }
395 398
396 399 /**
397 400 * Returns a general-purpose number format for the current default locale.
398 401 */
399 402 public final static NumberFormat getNumberInstance() {
400 403 return getInstance(Locale.getDefault(Locale.Category.FORMAT), NUMBERSTYLE);
401 404 }
402 405
403 406 /**
404 407 * Returns a general-purpose number format for the specified locale.
405 408 */
406 409 public static NumberFormat getNumberInstance(Locale inLocale) {
407 410 return getInstance(inLocale, NUMBERSTYLE);
408 411 }
409 412
410 413 /**
411 414 * Returns an integer number format for the current default locale. The
412 415 * returned number format is configured to round floating point numbers
413 416 * to the nearest integer using half-even rounding (see {@link
414 417 * java.math.RoundingMode#HALF_EVEN RoundingMode.HALF_EVEN}) for formatting,
415 418 * and to parse only the integer part of an input string (see {@link
416 419 * #isParseIntegerOnly isParseIntegerOnly}).
417 420 *
418 421 * @see #getRoundingMode()
419 422 * @return a number format for integer values
420 423 * @since 1.4
421 424 */
422 425 public final static NumberFormat getIntegerInstance() {
423 426 return getInstance(Locale.getDefault(Locale.Category.FORMAT), INTEGERSTYLE);
424 427 }
425 428
426 429 /**
427 430 * Returns an integer number format for the specified locale. The
428 431 * returned number format is configured to round floating point numbers
429 432 * to the nearest integer using half-even rounding (see {@link
430 433 * java.math.RoundingMode#HALF_EVEN RoundingMode.HALF_EVEN}) for formatting,
431 434 * and to parse only the integer part of an input string (see {@link
432 435 * #isParseIntegerOnly isParseIntegerOnly}).
433 436 *
434 437 * @see #getRoundingMode()
435 438 * @return a number format for integer values
436 439 * @since 1.4
437 440 */
438 441 public static NumberFormat getIntegerInstance(Locale inLocale) {
439 442 return getInstance(inLocale, INTEGERSTYLE);
440 443 }
441 444
442 445 /**
443 446 * Returns a currency format for the current default locale.
444 447 */
445 448 public final static NumberFormat getCurrencyInstance() {
446 449 return getInstance(Locale.getDefault(Locale.Category.FORMAT), CURRENCYSTYLE);
447 450 }
448 451
449 452 /**
450 453 * Returns a currency format for the specified locale.
451 454 */
452 455 public static NumberFormat getCurrencyInstance(Locale inLocale) {
453 456 return getInstance(inLocale, CURRENCYSTYLE);
454 457 }
455 458
456 459 /**
457 460 * Returns a percentage format for the current default locale.
458 461 */
459 462 public final static NumberFormat getPercentInstance() {
460 463 return getInstance(Locale.getDefault(Locale.Category.FORMAT), PERCENTSTYLE);
461 464 }
462 465
463 466 /**
464 467 * Returns a percentage format for the specified locale.
465 468 */
466 469 public static NumberFormat getPercentInstance(Locale inLocale) {
467 470 return getInstance(inLocale, PERCENTSTYLE);
468 471 }
469 472
470 473 /**
471 474 * Returns a scientific format for the current default locale.
472 475 */
473 476 /*public*/ final static NumberFormat getScientificInstance() {
474 477 return getInstance(Locale.getDefault(Locale.Category.FORMAT), SCIENTIFICSTYLE);
475 478 }
476 479
477 480 /**
478 481 * Returns a scientific format for the specified locale.
479 482 */
480 483 /*public*/ static NumberFormat getScientificInstance(Locale inLocale) {
481 484 return getInstance(inLocale, SCIENTIFICSTYLE);
482 485 }
483 486
484 487 /**
485 488 * Returns an array of all locales for which the
486 489 * <code>get*Instance</code> methods of this class can return
487 490 * localized instances.
488 491 * The returned array represents the union of locales supported by the Java
489 492 * runtime and by installed
490 493 * {@link java.text.spi.NumberFormatProvider NumberFormatProvider} implementations.
491 494 * It must contain at least a <code>Locale</code> instance equal to
492 495 * {@link java.util.Locale#US Locale.US}.
493 496 *
↓ open down ↓ |
209 lines elided |
↑ open up ↑ |
494 497 * @return An array of locales for which localized
495 498 * <code>NumberFormat</code> instances are available.
496 499 */
497 500 public static Locale[] getAvailableLocales() {
498 501 LocaleServiceProviderPool pool =
499 502 LocaleServiceProviderPool.getPool(NumberFormatProvider.class);
500 503 return pool.getAvailableLocales();
501 504 }
502 505
503 506 /**
504 - * Overrides hashCode
507 + * Overrides hashCode.
505 508 */
509 + @Override
506 510 public int hashCode() {
507 511 return maximumIntegerDigits * 37 + maxFractionDigits;
508 512 // just enough fields for a reasonable distribution
509 513 }
510 514
511 515 /**
512 - * Overrides equals
516 + * Overrides equals.
513 517 */
518 + @Override
514 519 public boolean equals(Object obj) {
515 520 if (obj == null) {
516 521 return false;
517 522 }
518 523 if (this == obj) {
519 524 return true;
520 525 }
521 526 if (getClass() != obj.getClass()) {
522 527 return false;
523 528 }
524 529 NumberFormat other = (NumberFormat) obj;
525 530 return (maximumIntegerDigits == other.maximumIntegerDigits
526 531 && minimumIntegerDigits == other.minimumIntegerDigits
527 532 && maximumFractionDigits == other.maximumFractionDigits
528 533 && minimumFractionDigits == other.minimumFractionDigits
529 534 && groupingUsed == other.groupingUsed
530 535 && parseIntegerOnly == other.parseIntegerOnly);
531 536 }
532 537
533 538 /**
534 - * Overrides Cloneable
539 + * Overrides Cloneable.
535 540 */
541 + @Override
536 542 public Object clone() {
537 543 NumberFormat other = (NumberFormat) super.clone();
538 544 return other;
539 545 }
540 546
541 547 /**
542 548 * Returns true if grouping is used in this format. For example, in the
543 549 * English locale, with grouping on, the number 1234567 might be formatted
544 550 * as "1,234,567". The grouping separator as well as the size of each group
545 551 * is locale dependant and is determined by sub-classes of NumberFormat.
546 552 * @see #setGroupingUsed
547 553 */
548 554 public boolean isGroupingUsed() {
549 555 return groupingUsed;
550 556 }
551 557
552 558 /**
553 559 * Set whether or not grouping will be used in this format.
554 560 * @see #isGroupingUsed
555 561 */
556 562 public void setGroupingUsed(boolean newValue) {
557 563 groupingUsed = newValue;
558 564 }
559 565
560 566 /**
561 567 * Returns the maximum number of digits allowed in the integer portion of a
562 568 * number.
563 569 * @see #setMaximumIntegerDigits
564 570 */
565 571 public int getMaximumIntegerDigits() {
566 572 return maximumIntegerDigits;
567 573 }
568 574
569 575 /**
570 576 * Sets the maximum number of digits allowed in the integer portion of a
571 577 * number. maximumIntegerDigits must be >= minimumIntegerDigits. If the
572 578 * new value for maximumIntegerDigits is less than the current value
573 579 * of minimumIntegerDigits, then minimumIntegerDigits will also be set to
574 580 * the new value.
575 581 * @param newValue the maximum number of integer digits to be shown; if
576 582 * less than zero, then zero is used. The concrete subclass may enforce an
577 583 * upper limit to this value appropriate to the numeric type being formatted.
578 584 * @see #getMaximumIntegerDigits
579 585 */
580 586 public void setMaximumIntegerDigits(int newValue) {
581 587 maximumIntegerDigits = Math.max(0,newValue);
582 588 if (minimumIntegerDigits > maximumIntegerDigits) {
583 589 minimumIntegerDigits = maximumIntegerDigits;
584 590 }
585 591 }
586 592
587 593 /**
588 594 * Returns the minimum number of digits allowed in the integer portion of a
589 595 * number.
590 596 * @see #setMinimumIntegerDigits
591 597 */
592 598 public int getMinimumIntegerDigits() {
593 599 return minimumIntegerDigits;
594 600 }
595 601
596 602 /**
597 603 * Sets the minimum number of digits allowed in the integer portion of a
598 604 * number. minimumIntegerDigits must be <= maximumIntegerDigits. If the
599 605 * new value for minimumIntegerDigits exceeds the current value
600 606 * of maximumIntegerDigits, then maximumIntegerDigits will also be set to
601 607 * the new value
602 608 * @param newValue the minimum number of integer digits to be shown; if
603 609 * less than zero, then zero is used. The concrete subclass may enforce an
604 610 * upper limit to this value appropriate to the numeric type being formatted.
605 611 * @see #getMinimumIntegerDigits
606 612 */
607 613 public void setMinimumIntegerDigits(int newValue) {
608 614 minimumIntegerDigits = Math.max(0,newValue);
609 615 if (minimumIntegerDigits > maximumIntegerDigits) {
610 616 maximumIntegerDigits = minimumIntegerDigits;
611 617 }
612 618 }
613 619
614 620 /**
615 621 * Returns the maximum number of digits allowed in the fraction portion of a
616 622 * number.
617 623 * @see #setMaximumFractionDigits
618 624 */
619 625 public int getMaximumFractionDigits() {
620 626 return maximumFractionDigits;
621 627 }
622 628
623 629 /**
624 630 * Sets the maximum number of digits allowed in the fraction portion of a
625 631 * number. maximumFractionDigits must be >= minimumFractionDigits. If the
626 632 * new value for maximumFractionDigits is less than the current value
627 633 * of minimumFractionDigits, then minimumFractionDigits will also be set to
628 634 * the new value.
629 635 * @param newValue the maximum number of fraction digits to be shown; if
630 636 * less than zero, then zero is used. The concrete subclass may enforce an
631 637 * upper limit to this value appropriate to the numeric type being formatted.
632 638 * @see #getMaximumFractionDigits
633 639 */
634 640 public void setMaximumFractionDigits(int newValue) {
635 641 maximumFractionDigits = Math.max(0,newValue);
636 642 if (maximumFractionDigits < minimumFractionDigits) {
637 643 minimumFractionDigits = maximumFractionDigits;
638 644 }
639 645 }
640 646
641 647 /**
642 648 * Returns the minimum number of digits allowed in the fraction portion of a
643 649 * number.
644 650 * @see #setMinimumFractionDigits
645 651 */
646 652 public int getMinimumFractionDigits() {
647 653 return minimumFractionDigits;
648 654 }
649 655
650 656 /**
651 657 * Sets the minimum number of digits allowed in the fraction portion of a
652 658 * number. minimumFractionDigits must be <= maximumFractionDigits. If the
653 659 * new value for minimumFractionDigits exceeds the current value
654 660 * of maximumFractionDigits, then maximumIntegerDigits will also be set to
655 661 * the new value
656 662 * @param newValue the minimum number of fraction digits to be shown; if
657 663 * less than zero, then zero is used. The concrete subclass may enforce an
658 664 * upper limit to this value appropriate to the numeric type being formatted.
659 665 * @see #getMinimumFractionDigits
660 666 */
661 667 public void setMinimumFractionDigits(int newValue) {
662 668 minimumFractionDigits = Math.max(0,newValue);
663 669 if (maximumFractionDigits < minimumFractionDigits) {
664 670 maximumFractionDigits = minimumFractionDigits;
665 671 }
666 672 }
667 673
668 674 /**
669 675 * Gets the currency used by this number format when formatting
670 676 * currency values. The initial value is derived in a locale dependent
671 677 * way. The returned value may be null if no valid
672 678 * currency could be determined and no currency has been set using
673 679 * {@link #setCurrency(java.util.Currency) setCurrency}.
674 680 * <p>
675 681 * The default implementation throws
676 682 * <code>UnsupportedOperationException</code>.
677 683 *
678 684 * @return the currency used by this number format, or <code>null</code>
679 685 * @exception UnsupportedOperationException if the number format class
680 686 * doesn't implement currency formatting
681 687 * @since 1.4
682 688 */
683 689 public Currency getCurrency() {
684 690 throw new UnsupportedOperationException();
685 691 }
686 692
687 693 /**
688 694 * Sets the currency used by this number format when formatting
689 695 * currency values. This does not update the minimum or maximum
690 696 * number of fraction digits used by the number format.
691 697 * <p>
692 698 * The default implementation throws
693 699 * <code>UnsupportedOperationException</code>.
694 700 *
695 701 * @param currency the new currency to be used by this number format
696 702 * @exception UnsupportedOperationException if the number format class
697 703 * doesn't implement currency formatting
698 704 * @exception NullPointerException if <code>currency</code> is null
699 705 * @since 1.4
700 706 */
701 707 public void setCurrency(Currency currency) {
702 708 throw new UnsupportedOperationException();
703 709 }
704 710
705 711 /**
706 712 * Gets the {@link java.math.RoundingMode} used in this NumberFormat.
707 713 * The default implementation of this method in NumberFormat
708 714 * always throws {@link java.lang.UnsupportedOperationException}.
709 715 * Subclasses which handle different rounding modes should override
710 716 * this method.
711 717 *
712 718 * @exception UnsupportedOperationException The default implementation
713 719 * always throws this exception
714 720 * @return The <code>RoundingMode</code> used for this NumberFormat.
715 721 * @see #setRoundingMode(RoundingMode)
716 722 * @since 1.6
717 723 */
718 724 public RoundingMode getRoundingMode() {
719 725 throw new UnsupportedOperationException();
720 726 }
721 727
722 728 /**
723 729 * Sets the {@link java.math.RoundingMode} used in this NumberFormat.
724 730 * The default implementation of this method in NumberFormat always
725 731 * throws {@link java.lang.UnsupportedOperationException}.
726 732 * Subclasses which handle different rounding modes should override
727 733 * this method.
728 734 *
729 735 * @exception UnsupportedOperationException The default implementation
730 736 * always throws this exception
731 737 * @exception NullPointerException if <code>roundingMode</code> is null
732 738 * @param roundingMode The <code>RoundingMode</code> to be used
733 739 * @see #getRoundingMode()
↓ open down ↓ |
188 lines elided |
↑ open up ↑ |
734 740 * @since 1.6
735 741 */
736 742 public void setRoundingMode(RoundingMode roundingMode) {
737 743 throw new UnsupportedOperationException();
738 744 }
739 745
740 746 // =======================privates===============================
741 747
742 748 private static NumberFormat getInstance(Locale desiredLocale,
743 749 int choice) {
744 - // Check whether a provider can provide an implementation that's closer
745 - // to the requested locale than what the Java runtime itself can provide.
746 - LocaleServiceProviderPool pool =
747 - LocaleServiceProviderPool.getPool(NumberFormatProvider.class);
748 - if (pool.hasProviders()) {
749 - NumberFormat providersInstance = pool.getLocalizedObject(
750 - NumberFormatGetter.INSTANCE,
751 - desiredLocale,
752 - choice);
753 - if (providersInstance != null) {
754 - return providersInstance;
755 - }
750 + LocaleProviderAdapter adapter;
751 + adapter = LocaleProviderAdapter.getAdapter(NumberFormatProvider.class,
752 + desiredLocale);
753 + NumberFormat numberFormat = getInstance(adapter, desiredLocale, choice);
754 + if (numberFormat == null) {
755 + numberFormat = getInstance(LocaleProviderAdapter.forJRE(),
756 + desiredLocale, choice);
756 757 }
758 + return numberFormat;
759 + }
757 760
758 - /* try the cache first */
759 - String[] numberPatterns = cachedLocaleData.get(desiredLocale);
760 - if (numberPatterns == null) { /* cache miss */
761 - ResourceBundle resource = LocaleData.getNumberFormatData(desiredLocale);
762 - numberPatterns = resource.getStringArray("NumberPatterns");
763 - /* update cache */
764 - cachedLocaleData.put(desiredLocale, numberPatterns);
761 + private static NumberFormat getInstance(LocaleProviderAdapter adapter,
762 + Locale locale, int choice) {
763 + NumberFormatProvider provider = adapter.getNumberFormatProvider();
764 + NumberFormat numberFormat = null;
765 + switch (choice) {
766 + case NUMBERSTYLE:
767 + numberFormat = provider.getNumberInstance(locale);
768 + break;
769 + case PERCENTSTYLE:
770 + numberFormat = provider.getPercentInstance(locale);
771 + break;
772 + case CURRENCYSTYLE:
773 + numberFormat = provider.getCurrencyInstance(locale);
774 + break;
775 + case INTEGERSTYLE:
776 + numberFormat = provider.getIntegerInstance(locale);
777 + break;
765 778 }
766 -
767 - DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(desiredLocale);
768 - int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice;
769 - DecimalFormat format = new DecimalFormat(numberPatterns[entry], symbols);
770 -
771 - if (choice == INTEGERSTYLE) {
772 - format.setMaximumFractionDigits(0);
773 - format.setDecimalSeparatorAlwaysShown(false);
774 - format.setParseIntegerOnly(true);
775 - } else if (choice == CURRENCYSTYLE) {
776 - format.adjustForCurrencyDefaultFractionDigits();
777 - }
778 -
779 - return format;
779 + return numberFormat;
780 780 }
781 781
782 782 /**
783 783 * First, read in the default serializable data.
784 784 *
785 785 * Then, if <code>serialVersionOnStream</code> is less than 1, indicating that
786 786 * the stream was written by JDK 1.1,
787 787 * set the <code>int</code> fields such as <code>maximumIntegerDigits</code>
788 788 * to be equal to the <code>byte</code> fields such as <code>maxIntegerDigits</code>,
789 789 * since the <code>int</code> fields were not present in JDK 1.1.
790 790 * Finally, set serialVersionOnStream back to the maximum allowed value so that
791 791 * default serialization will work properly if this object is streamed out again.
792 792 *
793 793 * <p>If <code>minimumIntegerDigits</code> is greater than
794 794 * <code>maximumIntegerDigits</code> or <code>minimumFractionDigits</code>
795 795 * is greater than <code>maximumFractionDigits</code>, then the stream data
796 796 * is invalid and this method throws an <code>InvalidObjectException</code>.
797 797 * In addition, if any of these values is negative, then this method throws
798 798 * an <code>InvalidObjectException</code>.
799 799 *
800 800 * @since 1.2
801 801 */
802 802 private void readObject(ObjectInputStream stream)
803 803 throws IOException, ClassNotFoundException
804 804 {
805 805 stream.defaultReadObject();
806 806 if (serialVersionOnStream < 1) {
807 807 // Didn't have additional int fields, reassign to use them.
808 808 maximumIntegerDigits = maxIntegerDigits;
809 809 minimumIntegerDigits = minIntegerDigits;
810 810 maximumFractionDigits = maxFractionDigits;
811 811 minimumFractionDigits = minFractionDigits;
812 812 }
813 813 if (minimumIntegerDigits > maximumIntegerDigits ||
814 814 minimumFractionDigits > maximumFractionDigits ||
815 815 minimumIntegerDigits < 0 || minimumFractionDigits < 0) {
816 816 throw new InvalidObjectException("Digit count range invalid");
817 817 }
818 818 serialVersionOnStream = currentSerialVersion;
819 819 }
820 820
821 821 /**
822 822 * Write out the default serializable data, after first setting
823 823 * the <code>byte</code> fields such as <code>maxIntegerDigits</code> to be
824 824 * equal to the <code>int</code> fields such as <code>maximumIntegerDigits</code>
825 825 * (or to <code>Byte.MAX_VALUE</code>, whichever is smaller), for compatibility
826 826 * with the JDK 1.1 version of the stream format.
827 827 *
828 828 * @since 1.2
829 829 */
830 830 private void writeObject(ObjectOutputStream stream)
831 831 throws IOException
832 832 {
833 833 maxIntegerDigits = (maximumIntegerDigits > Byte.MAX_VALUE) ?
↓ open down ↓ |
44 lines elided |
↑ open up ↑ |
834 834 Byte.MAX_VALUE : (byte)maximumIntegerDigits;
835 835 minIntegerDigits = (minimumIntegerDigits > Byte.MAX_VALUE) ?
836 836 Byte.MAX_VALUE : (byte)minimumIntegerDigits;
837 837 maxFractionDigits = (maximumFractionDigits > Byte.MAX_VALUE) ?
838 838 Byte.MAX_VALUE : (byte)maximumFractionDigits;
839 839 minFractionDigits = (minimumFractionDigits > Byte.MAX_VALUE) ?
840 840 Byte.MAX_VALUE : (byte)minimumFractionDigits;
841 841 stream.defaultWriteObject();
842 842 }
843 843
844 - /**
845 - * Cache to hold the NumberPatterns of a Locale.
846 - */
847 - private static final Hashtable<Locale, String[]> cachedLocaleData = new Hashtable<>(3);
848 -
849 844 // Constants used by factory methods to specify a style of format.
850 845 private static final int NUMBERSTYLE = 0;
851 846 private static final int CURRENCYSTYLE = 1;
852 847 private static final int PERCENTSTYLE = 2;
853 848 private static final int SCIENTIFICSTYLE = 3;
854 849 private static final int INTEGERSTYLE = 4;
855 850
856 851 /**
857 852 * True if the grouping (i.e. thousands) separator is used when
858 853 * formatting and parsing numbers.
859 854 *
860 855 * @serial
861 856 * @see #isGroupingUsed
862 857 */
863 858 private boolean groupingUsed = true;
864 859
865 860 /**
866 861 * The maximum number of digits allowed in the integer portion of a
867 862 * number. <code>maxIntegerDigits</code> must be greater than or equal to
868 863 * <code>minIntegerDigits</code>.
869 864 * <p>
870 865 * <strong>Note:</strong> This field exists only for serialization
871 866 * compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new
872 867 * <code>int</code> field <code>maximumIntegerDigits</code> is used instead.
873 868 * When writing to a stream, <code>maxIntegerDigits</code> is set to
874 869 * <code>maximumIntegerDigits</code> or <code>Byte.MAX_VALUE</code>,
875 870 * whichever is smaller. When reading from a stream, this field is used
876 871 * only if <code>serialVersionOnStream</code> is less than 1.
877 872 *
878 873 * @serial
879 874 * @see #getMaximumIntegerDigits
880 875 */
881 876 private byte maxIntegerDigits = 40;
882 877
883 878 /**
884 879 * The minimum number of digits allowed in the integer portion of a
885 880 * number. <code>minimumIntegerDigits</code> must be less than or equal to
886 881 * <code>maximumIntegerDigits</code>.
887 882 * <p>
888 883 * <strong>Note:</strong> This field exists only for serialization
889 884 * compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new
890 885 * <code>int</code> field <code>minimumIntegerDigits</code> is used instead.
891 886 * When writing to a stream, <code>minIntegerDigits</code> is set to
892 887 * <code>minimumIntegerDigits</code> or <code>Byte.MAX_VALUE</code>,
893 888 * whichever is smaller. When reading from a stream, this field is used
894 889 * only if <code>serialVersionOnStream</code> is less than 1.
895 890 *
896 891 * @serial
897 892 * @see #getMinimumIntegerDigits
898 893 */
899 894 private byte minIntegerDigits = 1;
900 895
901 896 /**
902 897 * The maximum number of digits allowed in the fractional portion of a
903 898 * number. <code>maximumFractionDigits</code> must be greater than or equal to
904 899 * <code>minimumFractionDigits</code>.
905 900 * <p>
906 901 * <strong>Note:</strong> This field exists only for serialization
907 902 * compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new
908 903 * <code>int</code> field <code>maximumFractionDigits</code> is used instead.
909 904 * When writing to a stream, <code>maxFractionDigits</code> is set to
910 905 * <code>maximumFractionDigits</code> or <code>Byte.MAX_VALUE</code>,
911 906 * whichever is smaller. When reading from a stream, this field is used
912 907 * only if <code>serialVersionOnStream</code> is less than 1.
913 908 *
914 909 * @serial
915 910 * @see #getMaximumFractionDigits
916 911 */
917 912 private byte maxFractionDigits = 3; // invariant, >= minFractionDigits
918 913
919 914 /**
920 915 * The minimum number of digits allowed in the fractional portion of a
921 916 * number. <code>minimumFractionDigits</code> must be less than or equal to
922 917 * <code>maximumFractionDigits</code>.
923 918 * <p>
924 919 * <strong>Note:</strong> This field exists only for serialization
925 920 * compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new
926 921 * <code>int</code> field <code>minimumFractionDigits</code> is used instead.
927 922 * When writing to a stream, <code>minFractionDigits</code> is set to
928 923 * <code>minimumFractionDigits</code> or <code>Byte.MAX_VALUE</code>,
929 924 * whichever is smaller. When reading from a stream, this field is used
930 925 * only if <code>serialVersionOnStream</code> is less than 1.
931 926 *
932 927 * @serial
933 928 * @see #getMinimumFractionDigits
934 929 */
935 930 private byte minFractionDigits = 0;
936 931
937 932 /**
938 933 * True if this format will parse numbers as integers only.
939 934 *
940 935 * @serial
941 936 * @see #isParseIntegerOnly
942 937 */
943 938 private boolean parseIntegerOnly = false;
944 939
945 940 // new fields for 1.2. byte is too small for integer digits.
946 941
947 942 /**
948 943 * The maximum number of digits allowed in the integer portion of a
949 944 * number. <code>maximumIntegerDigits</code> must be greater than or equal to
950 945 * <code>minimumIntegerDigits</code>.
951 946 *
952 947 * @serial
953 948 * @since 1.2
954 949 * @see #getMaximumIntegerDigits
955 950 */
956 951 private int maximumIntegerDigits = 40;
957 952
958 953 /**
959 954 * The minimum number of digits allowed in the integer portion of a
960 955 * number. <code>minimumIntegerDigits</code> must be less than or equal to
961 956 * <code>maximumIntegerDigits</code>.
962 957 *
963 958 * @serial
964 959 * @since 1.2
965 960 * @see #getMinimumIntegerDigits
966 961 */
967 962 private int minimumIntegerDigits = 1;
968 963
969 964 /**
970 965 * The maximum number of digits allowed in the fractional portion of a
971 966 * number. <code>maximumFractionDigits</code> must be greater than or equal to
972 967 * <code>minimumFractionDigits</code>.
973 968 *
974 969 * @serial
975 970 * @since 1.2
976 971 * @see #getMaximumFractionDigits
977 972 */
978 973 private int maximumFractionDigits = 3; // invariant, >= minFractionDigits
979 974
980 975 /**
981 976 * The minimum number of digits allowed in the fractional portion of a
982 977 * number. <code>minimumFractionDigits</code> must be less than or equal to
983 978 * <code>maximumFractionDigits</code>.
984 979 *
985 980 * @serial
986 981 * @since 1.2
987 982 * @see #getMinimumFractionDigits
988 983 */
989 984 private int minimumFractionDigits = 0;
990 985
991 986 static final int currentSerialVersion = 1;
992 987
993 988 /**
994 989 * Describes the version of <code>NumberFormat</code> present on the stream.
995 990 * Possible values are:
996 991 * <ul>
997 992 * <li><b>0</b> (or uninitialized): the JDK 1.1 version of the stream format.
998 993 * In this version, the <code>int</code> fields such as
999 994 * <code>maximumIntegerDigits</code> were not present, and the <code>byte</code>
1000 995 * fields such as <code>maxIntegerDigits</code> are used instead.
1001 996 *
1002 997 * <li><b>1</b>: the 1.2 version of the stream format. The values of the
1003 998 * <code>byte</code> fields such as <code>maxIntegerDigits</code> are ignored,
1004 999 * and the <code>int</code> fields such as <code>maximumIntegerDigits</code>
1005 1000 * are used instead.
1006 1001 * </ul>
1007 1002 * When streaming out a <code>NumberFormat</code>, the most recent format
1008 1003 * (corresponding to the highest allowable <code>serialVersionOnStream</code>)
1009 1004 * is always written.
1010 1005 *
1011 1006 * @serial
1012 1007 * @since 1.2
1013 1008 */
1014 1009 private int serialVersionOnStream = currentSerialVersion;
1015 1010
1016 1011 // Removed "implements Cloneable" clause. Needs to update serialization
1017 1012 // ID for backward compatibility.
1018 1013 static final long serialVersionUID = -2308460125733713944L;
1019 1014
1020 1015
1021 1016 //
1022 1017 // class for AttributedCharacterIterator attributes
1023 1018 //
1024 1019 /**
1025 1020 * Defines constants that are used as attribute keys in the
1026 1021 * <code>AttributedCharacterIterator</code> returned
1027 1022 * from <code>NumberFormat.formatToCharacterIterator</code> and as
1028 1023 * field identifiers in <code>FieldPosition</code>.
1029 1024 *
1030 1025 * @since 1.4
1031 1026 */
1032 1027 public static class Field extends Format.Field {
1033 1028
1034 1029 // Proclaim serial compatibility with 1.4 FCS
1035 1030 private static final long serialVersionUID = 7494728892700160890L;
1036 1031
1037 1032 // table of all instances in this class, used by readResolve
1038 1033 private static final Map<String, Field> instanceMap = new HashMap<>(11);
1039 1034
1040 1035 /**
1041 1036 * Creates a Field instance with the specified
1042 1037 * name.
1043 1038 *
1044 1039 * @param name Name of the attribute
1045 1040 */
1046 1041 protected Field(String name) {
1047 1042 super(name);
1048 1043 if (this.getClass() == NumberFormat.Field.class) {
↓ open down ↓ |
190 lines elided |
↑ open up ↑ |
1049 1044 instanceMap.put(name, this);
1050 1045 }
1051 1046 }
1052 1047
1053 1048 /**
1054 1049 * Resolves instances being deserialized to the predefined constants.
1055 1050 *
1056 1051 * @throws InvalidObjectException if the constant could not be resolved.
1057 1052 * @return resolved NumberFormat.Field constant
1058 1053 */
1054 + @Override
1059 1055 protected Object readResolve() throws InvalidObjectException {
1060 1056 if (this.getClass() != NumberFormat.Field.class) {
1061 1057 throw new InvalidObjectException("subclass didn't correctly implement readResolve");
1062 1058 }
1063 1059
1064 1060 Object instance = instanceMap.get(getName());
1065 1061 if (instance != null) {
1066 1062 return instance;
1067 1063 } else {
1068 1064 throw new InvalidObjectException("unknown attribute name");
1069 1065 }
1070 1066 }
1071 1067
1072 1068 /**
1073 1069 * Constant identifying the integer field.
1074 1070 */
1075 1071 public static final Field INTEGER = new Field("integer");
1076 1072
1077 1073 /**
1078 1074 * Constant identifying the fraction field.
1079 1075 */
1080 1076 public static final Field FRACTION = new Field("fraction");
1081 1077
1082 1078 /**
1083 1079 * Constant identifying the exponent field.
1084 1080 */
1085 1081 public static final Field EXPONENT = new Field("exponent");
1086 1082
1087 1083 /**
1088 1084 * Constant identifying the decimal separator field.
1089 1085 */
1090 1086 public static final Field DECIMAL_SEPARATOR =
1091 1087 new Field("decimal separator");
1092 1088
1093 1089 /**
1094 1090 * Constant identifying the sign field.
1095 1091 */
1096 1092 public static final Field SIGN = new Field("sign");
1097 1093
1098 1094 /**
1099 1095 * Constant identifying the grouping separator field.
1100 1096 */
1101 1097 public static final Field GROUPING_SEPARATOR =
1102 1098 new Field("grouping separator");
1103 1099
1104 1100 /**
1105 1101 * Constant identifying the exponent symbol field.
1106 1102 */
1107 1103 public static final Field EXPONENT_SYMBOL = new
1108 1104 Field("exponent symbol");
1109 1105
1110 1106 /**
1111 1107 * Constant identifying the percent field.
1112 1108 */
1113 1109 public static final Field PERCENT = new Field("percent");
1114 1110
1115 1111 /**
1116 1112 * Constant identifying the permille field.
1117 1113 */
1118 1114 public static final Field PERMILLE = new Field("per mille");
1119 1115
↓ open down ↓ |
51 lines elided |
↑ open up ↑ |
1120 1116 /**
1121 1117 * Constant identifying the currency field.
1122 1118 */
1123 1119 public static final Field CURRENCY = new Field("currency");
1124 1120
1125 1121 /**
1126 1122 * Constant identifying the exponent sign field.
1127 1123 */
1128 1124 public static final Field EXPONENT_SIGN = new Field("exponent sign");
1129 1125 }
1130 -
1131 - /**
1132 - * Obtains a NumberFormat instance from a NumberFormatProvider implementation.
1133 - */
1134 - private static class NumberFormatGetter
1135 - implements LocaleServiceProviderPool.LocalizedObjectGetter<NumberFormatProvider,
1136 - NumberFormat> {
1137 - private static final NumberFormatGetter INSTANCE = new NumberFormatGetter();
1138 -
1139 - public NumberFormat getObject(NumberFormatProvider numberFormatProvider,
1140 - Locale locale,
1141 - String key,
1142 - Object... params) {
1143 - assert params.length == 1;
1144 - int choice = (Integer)params[0];
1145 -
1146 - switch (choice) {
1147 - case NUMBERSTYLE:
1148 - return numberFormatProvider.getNumberInstance(locale);
1149 - case PERCENTSTYLE:
1150 - return numberFormatProvider.getPercentInstance(locale);
1151 - case CURRENCYSTYLE:
1152 - return numberFormatProvider.getCurrencyInstance(locale);
1153 - case INTEGERSTYLE:
1154 - return numberFormatProvider.getIntegerInstance(locale);
1155 - default:
1156 - assert false : choice;
1157 - }
1158 -
1159 - return null;
1160 - }
1161 - }
1162 1126 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX