aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/text/NumberFormat.java
blob: 3a30010b073279714c120033a4f17def3c5955a7 (plain)
1
2
3
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
/* Copyright (C) 1998, 1999, 2000  Free Software Foundation

   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.util.Locale;
import java.util.ResourceBundle;
import java.util.MissingResourceException;

/**
 * @author Tom Tromey <tromey@cygnus.com>
 * @date March 4, 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
 *          and getAvailableLocales.
 */

public abstract class NumberFormat extends Format implements Cloneable
{
  public static final int INTEGER_FIELD = 0;
  public static final int FRACTION_FIELD = 1;

  public final String format (long number)
    {
      StringBuffer sbuf = new StringBuffer(50);
      format (number, sbuf, null);
      return sbuf.toString();
    }

  public final StringBuffer format (Object obj, StringBuffer sbuf,
				    FieldPosition pos)
    {
      if (obj instanceof Number)
        return format(((Number) obj).doubleValue(), sbuf, pos);
      else
        throw new IllegalArgumentException 
          ("Cannot format given Object as a Number");
    }

  public abstract StringBuffer format (double number,
				       StringBuffer sbuf, FieldPosition pos);

  public abstract StringBuffer format (long number,
				       StringBuffer sbuf, FieldPosition pos);

  public Object clone ()
  {
    // We know the superclass just uses Object's generic cloner.
    // Why not just inherit?  Because the online docs specify that
    // this method exists for this class.
    return super.clone ();
  }

  public boolean equals (Object obj)
  {
    if (! (obj instanceof NumberFormat))
      return false;
    NumberFormat nf = (NumberFormat) obj;
    return (groupingUsed == nf.groupingUsed
	    && maximumFractionDigits == nf.maximumFractionDigits
	    && maximumIntegerDigits == nf.maximumIntegerDigits
	    && minimumFractionDigits == nf.minimumFractionDigits
	    && minimumIntegerDigits == nf.minimumIntegerDigits
	    && parseIntegerOnly == nf.parseIntegerOnly);
  }

  public static Locale[] getAvailableLocales ()
    {
      // FIXME.
      return null;
    }

  private static final NumberFormat computeInstance (Locale loc,
						     String resource,
						     String def)
    {
      ResourceBundle res;
      try
	{
	  res = ResourceBundle.getBundle("gnu.gcj.text.LocaleData", loc);
	}
      catch (MissingResourceException x)
	{
	  res = null;
	}
      String fmt;
      try
	{
	  fmt = res == null ? def : res.getString(resource);
	}
      catch (MissingResourceException x)
	{
	  fmt = def;
	}
      DecimalFormatSymbols dfs = new DecimalFormatSymbols (loc);
      return new DecimalFormat (fmt, dfs);
    }

  public static final NumberFormat getCurrencyInstance ()
    {
      return getCurrencyInstance (Locale.getDefault());
    }

  public static NumberFormat getCurrencyInstance (Locale loc)
    {
      return computeInstance (loc, "currencyFormat", "$#,##0.00;($#,##0.00)");
    }

  public static final NumberFormat getInstance ()
    {
      return getInstance (Locale.getDefault());
    }

  public static NumberFormat getInstance (Locale loc)
    {
      // For now always return a number instance.
      return getNumberInstance (loc);
    }

  public int getMaximumFractionDigits ()
    {
      return maximumFractionDigits;
    }

  public int getMaximumIntegerDigits ()
    {
      return maximumIntegerDigits;
    }

  public int getMinimumFractionDigits ()
    {
      return minimumFractionDigits;
    }

  public int getMinimumIntegerDigits ()
    {
      return minimumIntegerDigits;
    }

  public static final NumberFormat getNumberInstance ()
    {
      return getNumberInstance (Locale.getDefault());
    }

  public static NumberFormat getNumberInstance (Locale loc)
    {
      return computeInstance (loc, "numberFormat", "#,##0.###");
    }

  public static final NumberFormat getPercentInstance ()
    {
      return getPercentInstance (Locale.getDefault());
    }

  public static NumberFormat getPercentInstance (Locale loc)
    {
      return computeInstance (loc, "percentFormat", "#,##0%");
    }

  public int hashCode ()
    {
      int hash = super.hashCode();
      hash ^= (maximumFractionDigits + maximumIntegerDigits
	       + minimumFractionDigits + minimumIntegerDigits);
      if (groupingUsed)
	hash ^= 0xf0f0;
      if (parseIntegerOnly)
	hash ^= 0x0f0f;
      return hash;
    }

  public boolean isGroupingUsed ()
    {
      return groupingUsed;
    }

  public boolean isParseIntegerOnly ()
    {
      return parseIntegerOnly;
    }

  public NumberFormat ()
    {
    }

  public abstract Number parse (String sourceStr, ParsePosition pos);

  public Number parse (String sourceStr) throws ParseException
    {
      ParsePosition pp = new ParsePosition (0);
      Number r = parse (sourceStr, pp);
      if (r == null)
	{
	  int index = pp.getErrorIndex();
	  if (index < 0)
	    index = pp.getIndex();
	  throw new ParseException ("couldn't parse number", index);
	}
      return r;
    }

  public final Object parseObject (String sourceStr, ParsePosition pos)
    {
      return parse (sourceStr, pos);
    }

  public void setGroupingUsed (boolean newValue)
    {
      groupingUsed = newValue;
    }

  public void setMaximumFractionDigits (int newValue)
    {
      maximumFractionDigits = newValue;
    }

  public void setMaximumIntegerDigits (int newValue)
    {
      maximumIntegerDigits = newValue;
    }

  public void setMinimumFractionDigits (int newValue)
    {
      minimumFractionDigits = newValue;
    }

  public void setMinimumIntegerDigits (int newValue)
    {
      minimumIntegerDigits = newValue;
    }

  public void setParseIntegerOnly (boolean value)
    {
      parseIntegerOnly = value;
    }

  public final String format (double number)
    {
      StringBuffer sbuf = new StringBuffer(50);
      format (number, sbuf, null);
      return sbuf.toString();
    }

  // These field names are fixed by the serialization spec.
  // FIXME: serialization spec also mentions `byte' versions of the
  // min/max fields.  We have no use for those, so for now they are
  // omitted.
  protected boolean groupingUsed;
  protected int maximumFractionDigits;
  protected int maximumIntegerDigits;
  protected int minimumFractionDigits;
  protected int minimumIntegerDigits;
  protected boolean parseIntegerOnly;
}