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