DecimalFormatSymbols.java 6.66 KB
Newer Older
Tom Tromey committed
1 2
// DecimalFormatSymbols.java - Symbols used to format numbers.

3
/* Copyright (C) 1999  Free Software Foundation
Tom Tromey committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package java.text;

import java.io.Serializable;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * @author Tom Tromey <tromey@cygnus.com>
 * @date February 24, 1999
 */
/* Written using "Java Class Libraries", 2nd edition, plus online
 * API docs for JDK 1.2 from http://www.javasoft.com.
 * Status:  Believed complete and correct to 1.2, except serialization.
 */

public final class DecimalFormatSymbols implements Cloneable, Serializable
{
  public Object clone ()
    {
      return new DecimalFormatSymbols (this);
    }

  private DecimalFormatSymbols (DecimalFormatSymbols orig)
    {
      this.currencySymbol = orig.currencySymbol;
      this.decimalSeparator = orig.decimalSeparator;
      this.digit = orig.digit;
      this.exponential = orig.exponential;
      this.groupingSeparator = orig.groupingSeparator;
      this.infinity = orig.infinity;
      this.intlCurrencySymbol = orig.intlCurrencySymbol;
      this.minusSign = orig.minusSign;
      this.NaN = orig.NaN;
      this.patternSeparator = orig.patternSeparator;
      this.percent = orig.percent;
      this.perMill = orig.perMill;
      this.zeroDigit = orig.zeroDigit;
    }

  public DecimalFormatSymbols ()
    {
      this (Locale.getDefault());
    }

  private final String safeGetString (ResourceBundle bundle,
				      String name, String def)
    {
      if (bundle != null)
	{
	  try
	    {
	      return bundle.getString(name);
	    }
	  catch (MissingResourceException x)
	    {
	    }
	}
      return def;
    }

  public final char safeGetChar (ResourceBundle bundle,
				 String name, char def)
    {
      String r = null;
      if (bundle != null)
	{
	  try
	    {
	      r = bundle.getString(name);
	    }
	  catch (MissingResourceException x)
	    {
	    }
	}
      if (r == null || r.length() < 1)
	return def;
      return r.charAt(0);
    }

  public DecimalFormatSymbols (Locale loc)
    {
      ResourceBundle res;
      try
	{
	  res = ResourceBundle.getBundle("gnu.gcj.text.LocaleData", loc);
	}
      catch (MissingResourceException x)
	{
	  res = null;
	}
      currencySymbol = safeGetString (res, "currencySymbol", "$");
      decimalSeparator = safeGetChar (res, "decimalSeparator", '.');
      digit = safeGetChar (res, "digit", '#');
      exponential = safeGetChar (res, "exponential", 'E');
      groupingSeparator = safeGetChar (res, "groupingSeparator", ',');
      infinity = safeGetString (res, "infinity", "\u221e");
      // FIXME: default?
      intlCurrencySymbol = safeGetString (res, "intlCurrencySymbol", "$");
      minusSign = safeGetChar (res, "minusSign", '-');
      NaN = safeGetString (res, "NaN", "\ufffd");
      patternSeparator = safeGetChar (res, "patternSeparator", ';');
      percent = safeGetChar (res, "percent", '%');
      perMill = safeGetChar (res, "perMill", '\u2030');
      zeroDigit = safeGetChar (res, "zeroDigit", '0');
    }

  public boolean equals (Object obj)
    {
      if (! (obj instanceof DecimalFormatSymbols))
	return false;
      DecimalFormatSymbols dfs = (DecimalFormatSymbols) obj;
      return (currencySymbol.equals(dfs.currencySymbol)
	      && decimalSeparator == dfs.decimalSeparator
	      && digit == dfs.digit
	      && exponential == dfs.exponential
	      && groupingSeparator == dfs.groupingSeparator
	      && infinity.equals(dfs.infinity)
	      && intlCurrencySymbol.equals(dfs.intlCurrencySymbol)
	      && minusSign == dfs.minusSign
	      && NaN.equals(dfs.NaN)
	      && patternSeparator == dfs.patternSeparator
	      && percent == dfs.percent
	      && perMill == dfs.perMill
	      && zeroDigit == dfs.zeroDigit);
    }

  public String getCurrencySymbol ()
    {
      return currencySymbol;
    }

  public char getDecimalSeparator ()
    {
      return decimalSeparator;
    }

  public char getDigit ()
    {
      return digit;
    }

  // This is our own extension.
  char getExponential ()
    {
      return exponential;
    }

  public char getGroupingSeparator ()
    {
      return groupingSeparator;
    }

  public String getInfinity ()
    {
      return infinity;
    }

  public String getInternationalCurrencySymbol ()
    {
      return intlCurrencySymbol;
    }

  public char getMinusSign ()
    {
      return minusSign;
    }

  public String getNaN ()
    {
      return NaN;
    }

  public char getPatternSeparator ()
    {
      return patternSeparator;
    }

  public char getPercent ()
    {
      return percent;
    }

  public char getPerMill ()
    {
      return perMill;
    }

  public char getZeroDigit ()
    {
      return zeroDigit;
    }

  public int hashCode ()
    {
      // Compute based on zero digit, grouping separator, and decimal
      // separator -- JCL book.  This probably isn't a very good hash
      // code.
      return zeroDigit << 16 + groupingSeparator << 8 + decimalSeparator;
    }

  public void setCurrenySymbol (String currency)
    {
      currencySymbol = currency;
    }

  public void setDecimalSeparator (char decimalSep)
    {
      decimalSeparator = decimalSep;
    }

  public void setDigit (char digit)
    {
      this.digit = digit;
    }

  // This is our own extension.
  void setExponential (char exp)
    {
      exponential = exp;
    }

  public void setGroupingSeparator (char groupSep)
    {
      groupingSeparator = groupSep;
    }

  public void setInfinity (String infinity)
    {
      this.infinity = infinity;
    }

  public void setInternationalCurrencySymbol (String currency)
    {
      intlCurrencySymbol = currency;
    }

  public void setMinusSign (char minusSign)
    {
      this.minusSign = minusSign;
    }

  public void setNaN (String nan)
    {
      NaN = nan;
    }

  public void setPatternSeparator (char patternSep)
    {
      patternSeparator = patternSep;
    }

  public void setPercent (char percent)
    {
      this.percent = percent;
    }

  public void setPerMill (char perMill)
    {
      this.perMill = perMill;
    }

  public void setZeroDigit (char zeroDigit)
    {
      this.zeroDigit = zeroDigit;
    }

  // The names of the instance variables are fixed by the
  // serialization spec.
  private String currencySymbol;
  private char decimalSeparator;
  private char digit;
  private char exponential;
  private char groupingSeparator;
  private String infinity;
  private String intlCurrencySymbol;
  private char minusSign;
  private String NaN;
  private char patternSeparator;
  private char percent;
  private char perMill;
  private char zeroDigit;
}