1 |
/* NumberFormat.java -- Formats and parses numbers |
/* NumberFormat.java -- Formats and parses numbers |
2 |
Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. |
Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
79 |
*/ |
*/ |
80 |
public static final int FRACTION_FIELD = 1; |
public static final int FRACTION_FIELD = 1; |
81 |
|
|
82 |
|
public static class Field extends Format.Field |
83 |
|
{ |
84 |
|
static final long serialVersionUID = 7494728892700160890L; |
85 |
|
|
86 |
|
/** |
87 |
|
* Attribute set to all characters containing digits of the integer |
88 |
|
* part. |
89 |
|
*/ |
90 |
|
public static final NumberFormat.Field INTEGER |
91 |
|
= new Field("integer"); |
92 |
|
|
93 |
|
/** |
94 |
|
* Attribute set to all characters containing digits of the fractional |
95 |
|
* part. |
96 |
|
*/ |
97 |
|
public static final NumberFormat.Field FRACTION |
98 |
|
= new Field("fraction"); |
99 |
|
|
100 |
|
/** |
101 |
|
* Attribute set to all characters containing digits of the exponential |
102 |
|
* part. |
103 |
|
*/ |
104 |
|
public static final NumberFormat.Field EXPONENT |
105 |
|
= new Field("exponent"); |
106 |
|
|
107 |
|
/** |
108 |
|
* Attribute set to all characters containing a decimal separator. |
109 |
|
*/ |
110 |
|
public static final NumberFormat.Field DECIMAL_SEPARATOR |
111 |
|
= new Field("decimal separator"); |
112 |
|
|
113 |
|
/** |
114 |
|
* Attribute set to all characters containing a sign (plus or minus). |
115 |
|
*/ |
116 |
|
public static final NumberFormat.Field SIGN |
117 |
|
= new Field("sign"); |
118 |
|
|
119 |
|
/** |
120 |
|
* Attribute set to all characters containing a grouping separator (e.g. |
121 |
|
* a comma, a white space,...). |
122 |
|
*/ |
123 |
|
public static final NumberFormat.Field GROUPING_SEPARATOR |
124 |
|
= new Field("grouping separator"); |
125 |
|
|
126 |
|
/** |
127 |
|
* Attribute set to all characters containing an exponential symbol (e.g. |
128 |
|
* 'E') |
129 |
|
*/ |
130 |
|
public static final NumberFormat.Field EXPONENT_SYMBOL |
131 |
|
= new Field("exponent symbol"); |
132 |
|
|
133 |
|
/** |
134 |
|
* Attribute set to all characters containing a percent symbol (e.g. '%') |
135 |
|
*/ |
136 |
|
public static final NumberFormat.Field PERCENT |
137 |
|
= new Field("percent"); |
138 |
|
|
139 |
|
/** |
140 |
|
* Attribute set to all characters containing a permille symbol. |
141 |
|
*/ |
142 |
|
public static final NumberFormat.Field PERMILLE |
143 |
|
= new Field("permille"); |
144 |
|
|
145 |
|
/** |
146 |
|
* Attribute set to all characters containing the currency unit. |
147 |
|
*/ |
148 |
|
public static final NumberFormat.Field CURRENCY |
149 |
|
= new Field("currency"); |
150 |
|
|
151 |
|
/** |
152 |
|
* Attribute set to all characters containing the exponent sign. |
153 |
|
*/ |
154 |
|
public static final NumberFormat.Field EXPONENT_SIGN |
155 |
|
= new Field("exponent sign"); |
156 |
|
|
157 |
|
/** |
158 |
|
* Private fields to register all fields contained in this descriptor. |
159 |
|
*/ |
160 |
|
private static final NumberFormat.Field[] allFields = |
161 |
|
{ |
162 |
|
INTEGER, FRACTION, EXPONENT, DECIMAL_SEPARATOR, SIGN, |
163 |
|
GROUPING_SEPARATOR, EXPONENT_SYMBOL, PERCENT, |
164 |
|
PERMILLE, CURRENCY, EXPONENT_SIGN |
165 |
|
}; |
166 |
|
|
167 |
|
/** |
168 |
|
* This constructor is only used by the deserializer. Without it, |
169 |
|
* it would fail to construct a valid object. |
170 |
|
*/ |
171 |
|
private Field() |
172 |
|
{ |
173 |
|
super(""); |
174 |
|
} |
175 |
|
|
176 |
|
/** |
177 |
|
* Create a Field instance with the specified field name. |
178 |
|
* |
179 |
|
* @param field_name Field name for the new Field instance. |
180 |
|
*/ |
181 |
|
protected Field(String field_name) |
182 |
|
{ |
183 |
|
super (field_name); |
184 |
|
} |
185 |
|
|
186 |
|
/** |
187 |
|
* This function is used by the deserializer to know which object |
188 |
|
* to use when it encounters an encoded NumberFormat.Field in a |
189 |
|
* serialization stream. If the stream is valid it should return |
190 |
|
* one of the above field. In the other case we throw an exception. |
191 |
|
* |
192 |
|
* @return a valid official NumberFormat.Field instance. |
193 |
|
* |
194 |
|
* @throws InvalidObjectException if the field name is invalid. |
195 |
|
*/ |
196 |
|
protected Object readResolve() throws InvalidObjectException |
197 |
|
{ |
198 |
|
String s = getName(); |
199 |
|
for (int i=0;i<allFields.length;i++) |
200 |
|
if (s.equals(allFields[i].getName())) |
201 |
|
return allFields[i]; |
202 |
|
|
203 |
|
throw new InvalidObjectException("no such NumberFormat field called " + s); |
204 |
|
} |
205 |
|
} |
206 |
|
|
207 |
/** |
/** |
208 |
* This method is a specialization of the format method that performs |
* This method is a specialization of the format method that performs |
209 |
* a simple formatting of the specified <code>long</code> number. |
* a simple formatting of the specified <code>long</code> number. |
451 |
} |
} |
452 |
|
|
453 |
/** |
/** |
454 |
|
* This method returns an integer formatting and parsing class for the |
455 |
|
* default locale. This will be a concrete subclass of <code>NumberFormat</code>, |
456 |
|
* but the actual class returned is dependent on the locale. |
457 |
|
* |
458 |
|
* @return An instance of an integer number formatter for the default locale. |
459 |
|
*/ |
460 |
|
public static final NumberFormat getIntegerInstance() |
461 |
|
{ |
462 |
|
return getIntegerInstance (Locale.getDefault()); |
463 |
|
} |
464 |
|
|
465 |
|
/** |
466 |
|
* This method returns an integer formatting and parsing class for the |
467 |
|
* default locale. This will be a concrete subclass of <code>NumberFormat</code>, |
468 |
|
* but the actual class returned is dependent on the locale. |
469 |
|
* |
470 |
|
* @param locale the desired locale. |
471 |
|
* |
472 |
|
* @return An instance of an integer number formatter for the desired locale. |
473 |
|
*/ |
474 |
|
public static NumberFormat getIntegerInstance(Locale locale) |
475 |
|
{ |
476 |
|
NumberFormat format = computeInstance (locale, "numberFormat", "#,##0"); |
477 |
|
format.setParseIntegerOnly (true); |
478 |
|
return format; |
479 |
|
} |
480 |
|
|
481 |
|
/** |
482 |
* This method returns an instance of <code>NumberFormat</code> suitable |
* This method returns an instance of <code>NumberFormat</code> suitable |
483 |
* for formatting and parsing percentage values in the default locale. |
* for formatting and parsing percentage values in the default locale. |
484 |
* |
* |