source: trunk/gcc/libjava/java/text/DecimalFormatSymbols.java

Last change on this file was 2, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 15.0 KB
Line 
1/* DecimalFormatSymbols.java -- Format symbols used by DecimalFormat
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.text;
40
41import java.io.Serializable;
42import java.util.Locale;
43import java.util.MissingResourceException;
44import java.util.ResourceBundle;
45import java.io.ObjectInputStream;
46import java.io.IOException;
47
48/**
49 * This class is a container for the symbols used by
50 * <code>DecimalFormat</code> to format numbers and currency. These are
51 * normally handled automatically, but an application can override
52 * values as desired using this class.
53 *
54 * @author Tom Tromey <tromey@cygnus.com>
55 * @author Aaron M. Renn (arenn@urbanophile.com)
56 * @date February 24, 1999
57 */
58/* Written using "Java Class Libraries", 2nd edition, plus online
59 * API docs for JDK 1.2 from http://www.javasoft.com.
60 * Status: Believed complete and correct to 1.2.
61 */
62public final class DecimalFormatSymbols implements Cloneable, Serializable
63{
64 public Object clone ()
65 {
66 try
67 {
68 return super.clone ();
69 }
70 catch(CloneNotSupportedException e)
71 {
72 return null;
73 }
74 }
75
76 /**
77 * This method initializes a new instance of
78 * <code>DecimalFormatSymbols</code> for the default locale.
79 */
80 public DecimalFormatSymbols ()
81 {
82 this (Locale.getDefault());
83 }
84
85 private final String safeGetString (ResourceBundle bundle,
86 String name, String def)
87 {
88 if (bundle != null)
89 {
90 try
91 {
92 return bundle.getString(name);
93 }
94 catch (MissingResourceException x)
95 {
96 }
97 }
98 return def;
99 }
100
101 private final char safeGetChar (ResourceBundle bundle,
102 String name, char def)
103 {
104 String r = null;
105 if (bundle != null)
106 {
107 try
108 {
109 r = bundle.getString(name);
110 }
111 catch (MissingResourceException x)
112 {
113 }
114 }
115 if (r == null || r.length() < 1)
116 return def;
117 return r.charAt(0);
118 }
119
120 /**
121 * This method initializes a new instance of
122 * <code>DecimalFormatSymbols</code> for the specified locale.
123 *
124 * @param locale The local to load symbols for.
125 */
126 public DecimalFormatSymbols (Locale loc)
127 {
128 ResourceBundle res;
129 try
130 {
131 res = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
132 loc);
133 }
134 catch (MissingResourceException x)
135 {
136 res = null;
137 }
138 currencySymbol = safeGetString (res, "currencySymbol", "$");
139 decimalSeparator = safeGetChar (res, "decimalSeparator", '.');
140 digit = safeGetChar (res, "digit", '#');
141 exponential = safeGetChar (res, "exponential", 'E');
142 groupingSeparator = safeGetChar (res, "groupingSeparator", ',');
143 infinity = safeGetString (res, "infinity", "\u221e");
144 // FIXME: default?
145 intlCurrencySymbol = safeGetString (res, "intlCurrencySymbol", "$");
146 try
147 {
148 monetarySeparator = safeGetChar (res, "monetarySeparator", '.');
149 }
150 catch (MissingResourceException x)
151 {
152 monetarySeparator = decimalSeparator;
153 }
154 minusSign = safeGetChar (res, "minusSign", '-');
155 NaN = safeGetString (res, "NaN", "\ufffd");
156 patternSeparator = safeGetChar (res, "patternSeparator", ';');
157 percent = safeGetChar (res, "percent", '%');
158 perMill = safeGetChar (res, "perMill", '\u2030');
159 zeroDigit = safeGetChar (res, "zeroDigit", '0');
160 }
161
162 /**
163 * This method this this object for equality against the specified object.
164 * This will be true if and only if the following criteria are met with
165 * regard to the specified object:
166 * <p>
167 * <ul>
168 * <li>It is not <code>null</code>.
169 * <li>It is an instance of <code>DecimalFormatSymbols</code>
170 * <li>All of its symbols are identical to the symbols in this object.
171 * </ul>
172 *
173 * @return <code>true</code> if the specified object is equal to this
174 * object, <code>false</code> otherwise.
175 */
176 public boolean equals (Object obj)
177 {
178 if (! (obj instanceof DecimalFormatSymbols))
179 return false;
180 DecimalFormatSymbols dfs = (DecimalFormatSymbols) obj;
181 return (currencySymbol.equals(dfs.currencySymbol)
182 && decimalSeparator == dfs.decimalSeparator
183 && digit == dfs.digit
184 && exponential == dfs.exponential
185 && groupingSeparator == dfs.groupingSeparator
186 && infinity.equals(dfs.infinity)
187 && intlCurrencySymbol.equals(dfs.intlCurrencySymbol)
188 && minusSign == dfs.minusSign
189 && monetarySeparator == dfs.monetarySeparator
190 && NaN.equals(dfs.NaN)
191 && patternSeparator == dfs.patternSeparator
192 && percent == dfs.percent
193 && perMill == dfs.perMill
194 && zeroDigit == dfs.zeroDigit);
195 }
196
197 /**
198 * This method returns the currency symbol in local format. For example,
199 * "$" for Canadian dollars.
200 *
201 * @return The currency symbol in local format.
202 */
203 public String getCurrencySymbol ()
204 {
205 return currencySymbol;
206 }
207
208 /**
209 * This method returns the character used as the decimal point.
210 *
211 * @return The character used as the decimal point.
212 */
213 public char getDecimalSeparator ()
214 {
215 return decimalSeparator;
216 }
217
218 /**
219 * This method returns the character used to represent a digit in a
220 * format pattern string.
221 *
222 * @return The character used to represent a digit in a format
223 * pattern string.
224 */
225 public char getDigit ()
226 {
227 return digit;
228 }
229
230 // This is our own extension.
231 char getExponential ()
232 {
233 return exponential;
234 }
235
236 /**
237 * This method sets the character used to separate groups of digits. For
238 * example, the United States uses a comma (,) to separate thousands in
239 * a number.
240 *
241 * @return The character used to separate groups of digits.
242 */
243 public char getGroupingSeparator ()
244 {
245 return groupingSeparator;
246 }
247
248 /**
249 * This method returns the character used to represent infinity.
250 *
251 * @return The character used to represent infinity.
252 */
253 public String getInfinity ()
254 {
255 return infinity;
256 }
257
258 /**
259 * This method returns the currency symbol in international format. For
260 * example, "C$" for Canadian dollars.
261 *
262 * @return The currency symbol in international format.
263 */
264 public String getInternationalCurrencySymbol ()
265 {
266 return intlCurrencySymbol;
267 }
268
269 /**
270 * This method returns the character used to represent the minus sign.
271 *
272 * @return The character used to represent the minus sign.
273 */
274 public char getMinusSign ()
275 {
276 return minusSign;
277 }
278
279 /**
280 * This method returns the character used to represent the decimal
281 * point for currency values.
282 *
283 * @return The decimal point character used in currency values.
284 */
285 public char getMonetaryDecimalSeparator ()
286 {
287 return monetarySeparator;
288 }
289
290 /**
291 * This method returns the string used to represent the NaN (not a number)
292 * value.
293 *
294 * @return The string used to represent NaN
295 */
296 public String getNaN ()
297 {
298 return NaN;
299 }
300
301 /**
302 * This method returns the character used to separate positive and negative
303 * subpatterns in a format pattern.
304 *
305 * @return The character used to separate positive and negative subpatterns
306 * in a format pattern.
307 */
308 public char getPatternSeparator ()
309 {
310 return patternSeparator;
311 }
312
313 /**
314 * This method returns the character used as the percent sign.
315 *
316 * @return The character used as the percent sign.
317 */
318 public char getPercent ()
319 {
320 return percent;
321 }
322
323 /**
324 * This method returns the character used as the per mille character.
325 *
326 * @return The per mille character.
327 */
328 public char getPerMill ()
329 {
330 return perMill;
331 }
332
333 /**
334 * This method returns the character used to represent the digit zero.
335 *
336 * @return The character used to represent the digit zero.
337 */
338 public char getZeroDigit ()
339 {
340 return zeroDigit;
341 }
342
343 /**
344 * This method returns a hash value for this object.
345 *
346 * @return A hash value for this object.
347 */
348 public int hashCode ()
349 {
350 // Compute based on zero digit, grouping separator, and decimal
351 // separator -- JCL book. This probably isn't a very good hash
352 // code.
353 return zeroDigit << 16 + groupingSeparator << 8 + decimalSeparator;
354 }
355
356 /**
357 * This method sets the currency symbol to the specified value.
358 *
359 * @param currencySymbol The new currency symbol
360 */
361 public void setCurrencySymbol (String currency)
362 {
363 currencySymbol = currency;
364 }
365
366 /**
367 * This method sets the decimal point character to the specified value.
368 *
369 * @param decimalSeparator The new decimal point character
370 */
371 public void setDecimalSeparator (char decimalSep)
372 {
373 decimalSeparator = decimalSep;
374 }
375
376 /**
377 * This method sets the character used to represents a digit in a format
378 * string to the specified value.
379 *
380 * @param digit The character used to represent a digit in a format pattern.
381 */
382 public void setDigit (char digit)
383 {
384 this.digit = digit;
385 }
386
387 // This is our own extension.
388 void setExponential (char exp)
389 {
390 exponential = exp;
391 }
392
393 /**
394 * This method sets the character used to separate groups of digits.
395 *
396 * @param groupingSeparator The character used to separate groups of digits.
397 */
398 public void setGroupingSeparator (char groupSep)
399 {
400 groupingSeparator = groupSep;
401 }
402
403 /**
404 * This method sets the string used to represents infinity.
405 *
406 * @param infinity The string used to represent infinity.
407 */
408 public void setInfinity (String infinity)
409 {
410 this.infinity = infinity;
411 }
412
413 /**
414 * This method sets the international currency symbols to the
415 * specified value.
416 *
417 * @param intlCurrencySymbol The new international currency symbol.
418 */
419 public void setInternationalCurrencySymbol (String currency)
420 {
421 intlCurrencySymbol = currency;
422 }
423
424 /**
425 * This method sets the character used to represent the minus sign.
426 *
427 * @param minusSign The character used to represent the minus sign.
428 */
429 public void setMinusSign (char minusSign)
430 {
431 this.minusSign = minusSign;
432 }
433
434 /**
435 * This method sets the character used for the decimal point in currency
436 * values.
437 *
438 * @param monetarySeparator The decimal point character used in
439 * currency values.
440 */
441 public void setMonetaryDecimalSeparator (char decimalSep)
442 {
443 monetarySeparator = decimalSep;
444 }
445
446 /**
447 * This method sets the string used to represent the NaN (not a
448 * number) value.
449 *
450 * @param NaN The string used to represent NaN
451 */
452 public void setNaN (String nan)
453 {
454 NaN = nan;
455 }
456
457 /**
458 * This method sets the character used to separate positive and negative
459 * subpatterns in a format pattern.
460 *
461 * @param patternSeparator The character used to separate positive and
462 * negative subpatterns in a format pattern.
463 */
464 public void setPatternSeparator (char patternSep)
465 {
466 patternSeparator = patternSep;
467 }
468
469 /**
470 * This method sets the character used as the percent sign.
471 *
472 * @param percent The character used as the percent sign.
473 */
474 public void setPercent (char percent)
475 {
476 this.percent = percent;
477 }
478
479 /**
480 * This method sets the character used as the per mille character.
481 *
482 * @param perMill The per mille character.
483 */
484 public void setPerMill (char perMill)
485 {
486 this.perMill = perMill;
487 }
488
489 /**
490 * This method sets the character used to represent the digit zero.
491 *
492 * @param zeroDigit The character used to represent the digit zero.
493 */
494 public void setZeroDigit (char zeroDigit)
495 {
496 this.zeroDigit = zeroDigit;
497 }
498
499 /**
500 * @serial A string used for the local currency
501 */
502 private String currencySymbol;
503 /**
504 * @serial The <code>char</code> used to separate decimals in a number.
505 */
506 private char decimalSeparator;
507 /**
508 * @serial This is the <code>char</code> used to represent a digit in
509 * a format specification.
510 */
511 private char digit;
512 /**
513 * @serial This is the <code>char</code> used to represent the exponent
514 * separator in exponential notation.
515 */
516 private char exponential;
517 /**
518 * @serial This separates groups of thousands in numbers.
519 */
520 private char groupingSeparator;
521 /**
522 * @serial This string represents infinity.
523 */
524 private String infinity;
525 /**
526 * @serial This string represents the local currency in an international
527 * context, eg, "C$" for Canadian dollars.
528 */
529 private String intlCurrencySymbol;
530 /**
531 * @serial This is the character used to represent the minus sign.
532 */
533 private char minusSign;
534 /**
535 * @serial This character is used to separate decimals when formatting
536 * currency values.
537 */
538 private char monetarySeparator;
539 /**
540 * @serial This string is used the represent the Java NaN value for
541 * "not a number".
542 */
543 private String NaN;
544 /**
545 * @serial This is the character used to separate positive and negative
546 * subpatterns in a format pattern.
547 */
548 private char patternSeparator;
549 /**
550 * @serial This is the percent symbols
551 */
552 private char percent;
553 /**
554 * @serial This character is used for the mille percent sign.
555 */
556 private char perMill;
557 /**
558 * @serial This value represents the type of object being de-serialized.
559 * 0 indicates a pre-Java 1.1.6 version, 1 indicates 1.1.6 or later.
560 */
561 private int serialVersionOnStream = 1;
562 /**
563 * @serial This is the character used to represent 0.
564 */
565 private char zeroDigit;
566
567 private static final long serialVersionUID = 5772796243397350300L;
568
569 private void readObject(ObjectInputStream stream)
570 throws IOException, ClassNotFoundException
571 {
572 stream.defaultReadObject();
573 if (serialVersionOnStream < 1)
574 {
575 monetarySeparator = decimalSeparator;
576 exponential = 'E';
577 serialVersionOnStream = 1;
578 }
579 }
580}
Note: See TracBrowser for help on using the repository browser.