jtulach@1334
|
1 |
/*
|
jtulach@1334
|
2 |
* Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
|
jtulach@1334
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jtulach@1334
|
4 |
*
|
jtulach@1334
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jtulach@1334
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jtulach@1334
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jtulach@1334
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jtulach@1334
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jtulach@1334
|
10 |
*
|
jtulach@1334
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jtulach@1334
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jtulach@1334
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jtulach@1334
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jtulach@1334
|
15 |
* accompanied this code).
|
jtulach@1334
|
16 |
*
|
jtulach@1334
|
17 |
* You should have received a copy of the GNU General Public License version
|
jtulach@1334
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jtulach@1334
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jtulach@1334
|
20 |
*
|
jtulach@1334
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jtulach@1334
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jtulach@1334
|
23 |
* questions.
|
jtulach@1334
|
24 |
*/
|
jtulach@1334
|
25 |
|
jtulach@1334
|
26 |
/*
|
jtulach@1334
|
27 |
* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
|
jtulach@1334
|
28 |
* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
|
jtulach@1334
|
29 |
*
|
jtulach@1334
|
30 |
* The original version of this source code and documentation is copyrighted
|
jtulach@1334
|
31 |
* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
|
jtulach@1334
|
32 |
* materials are provided under terms of a License Agreement between Taligent
|
jtulach@1334
|
33 |
* and Sun. This technology is protected by multiple US and International
|
jtulach@1334
|
34 |
* patents. This notice and attribution to Taligent may not be removed.
|
jtulach@1334
|
35 |
* Taligent is a registered trademark of Taligent, Inc.
|
jtulach@1334
|
36 |
*
|
jtulach@1334
|
37 |
*/
|
jtulach@1334
|
38 |
|
jtulach@1334
|
39 |
package java.text;
|
jtulach@1334
|
40 |
|
jtulach@1334
|
41 |
import java.io.InvalidObjectException;
|
jtulach@1334
|
42 |
import java.io.IOException;
|
jtulach@1334
|
43 |
import java.io.ObjectInputStream;
|
jtulach@1334
|
44 |
import java.io.ObjectOutputStream;
|
jtulach@1334
|
45 |
import java.math.BigInteger;
|
jtulach@1334
|
46 |
import java.math.RoundingMode;
|
jtulach@1334
|
47 |
import java.text.spi.NumberFormatProvider;
|
jtulach@1334
|
48 |
import java.util.Currency;
|
jtulach@1334
|
49 |
import java.util.HashMap;
|
jtulach@1334
|
50 |
import java.util.Hashtable;
|
jtulach@1334
|
51 |
import java.util.Locale;
|
jtulach@1334
|
52 |
import java.util.Map;
|
jtulach@1334
|
53 |
import java.util.ResourceBundle;
|
jtulach@1334
|
54 |
import java.util.concurrent.atomic.AtomicInteger;
|
jtulach@1334
|
55 |
import java.util.concurrent.atomic.AtomicLong;
|
jtulach@1334
|
56 |
import java.util.spi.LocaleServiceProvider;
|
jtulach@1334
|
57 |
import sun.util.LocaleServiceProviderPool;
|
jtulach@1334
|
58 |
import sun.util.resources.LocaleData;
|
jtulach@1334
|
59 |
|
jtulach@1334
|
60 |
/**
|
jtulach@1334
|
61 |
* <code>NumberFormat</code> is the abstract base class for all number
|
jtulach@1334
|
62 |
* formats. This class provides the interface for formatting and parsing
|
jtulach@1334
|
63 |
* numbers. <code>NumberFormat</code> also provides methods for determining
|
jtulach@1334
|
64 |
* which locales have number formats, and what their names are.
|
jtulach@1334
|
65 |
*
|
jtulach@1334
|
66 |
* <p>
|
jtulach@1334
|
67 |
* <code>NumberFormat</code> helps you to format and parse numbers for any locale.
|
jtulach@1334
|
68 |
* Your code can be completely independent of the locale conventions for
|
jtulach@1334
|
69 |
* decimal points, thousands-separators, or even the particular decimal
|
jtulach@1334
|
70 |
* digits used, or whether the number format is even decimal.
|
jtulach@1334
|
71 |
*
|
jtulach@1334
|
72 |
* <p>
|
jtulach@1334
|
73 |
* To format a number for the current Locale, use one of the factory
|
jtulach@1334
|
74 |
* class methods:
|
jtulach@1334
|
75 |
* <blockquote>
|
jtulach@1334
|
76 |
* <pre>
|
jtulach@1334
|
77 |
* myString = NumberFormat.getInstance().format(myNumber);
|
jtulach@1334
|
78 |
* </pre>
|
jtulach@1334
|
79 |
* </blockquote>
|
jtulach@1334
|
80 |
* If you are formatting multiple numbers, it is
|
jtulach@1334
|
81 |
* more efficient to get the format and use it multiple times so that
|
jtulach@1334
|
82 |
* the system doesn't have to fetch the information about the local
|
jtulach@1334
|
83 |
* language and country conventions multiple times.
|
jtulach@1334
|
84 |
* <blockquote>
|
jtulach@1334
|
85 |
* <pre>
|
jtulach@1334
|
86 |
* NumberFormat nf = NumberFormat.getInstance();
|
jtulach@1334
|
87 |
* for (int i = 0; i < myNumber.length; ++i) {
|
jtulach@1334
|
88 |
* output.println(nf.format(myNumber[i]) + "; ");
|
jtulach@1334
|
89 |
* }
|
jtulach@1334
|
90 |
* </pre>
|
jtulach@1334
|
91 |
* </blockquote>
|
jtulach@1334
|
92 |
* To format a number for a different Locale, specify it in the
|
jtulach@1334
|
93 |
* call to <code>getInstance</code>.
|
jtulach@1334
|
94 |
* <blockquote>
|
jtulach@1334
|
95 |
* <pre>
|
jtulach@1334
|
96 |
* NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
|
jtulach@1334
|
97 |
* </pre>
|
jtulach@1334
|
98 |
* </blockquote>
|
jtulach@1334
|
99 |
* You can also use a <code>NumberFormat</code> to parse numbers:
|
jtulach@1334
|
100 |
* <blockquote>
|
jtulach@1334
|
101 |
* <pre>
|
jtulach@1334
|
102 |
* myNumber = nf.parse(myString);
|
jtulach@1334
|
103 |
* </pre>
|
jtulach@1334
|
104 |
* </blockquote>
|
jtulach@1334
|
105 |
* Use <code>getInstance</code> or <code>getNumberInstance</code> to get the
|
jtulach@1334
|
106 |
* normal number format. Use <code>getIntegerInstance</code> to get an
|
jtulach@1334
|
107 |
* integer number format. Use <code>getCurrencyInstance</code> to get the
|
jtulach@1334
|
108 |
* currency number format. And use <code>getPercentInstance</code> to get a
|
jtulach@1334
|
109 |
* format for displaying percentages. With this format, a fraction like
|
jtulach@1334
|
110 |
* 0.53 is displayed as 53%.
|
jtulach@1334
|
111 |
*
|
jtulach@1334
|
112 |
* <p>
|
jtulach@1334
|
113 |
* You can also control the display of numbers with such methods as
|
jtulach@1334
|
114 |
* <code>setMinimumFractionDigits</code>.
|
jtulach@1334
|
115 |
* If you want even more control over the format or parsing,
|
jtulach@1334
|
116 |
* or want to give your users more control,
|
jtulach@1334
|
117 |
* you can try casting the <code>NumberFormat</code> you get from the factory methods
|
jtulach@1334
|
118 |
* to a <code>DecimalFormat</code>. This will work for the vast majority
|
jtulach@1334
|
119 |
* of locales; just remember to put it in a <code>try</code> block in case you
|
jtulach@1334
|
120 |
* encounter an unusual one.
|
jtulach@1334
|
121 |
*
|
jtulach@1334
|
122 |
* <p>
|
jtulach@1334
|
123 |
* NumberFormat and DecimalFormat are designed such that some controls
|
jtulach@1334
|
124 |
* work for formatting and others work for parsing. The following is
|
jtulach@1334
|
125 |
* the detailed description for each these control methods,
|
jtulach@1334
|
126 |
* <p>
|
jtulach@1334
|
127 |
* setParseIntegerOnly : only affects parsing, e.g.
|
jtulach@1334
|
128 |
* if true, "3456.78" -> 3456 (and leaves the parse position just after index 6)
|
jtulach@1334
|
129 |
* if false, "3456.78" -> 3456.78 (and leaves the parse position just after index 8)
|
jtulach@1334
|
130 |
* This is independent of formatting. If you want to not show a decimal point
|
jtulach@1334
|
131 |
* where there might be no digits after the decimal point, use
|
jtulach@1334
|
132 |
* setDecimalSeparatorAlwaysShown.
|
jtulach@1334
|
133 |
* <p>
|
jtulach@1334
|
134 |
* setDecimalSeparatorAlwaysShown : only affects formatting, and only where
|
jtulach@1334
|
135 |
* there might be no digits after the decimal point, such as with a pattern
|
jtulach@1334
|
136 |
* like "#,##0.##", e.g.,
|
jtulach@1334
|
137 |
* if true, 3456.00 -> "3,456."
|
jtulach@1334
|
138 |
* if false, 3456.00 -> "3456"
|
jtulach@1334
|
139 |
* This is independent of parsing. If you want parsing to stop at the decimal
|
jtulach@1334
|
140 |
* point, use setParseIntegerOnly.
|
jtulach@1334
|
141 |
*
|
jtulach@1334
|
142 |
* <p>
|
jtulach@1334
|
143 |
* You can also use forms of the <code>parse</code> and <code>format</code>
|
jtulach@1334
|
144 |
* methods with <code>ParsePosition</code> and <code>FieldPosition</code> to
|
jtulach@1334
|
145 |
* allow you to:
|
jtulach@1334
|
146 |
* <ul>
|
jtulach@1334
|
147 |
* <li> progressively parse through pieces of a string
|
jtulach@1334
|
148 |
* <li> align the decimal point and other areas
|
jtulach@1334
|
149 |
* </ul>
|
jtulach@1334
|
150 |
* For example, you can align numbers in two ways:
|
jtulach@1334
|
151 |
* <ol>
|
jtulach@1334
|
152 |
* <li> If you are using a monospaced font with spacing for alignment,
|
jtulach@1334
|
153 |
* you can pass the <code>FieldPosition</code> in your format call, with
|
jtulach@1334
|
154 |
* <code>field</code> = <code>INTEGER_FIELD</code>. On output,
|
jtulach@1334
|
155 |
* <code>getEndIndex</code> will be set to the offset between the
|
jtulach@1334
|
156 |
* last character of the integer and the decimal. Add
|
jtulach@1334
|
157 |
* (desiredSpaceCount - getEndIndex) spaces at the front of the string.
|
jtulach@1334
|
158 |
*
|
jtulach@1334
|
159 |
* <li> If you are using proportional fonts,
|
jtulach@1334
|
160 |
* instead of padding with spaces, measure the width
|
jtulach@1334
|
161 |
* of the string in pixels from the start to <code>getEndIndex</code>.
|
jtulach@1334
|
162 |
* Then move the pen by
|
jtulach@1334
|
163 |
* (desiredPixelWidth - widthToAlignmentPoint) before drawing the text.
|
jtulach@1334
|
164 |
* It also works where there is no decimal, but possibly additional
|
jtulach@1334
|
165 |
* characters at the end, e.g., with parentheses in negative
|
jtulach@1334
|
166 |
* numbers: "(12)" for -12.
|
jtulach@1334
|
167 |
* </ol>
|
jtulach@1334
|
168 |
*
|
jtulach@1334
|
169 |
* <h4><a name="synchronization">Synchronization</a></h4>
|
jtulach@1334
|
170 |
*
|
jtulach@1334
|
171 |
* <p>
|
jtulach@1334
|
172 |
* Number formats are generally not synchronized.
|
jtulach@1334
|
173 |
* It is recommended to create separate format instances for each thread.
|
jtulach@1334
|
174 |
* If multiple threads access a format concurrently, it must be synchronized
|
jtulach@1334
|
175 |
* externally.
|
jtulach@1334
|
176 |
*
|
jtulach@1334
|
177 |
* @see DecimalFormat
|
jtulach@1334
|
178 |
* @see ChoiceFormat
|
jtulach@1334
|
179 |
* @author Mark Davis
|
jtulach@1334
|
180 |
* @author Helena Shih
|
jtulach@1334
|
181 |
*/
|
jtulach@1334
|
182 |
public abstract class NumberFormat extends Format {
|
jtulach@1334
|
183 |
|
jtulach@1334
|
184 |
/**
|
jtulach@1334
|
185 |
* Field constant used to construct a FieldPosition object. Signifies that
|
jtulach@1334
|
186 |
* the position of the integer part of a formatted number should be returned.
|
jtulach@1334
|
187 |
* @see java.text.FieldPosition
|
jtulach@1334
|
188 |
*/
|
jtulach@1334
|
189 |
public static final int INTEGER_FIELD = 0;
|
jtulach@1334
|
190 |
|
jtulach@1334
|
191 |
/**
|
jtulach@1334
|
192 |
* Field constant used to construct a FieldPosition object. Signifies that
|
jtulach@1334
|
193 |
* the position of the fraction part of a formatted number should be returned.
|
jtulach@1334
|
194 |
* @see java.text.FieldPosition
|
jtulach@1334
|
195 |
*/
|
jtulach@1334
|
196 |
public static final int FRACTION_FIELD = 1;
|
jtulach@1334
|
197 |
|
jtulach@1334
|
198 |
/**
|
jtulach@1334
|
199 |
* Sole constructor. (For invocation by subclass constructors, typically
|
jtulach@1334
|
200 |
* implicit.)
|
jtulach@1334
|
201 |
*/
|
jtulach@1334
|
202 |
protected NumberFormat() {
|
jtulach@1334
|
203 |
}
|
jtulach@1334
|
204 |
|
jtulach@1334
|
205 |
/**
|
jtulach@1334
|
206 |
* Formats a number and appends the resulting text to the given string
|
jtulach@1334
|
207 |
* buffer.
|
jtulach@1334
|
208 |
* The number can be of any subclass of {@link java.lang.Number}.
|
jtulach@1334
|
209 |
* <p>
|
jtulach@1334
|
210 |
* This implementation extracts the number's value using
|
jtulach@1334
|
211 |
* {@link java.lang.Number#longValue()} for all integral type values that
|
jtulach@1334
|
212 |
* can be converted to <code>long</code> without loss of information,
|
jtulach@1334
|
213 |
* including <code>BigInteger</code> values with a
|
jtulach@1334
|
214 |
* {@link java.math.BigInteger#bitLength() bit length} of less than 64,
|
jtulach@1334
|
215 |
* and {@link java.lang.Number#doubleValue()} for all other types. It
|
jtulach@1334
|
216 |
* then calls
|
jtulach@1334
|
217 |
* {@link #format(long,java.lang.StringBuffer,java.text.FieldPosition)}
|
jtulach@1334
|
218 |
* or {@link #format(double,java.lang.StringBuffer,java.text.FieldPosition)}.
|
jtulach@1334
|
219 |
* This may result in loss of magnitude information and precision for
|
jtulach@1334
|
220 |
* <code>BigInteger</code> and <code>BigDecimal</code> values.
|
jtulach@1334
|
221 |
* @param number the number to format
|
jtulach@1334
|
222 |
* @param toAppendTo the <code>StringBuffer</code> to which the formatted
|
jtulach@1334
|
223 |
* text is to be appended
|
jtulach@1334
|
224 |
* @param pos On input: an alignment field, if desired.
|
jtulach@1334
|
225 |
* On output: the offsets of the alignment field.
|
jtulach@1334
|
226 |
* @return the value passed in as <code>toAppendTo</code>
|
jtulach@1334
|
227 |
* @exception IllegalArgumentException if <code>number</code> is
|
jtulach@1334
|
228 |
* null or not an instance of <code>Number</code>.
|
jtulach@1334
|
229 |
* @exception NullPointerException if <code>toAppendTo</code> or
|
jtulach@1334
|
230 |
* <code>pos</code> is null
|
jtulach@1334
|
231 |
* @exception ArithmeticException if rounding is needed with rounding
|
jtulach@1334
|
232 |
* mode being set to RoundingMode.UNNECESSARY
|
jtulach@1334
|
233 |
* @see java.text.FieldPosition
|
jtulach@1334
|
234 |
*/
|
jtulach@1334
|
235 |
public StringBuffer format(Object number,
|
jtulach@1334
|
236 |
StringBuffer toAppendTo,
|
jtulach@1334
|
237 |
FieldPosition pos) {
|
jtulach@1334
|
238 |
if (number instanceof Long || number instanceof Integer ||
|
jtulach@1334
|
239 |
number instanceof Short || number instanceof Byte ||
|
jtulach@1334
|
240 |
number instanceof AtomicInteger || number instanceof AtomicLong ||
|
jtulach@1334
|
241 |
(number instanceof BigInteger &&
|
jtulach@1334
|
242 |
((BigInteger)number).bitLength() < 64)) {
|
jtulach@1334
|
243 |
return format(((Number)number).longValue(), toAppendTo, pos);
|
jtulach@1334
|
244 |
} else if (number instanceof Number) {
|
jtulach@1334
|
245 |
return format(((Number)number).doubleValue(), toAppendTo, pos);
|
jtulach@1334
|
246 |
} else {
|
jtulach@1334
|
247 |
throw new IllegalArgumentException("Cannot format given Object as a Number");
|
jtulach@1334
|
248 |
}
|
jtulach@1334
|
249 |
}
|
jtulach@1334
|
250 |
|
jtulach@1334
|
251 |
/**
|
jtulach@1334
|
252 |
* Parses text from a string to produce a <code>Number</code>.
|
jtulach@1334
|
253 |
* <p>
|
jtulach@1334
|
254 |
* The method attempts to parse text starting at the index given by
|
jtulach@1334
|
255 |
* <code>pos</code>.
|
jtulach@1334
|
256 |
* If parsing succeeds, then the index of <code>pos</code> is updated
|
jtulach@1334
|
257 |
* to the index after the last character used (parsing does not necessarily
|
jtulach@1334
|
258 |
* use all characters up to the end of the string), and the parsed
|
jtulach@1334
|
259 |
* number is returned. The updated <code>pos</code> can be used to
|
jtulach@1334
|
260 |
* indicate the starting point for the next call to this method.
|
jtulach@1334
|
261 |
* If an error occurs, then the index of <code>pos</code> is not
|
jtulach@1334
|
262 |
* changed, the error index of <code>pos</code> is set to the index of
|
jtulach@1334
|
263 |
* the character where the error occurred, and null is returned.
|
jtulach@1334
|
264 |
* <p>
|
jtulach@1334
|
265 |
* See the {@link #parse(String, ParsePosition)} method for more information
|
jtulach@1334
|
266 |
* on number parsing.
|
jtulach@1334
|
267 |
*
|
jtulach@1334
|
268 |
* @param source A <code>String</code>, part of which should be parsed.
|
jtulach@1334
|
269 |
* @param pos A <code>ParsePosition</code> object with index and error
|
jtulach@1334
|
270 |
* index information as described above.
|
jtulach@1334
|
271 |
* @return A <code>Number</code> parsed from the string. In case of
|
jtulach@1334
|
272 |
* error, returns null.
|
jtulach@1334
|
273 |
* @exception NullPointerException if <code>pos</code> is null.
|
jtulach@1334
|
274 |
*/
|
jtulach@1334
|
275 |
public final Object parseObject(String source, ParsePosition pos) {
|
jtulach@1334
|
276 |
return parse(source, pos);
|
jtulach@1334
|
277 |
}
|
jtulach@1334
|
278 |
|
jtulach@1334
|
279 |
/**
|
jtulach@1334
|
280 |
* Specialization of format.
|
jtulach@1334
|
281 |
* @exception ArithmeticException if rounding is needed with rounding
|
jtulach@1334
|
282 |
* mode being set to RoundingMode.UNNECESSARY
|
jtulach@1334
|
283 |
* @see java.text.Format#format
|
jtulach@1334
|
284 |
*/
|
jtulach@1334
|
285 |
public final String format(double number) {
|
jtulach@1334
|
286 |
return format(number, new StringBuffer(),
|
jtulach@1334
|
287 |
DontCareFieldPosition.INSTANCE).toString();
|
jtulach@1334
|
288 |
}
|
jtulach@1334
|
289 |
|
jtulach@1334
|
290 |
/**
|
jtulach@1334
|
291 |
* Specialization of format.
|
jtulach@1334
|
292 |
* @exception ArithmeticException if rounding is needed with rounding
|
jtulach@1334
|
293 |
* mode being set to RoundingMode.UNNECESSARY
|
jtulach@1334
|
294 |
* @see java.text.Format#format
|
jtulach@1334
|
295 |
*/
|
jtulach@1334
|
296 |
public final String format(long number) {
|
jtulach@1334
|
297 |
return format(number, new StringBuffer(),
|
jtulach@1334
|
298 |
DontCareFieldPosition.INSTANCE).toString();
|
jtulach@1334
|
299 |
}
|
jtulach@1334
|
300 |
|
jtulach@1334
|
301 |
/**
|
jtulach@1334
|
302 |
* Specialization of format.
|
jtulach@1334
|
303 |
* @exception ArithmeticException if rounding is needed with rounding
|
jtulach@1334
|
304 |
* mode being set to RoundingMode.UNNECESSARY
|
jtulach@1334
|
305 |
* @see java.text.Format#format
|
jtulach@1334
|
306 |
*/
|
jtulach@1334
|
307 |
public abstract StringBuffer format(double number,
|
jtulach@1334
|
308 |
StringBuffer toAppendTo,
|
jtulach@1334
|
309 |
FieldPosition pos);
|
jtulach@1334
|
310 |
|
jtulach@1334
|
311 |
/**
|
jtulach@1334
|
312 |
* Specialization of format.
|
jtulach@1334
|
313 |
* @exception ArithmeticException if rounding is needed with rounding
|
jtulach@1334
|
314 |
* mode being set to RoundingMode.UNNECESSARY
|
jtulach@1334
|
315 |
* @see java.text.Format#format
|
jtulach@1334
|
316 |
*/
|
jtulach@1334
|
317 |
public abstract StringBuffer format(long number,
|
jtulach@1334
|
318 |
StringBuffer toAppendTo,
|
jtulach@1334
|
319 |
FieldPosition pos);
|
jtulach@1334
|
320 |
|
jtulach@1334
|
321 |
/**
|
jtulach@1334
|
322 |
* Returns a Long if possible (e.g., within the range [Long.MIN_VALUE,
|
jtulach@1334
|
323 |
* Long.MAX_VALUE] and with no decimals), otherwise a Double.
|
jtulach@1334
|
324 |
* If IntegerOnly is set, will stop at a decimal
|
jtulach@1334
|
325 |
* point (or equivalent; e.g., for rational numbers "1 2/3", will stop
|
jtulach@1334
|
326 |
* after the 1).
|
jtulach@1334
|
327 |
* Does not throw an exception; if no object can be parsed, index is
|
jtulach@1334
|
328 |
* unchanged!
|
jtulach@1334
|
329 |
* @see java.text.NumberFormat#isParseIntegerOnly
|
jtulach@1334
|
330 |
* @see java.text.Format#parseObject
|
jtulach@1334
|
331 |
*/
|
jtulach@1334
|
332 |
public abstract Number parse(String source, ParsePosition parsePosition);
|
jtulach@1334
|
333 |
|
jtulach@1334
|
334 |
/**
|
jtulach@1334
|
335 |
* Parses text from the beginning of the given string to produce a number.
|
jtulach@1334
|
336 |
* The method may not use the entire text of the given string.
|
jtulach@1334
|
337 |
* <p>
|
jtulach@1334
|
338 |
* See the {@link #parse(String, ParsePosition)} method for more information
|
jtulach@1334
|
339 |
* on number parsing.
|
jtulach@1334
|
340 |
*
|
jtulach@1334
|
341 |
* @param source A <code>String</code> whose beginning should be parsed.
|
jtulach@1334
|
342 |
* @return A <code>Number</code> parsed from the string.
|
jtulach@1334
|
343 |
* @exception ParseException if the beginning of the specified string
|
jtulach@1334
|
344 |
* cannot be parsed.
|
jtulach@1334
|
345 |
*/
|
jtulach@1334
|
346 |
public Number parse(String source) throws ParseException {
|
jtulach@1334
|
347 |
ParsePosition parsePosition = new ParsePosition(0);
|
jtulach@1334
|
348 |
Number result = parse(source, parsePosition);
|
jtulach@1334
|
349 |
if (parsePosition.index == 0) {
|
jtulach@1334
|
350 |
throw new ParseException("Unparseable number: \"" + source + "\"",
|
jtulach@1334
|
351 |
parsePosition.errorIndex);
|
jtulach@1334
|
352 |
}
|
jtulach@1334
|
353 |
return result;
|
jtulach@1334
|
354 |
}
|
jtulach@1334
|
355 |
|
jtulach@1334
|
356 |
/**
|
jtulach@1334
|
357 |
* Returns true if this format will parse numbers as integers only.
|
jtulach@1334
|
358 |
* For example in the English locale, with ParseIntegerOnly true, the
|
jtulach@1334
|
359 |
* string "1234." would be parsed as the integer value 1234 and parsing
|
jtulach@1334
|
360 |
* would stop at the "." character. Of course, the exact format accepted
|
jtulach@1334
|
361 |
* by the parse operation is locale dependant and determined by sub-classes
|
jtulach@1334
|
362 |
* of NumberFormat.
|
jtulach@1334
|
363 |
*/
|
jtulach@1334
|
364 |
public boolean isParseIntegerOnly() {
|
jtulach@1334
|
365 |
return parseIntegerOnly;
|
jtulach@1334
|
366 |
}
|
jtulach@1334
|
367 |
|
jtulach@1334
|
368 |
/**
|
jtulach@1334
|
369 |
* Sets whether or not numbers should be parsed as integers only.
|
jtulach@1334
|
370 |
* @see #isParseIntegerOnly
|
jtulach@1334
|
371 |
*/
|
jtulach@1334
|
372 |
public void setParseIntegerOnly(boolean value) {
|
jtulach@1334
|
373 |
parseIntegerOnly = value;
|
jtulach@1334
|
374 |
}
|
jtulach@1334
|
375 |
|
jtulach@1334
|
376 |
//============== Locale Stuff =====================
|
jtulach@1334
|
377 |
|
jtulach@1334
|
378 |
/**
|
jtulach@1334
|
379 |
* Returns a general-purpose number format for the current default locale.
|
jtulach@1334
|
380 |
* This is the same as calling
|
jtulach@1334
|
381 |
* {@link #getNumberInstance() getNumberInstance()}.
|
jtulach@1334
|
382 |
*/
|
jtulach@1334
|
383 |
public final static NumberFormat getInstance() {
|
jtulach@1334
|
384 |
return getInstance(Locale.getDefault(Locale.Category.FORMAT), NUMBERSTYLE);
|
jtulach@1334
|
385 |
}
|
jtulach@1334
|
386 |
|
jtulach@1334
|
387 |
/**
|
jtulach@1334
|
388 |
* Returns a general-purpose number format for the specified locale.
|
jtulach@1334
|
389 |
* This is the same as calling
|
jtulach@1334
|
390 |
* {@link #getNumberInstance(java.util.Locale) getNumberInstance(inLocale)}.
|
jtulach@1334
|
391 |
*/
|
jtulach@1334
|
392 |
public static NumberFormat getInstance(Locale inLocale) {
|
jtulach@1334
|
393 |
return getInstance(inLocale, NUMBERSTYLE);
|
jtulach@1334
|
394 |
}
|
jtulach@1334
|
395 |
|
jtulach@1334
|
396 |
/**
|
jtulach@1334
|
397 |
* Returns a general-purpose number format for the current default locale.
|
jtulach@1334
|
398 |
*/
|
jtulach@1334
|
399 |
public final static NumberFormat getNumberInstance() {
|
jtulach@1334
|
400 |
return getInstance(Locale.getDefault(Locale.Category.FORMAT), NUMBERSTYLE);
|
jtulach@1334
|
401 |
}
|
jtulach@1334
|
402 |
|
jtulach@1334
|
403 |
/**
|
jtulach@1334
|
404 |
* Returns a general-purpose number format for the specified locale.
|
jtulach@1334
|
405 |
*/
|
jtulach@1334
|
406 |
public static NumberFormat getNumberInstance(Locale inLocale) {
|
jtulach@1334
|
407 |
return getInstance(inLocale, NUMBERSTYLE);
|
jtulach@1334
|
408 |
}
|
jtulach@1334
|
409 |
|
jtulach@1334
|
410 |
/**
|
jtulach@1334
|
411 |
* Returns an integer number format for the current default locale. The
|
jtulach@1334
|
412 |
* returned number format is configured to round floating point numbers
|
jtulach@1334
|
413 |
* to the nearest integer using half-even rounding (see {@link
|
jtulach@1334
|
414 |
* java.math.RoundingMode#HALF_EVEN RoundingMode.HALF_EVEN}) for formatting,
|
jtulach@1334
|
415 |
* and to parse only the integer part of an input string (see {@link
|
jtulach@1334
|
416 |
* #isParseIntegerOnly isParseIntegerOnly}).
|
jtulach@1334
|
417 |
*
|
jtulach@1334
|
418 |
* @see #getRoundingMode()
|
jtulach@1334
|
419 |
* @return a number format for integer values
|
jtulach@1334
|
420 |
* @since 1.4
|
jtulach@1334
|
421 |
*/
|
jtulach@1334
|
422 |
public final static NumberFormat getIntegerInstance() {
|
jtulach@1334
|
423 |
return getInstance(Locale.getDefault(Locale.Category.FORMAT), INTEGERSTYLE);
|
jtulach@1334
|
424 |
}
|
jtulach@1334
|
425 |
|
jtulach@1334
|
426 |
/**
|
jtulach@1334
|
427 |
* Returns an integer number format for the specified locale. The
|
jtulach@1334
|
428 |
* returned number format is configured to round floating point numbers
|
jtulach@1334
|
429 |
* to the nearest integer using half-even rounding (see {@link
|
jtulach@1334
|
430 |
* java.math.RoundingMode#HALF_EVEN RoundingMode.HALF_EVEN}) for formatting,
|
jtulach@1334
|
431 |
* and to parse only the integer part of an input string (see {@link
|
jtulach@1334
|
432 |
* #isParseIntegerOnly isParseIntegerOnly}).
|
jtulach@1334
|
433 |
*
|
jtulach@1334
|
434 |
* @see #getRoundingMode()
|
jtulach@1334
|
435 |
* @return a number format for integer values
|
jtulach@1334
|
436 |
* @since 1.4
|
jtulach@1334
|
437 |
*/
|
jtulach@1334
|
438 |
public static NumberFormat getIntegerInstance(Locale inLocale) {
|
jtulach@1334
|
439 |
return getInstance(inLocale, INTEGERSTYLE);
|
jtulach@1334
|
440 |
}
|
jtulach@1334
|
441 |
|
jtulach@1334
|
442 |
/**
|
jtulach@1334
|
443 |
* Returns a currency format for the current default locale.
|
jtulach@1334
|
444 |
*/
|
jtulach@1334
|
445 |
public final static NumberFormat getCurrencyInstance() {
|
jtulach@1334
|
446 |
return getInstance(Locale.getDefault(Locale.Category.FORMAT), CURRENCYSTYLE);
|
jtulach@1334
|
447 |
}
|
jtulach@1334
|
448 |
|
jtulach@1334
|
449 |
/**
|
jtulach@1334
|
450 |
* Returns a currency format for the specified locale.
|
jtulach@1334
|
451 |
*/
|
jtulach@1334
|
452 |
public static NumberFormat getCurrencyInstance(Locale inLocale) {
|
jtulach@1334
|
453 |
return getInstance(inLocale, CURRENCYSTYLE);
|
jtulach@1334
|
454 |
}
|
jtulach@1334
|
455 |
|
jtulach@1334
|
456 |
/**
|
jtulach@1334
|
457 |
* Returns a percentage format for the current default locale.
|
jtulach@1334
|
458 |
*/
|
jtulach@1334
|
459 |
public final static NumberFormat getPercentInstance() {
|
jtulach@1334
|
460 |
return getInstance(Locale.getDefault(Locale.Category.FORMAT), PERCENTSTYLE);
|
jtulach@1334
|
461 |
}
|
jtulach@1334
|
462 |
|
jtulach@1334
|
463 |
/**
|
jtulach@1334
|
464 |
* Returns a percentage format for the specified locale.
|
jtulach@1334
|
465 |
*/
|
jtulach@1334
|
466 |
public static NumberFormat getPercentInstance(Locale inLocale) {
|
jtulach@1334
|
467 |
return getInstance(inLocale, PERCENTSTYLE);
|
jtulach@1334
|
468 |
}
|
jtulach@1334
|
469 |
|
jtulach@1334
|
470 |
/**
|
jtulach@1334
|
471 |
* Returns a scientific format for the current default locale.
|
jtulach@1334
|
472 |
*/
|
jtulach@1334
|
473 |
/*public*/ final static NumberFormat getScientificInstance() {
|
jtulach@1334
|
474 |
return getInstance(Locale.getDefault(Locale.Category.FORMAT), SCIENTIFICSTYLE);
|
jtulach@1334
|
475 |
}
|
jtulach@1334
|
476 |
|
jtulach@1334
|
477 |
/**
|
jtulach@1334
|
478 |
* Returns a scientific format for the specified locale.
|
jtulach@1334
|
479 |
*/
|
jtulach@1334
|
480 |
/*public*/ static NumberFormat getScientificInstance(Locale inLocale) {
|
jtulach@1334
|
481 |
return getInstance(inLocale, SCIENTIFICSTYLE);
|
jtulach@1334
|
482 |
}
|
jtulach@1334
|
483 |
|
jtulach@1334
|
484 |
/**
|
jtulach@1334
|
485 |
* Returns an array of all locales for which the
|
jtulach@1334
|
486 |
* <code>get*Instance</code> methods of this class can return
|
jtulach@1334
|
487 |
* localized instances.
|
jtulach@1334
|
488 |
* The returned array represents the union of locales supported by the Java
|
jtulach@1334
|
489 |
* runtime and by installed
|
jtulach@1334
|
490 |
* {@link java.text.spi.NumberFormatProvider NumberFormatProvider} implementations.
|
jtulach@1334
|
491 |
* It must contain at least a <code>Locale</code> instance equal to
|
jtulach@1334
|
492 |
* {@link java.util.Locale#US Locale.US}.
|
jtulach@1334
|
493 |
*
|
jtulach@1334
|
494 |
* @return An array of locales for which localized
|
jtulach@1334
|
495 |
* <code>NumberFormat</code> instances are available.
|
jtulach@1334
|
496 |
*/
|
jtulach@1334
|
497 |
public static Locale[] getAvailableLocales() {
|
jtulach@1334
|
498 |
LocaleServiceProviderPool pool =
|
jtulach@1334
|
499 |
LocaleServiceProviderPool.getPool(NumberFormatProvider.class);
|
jtulach@1334
|
500 |
return pool.getAvailableLocales();
|
jtulach@1334
|
501 |
}
|
jtulach@1334
|
502 |
|
jtulach@1334
|
503 |
/**
|
jtulach@1334
|
504 |
* Overrides hashCode
|
jtulach@1334
|
505 |
*/
|
jtulach@1334
|
506 |
public int hashCode() {
|
jtulach@1334
|
507 |
return maximumIntegerDigits * 37 + maxFractionDigits;
|
jtulach@1334
|
508 |
// just enough fields for a reasonable distribution
|
jtulach@1334
|
509 |
}
|
jtulach@1334
|
510 |
|
jtulach@1334
|
511 |
/**
|
jtulach@1334
|
512 |
* Overrides equals
|
jtulach@1334
|
513 |
*/
|
jtulach@1334
|
514 |
public boolean equals(Object obj) {
|
jtulach@1334
|
515 |
if (obj == null) {
|
jtulach@1334
|
516 |
return false;
|
jtulach@1334
|
517 |
}
|
jtulach@1334
|
518 |
if (this == obj) {
|
jtulach@1334
|
519 |
return true;
|
jtulach@1334
|
520 |
}
|
jtulach@1334
|
521 |
if (getClass() != obj.getClass()) {
|
jtulach@1334
|
522 |
return false;
|
jtulach@1334
|
523 |
}
|
jtulach@1334
|
524 |
NumberFormat other = (NumberFormat) obj;
|
jtulach@1334
|
525 |
return (maximumIntegerDigits == other.maximumIntegerDigits
|
jtulach@1334
|
526 |
&& minimumIntegerDigits == other.minimumIntegerDigits
|
jtulach@1334
|
527 |
&& maximumFractionDigits == other.maximumFractionDigits
|
jtulach@1334
|
528 |
&& minimumFractionDigits == other.minimumFractionDigits
|
jtulach@1334
|
529 |
&& groupingUsed == other.groupingUsed
|
jtulach@1334
|
530 |
&& parseIntegerOnly == other.parseIntegerOnly);
|
jtulach@1334
|
531 |
}
|
jtulach@1334
|
532 |
|
jtulach@1334
|
533 |
/**
|
jtulach@1334
|
534 |
* Overrides Cloneable
|
jtulach@1334
|
535 |
*/
|
jtulach@1334
|
536 |
public Object clone() {
|
jtulach@1334
|
537 |
NumberFormat other = (NumberFormat) super.clone();
|
jtulach@1334
|
538 |
return other;
|
jtulach@1334
|
539 |
}
|
jtulach@1334
|
540 |
|
jtulach@1334
|
541 |
/**
|
jtulach@1334
|
542 |
* Returns true if grouping is used in this format. For example, in the
|
jtulach@1334
|
543 |
* English locale, with grouping on, the number 1234567 might be formatted
|
jtulach@1334
|
544 |
* as "1,234,567". The grouping separator as well as the size of each group
|
jtulach@1334
|
545 |
* is locale dependant and is determined by sub-classes of NumberFormat.
|
jtulach@1334
|
546 |
* @see #setGroupingUsed
|
jtulach@1334
|
547 |
*/
|
jtulach@1334
|
548 |
public boolean isGroupingUsed() {
|
jtulach@1334
|
549 |
return groupingUsed;
|
jtulach@1334
|
550 |
}
|
jtulach@1334
|
551 |
|
jtulach@1334
|
552 |
/**
|
jtulach@1334
|
553 |
* Set whether or not grouping will be used in this format.
|
jtulach@1334
|
554 |
* @see #isGroupingUsed
|
jtulach@1334
|
555 |
*/
|
jtulach@1334
|
556 |
public void setGroupingUsed(boolean newValue) {
|
jtulach@1334
|
557 |
groupingUsed = newValue;
|
jtulach@1334
|
558 |
}
|
jtulach@1334
|
559 |
|
jtulach@1334
|
560 |
/**
|
jtulach@1334
|
561 |
* Returns the maximum number of digits allowed in the integer portion of a
|
jtulach@1334
|
562 |
* number.
|
jtulach@1334
|
563 |
* @see #setMaximumIntegerDigits
|
jtulach@1334
|
564 |
*/
|
jtulach@1334
|
565 |
public int getMaximumIntegerDigits() {
|
jtulach@1334
|
566 |
return maximumIntegerDigits;
|
jtulach@1334
|
567 |
}
|
jtulach@1334
|
568 |
|
jtulach@1334
|
569 |
/**
|
jtulach@1334
|
570 |
* Sets the maximum number of digits allowed in the integer portion of a
|
jtulach@1334
|
571 |
* number. maximumIntegerDigits must be >= minimumIntegerDigits. If the
|
jtulach@1334
|
572 |
* new value for maximumIntegerDigits is less than the current value
|
jtulach@1334
|
573 |
* of minimumIntegerDigits, then minimumIntegerDigits will also be set to
|
jtulach@1334
|
574 |
* the new value.
|
jtulach@1334
|
575 |
* @param newValue the maximum number of integer digits to be shown; if
|
jtulach@1334
|
576 |
* less than zero, then zero is used. The concrete subclass may enforce an
|
jtulach@1334
|
577 |
* upper limit to this value appropriate to the numeric type being formatted.
|
jtulach@1334
|
578 |
* @see #getMaximumIntegerDigits
|
jtulach@1334
|
579 |
*/
|
jtulach@1334
|
580 |
public void setMaximumIntegerDigits(int newValue) {
|
jtulach@1334
|
581 |
maximumIntegerDigits = Math.max(0,newValue);
|
jtulach@1334
|
582 |
if (minimumIntegerDigits > maximumIntegerDigits) {
|
jtulach@1334
|
583 |
minimumIntegerDigits = maximumIntegerDigits;
|
jtulach@1334
|
584 |
}
|
jtulach@1334
|
585 |
}
|
jtulach@1334
|
586 |
|
jtulach@1334
|
587 |
/**
|
jtulach@1334
|
588 |
* Returns the minimum number of digits allowed in the integer portion of a
|
jtulach@1334
|
589 |
* number.
|
jtulach@1334
|
590 |
* @see #setMinimumIntegerDigits
|
jtulach@1334
|
591 |
*/
|
jtulach@1334
|
592 |
public int getMinimumIntegerDigits() {
|
jtulach@1334
|
593 |
return minimumIntegerDigits;
|
jtulach@1334
|
594 |
}
|
jtulach@1334
|
595 |
|
jtulach@1334
|
596 |
/**
|
jtulach@1334
|
597 |
* Sets the minimum number of digits allowed in the integer portion of a
|
jtulach@1334
|
598 |
* number. minimumIntegerDigits must be <= maximumIntegerDigits. If the
|
jtulach@1334
|
599 |
* new value for minimumIntegerDigits exceeds the current value
|
jtulach@1334
|
600 |
* of maximumIntegerDigits, then maximumIntegerDigits will also be set to
|
jtulach@1334
|
601 |
* the new value
|
jtulach@1334
|
602 |
* @param newValue the minimum number of integer digits to be shown; if
|
jtulach@1334
|
603 |
* less than zero, then zero is used. The concrete subclass may enforce an
|
jtulach@1334
|
604 |
* upper limit to this value appropriate to the numeric type being formatted.
|
jtulach@1334
|
605 |
* @see #getMinimumIntegerDigits
|
jtulach@1334
|
606 |
*/
|
jtulach@1334
|
607 |
public void setMinimumIntegerDigits(int newValue) {
|
jtulach@1334
|
608 |
minimumIntegerDigits = Math.max(0,newValue);
|
jtulach@1334
|
609 |
if (minimumIntegerDigits > maximumIntegerDigits) {
|
jtulach@1334
|
610 |
maximumIntegerDigits = minimumIntegerDigits;
|
jtulach@1334
|
611 |
}
|
jtulach@1334
|
612 |
}
|
jtulach@1334
|
613 |
|
jtulach@1334
|
614 |
/**
|
jtulach@1334
|
615 |
* Returns the maximum number of digits allowed in the fraction portion of a
|
jtulach@1334
|
616 |
* number.
|
jtulach@1334
|
617 |
* @see #setMaximumFractionDigits
|
jtulach@1334
|
618 |
*/
|
jtulach@1334
|
619 |
public int getMaximumFractionDigits() {
|
jtulach@1334
|
620 |
return maximumFractionDigits;
|
jtulach@1334
|
621 |
}
|
jtulach@1334
|
622 |
|
jtulach@1334
|
623 |
/**
|
jtulach@1334
|
624 |
* Sets the maximum number of digits allowed in the fraction portion of a
|
jtulach@1334
|
625 |
* number. maximumFractionDigits must be >= minimumFractionDigits. If the
|
jtulach@1334
|
626 |
* new value for maximumFractionDigits is less than the current value
|
jtulach@1334
|
627 |
* of minimumFractionDigits, then minimumFractionDigits will also be set to
|
jtulach@1334
|
628 |
* the new value.
|
jtulach@1334
|
629 |
* @param newValue the maximum number of fraction digits to be shown; if
|
jtulach@1334
|
630 |
* less than zero, then zero is used. The concrete subclass may enforce an
|
jtulach@1334
|
631 |
* upper limit to this value appropriate to the numeric type being formatted.
|
jtulach@1334
|
632 |
* @see #getMaximumFractionDigits
|
jtulach@1334
|
633 |
*/
|
jtulach@1334
|
634 |
public void setMaximumFractionDigits(int newValue) {
|
jtulach@1334
|
635 |
maximumFractionDigits = Math.max(0,newValue);
|
jtulach@1334
|
636 |
if (maximumFractionDigits < minimumFractionDigits) {
|
jtulach@1334
|
637 |
minimumFractionDigits = maximumFractionDigits;
|
jtulach@1334
|
638 |
}
|
jtulach@1334
|
639 |
}
|
jtulach@1334
|
640 |
|
jtulach@1334
|
641 |
/**
|
jtulach@1334
|
642 |
* Returns the minimum number of digits allowed in the fraction portion of a
|
jtulach@1334
|
643 |
* number.
|
jtulach@1334
|
644 |
* @see #setMinimumFractionDigits
|
jtulach@1334
|
645 |
*/
|
jtulach@1334
|
646 |
public int getMinimumFractionDigits() {
|
jtulach@1334
|
647 |
return minimumFractionDigits;
|
jtulach@1334
|
648 |
}
|
jtulach@1334
|
649 |
|
jtulach@1334
|
650 |
/**
|
jtulach@1334
|
651 |
* Sets the minimum number of digits allowed in the fraction portion of a
|
jtulach@1334
|
652 |
* number. minimumFractionDigits must be <= maximumFractionDigits. If the
|
jtulach@1334
|
653 |
* new value for minimumFractionDigits exceeds the current value
|
jtulach@1334
|
654 |
* of maximumFractionDigits, then maximumIntegerDigits will also be set to
|
jtulach@1334
|
655 |
* the new value
|
jtulach@1334
|
656 |
* @param newValue the minimum number of fraction digits to be shown; if
|
jtulach@1334
|
657 |
* less than zero, then zero is used. The concrete subclass may enforce an
|
jtulach@1334
|
658 |
* upper limit to this value appropriate to the numeric type being formatted.
|
jtulach@1334
|
659 |
* @see #getMinimumFractionDigits
|
jtulach@1334
|
660 |
*/
|
jtulach@1334
|
661 |
public void setMinimumFractionDigits(int newValue) {
|
jtulach@1334
|
662 |
minimumFractionDigits = Math.max(0,newValue);
|
jtulach@1334
|
663 |
if (maximumFractionDigits < minimumFractionDigits) {
|
jtulach@1334
|
664 |
maximumFractionDigits = minimumFractionDigits;
|
jtulach@1334
|
665 |
}
|
jtulach@1334
|
666 |
}
|
jtulach@1334
|
667 |
|
jtulach@1334
|
668 |
/**
|
jtulach@1334
|
669 |
* Gets the currency used by this number format when formatting
|
jtulach@1334
|
670 |
* currency values. The initial value is derived in a locale dependent
|
jtulach@1334
|
671 |
* way. The returned value may be null if no valid
|
jtulach@1334
|
672 |
* currency could be determined and no currency has been set using
|
jtulach@1334
|
673 |
* {@link #setCurrency(java.util.Currency) setCurrency}.
|
jtulach@1334
|
674 |
* <p>
|
jtulach@1334
|
675 |
* The default implementation throws
|
jtulach@1334
|
676 |
* <code>UnsupportedOperationException</code>.
|
jtulach@1334
|
677 |
*
|
jtulach@1334
|
678 |
* @return the currency used by this number format, or <code>null</code>
|
jtulach@1334
|
679 |
* @exception UnsupportedOperationException if the number format class
|
jtulach@1334
|
680 |
* doesn't implement currency formatting
|
jtulach@1334
|
681 |
* @since 1.4
|
jtulach@1334
|
682 |
*/
|
jtulach@1334
|
683 |
public Currency getCurrency() {
|
jtulach@1334
|
684 |
throw new UnsupportedOperationException();
|
jtulach@1334
|
685 |
}
|
jtulach@1334
|
686 |
|
jtulach@1334
|
687 |
/**
|
jtulach@1334
|
688 |
* Sets the currency used by this number format when formatting
|
jtulach@1334
|
689 |
* currency values. This does not update the minimum or maximum
|
jtulach@1334
|
690 |
* number of fraction digits used by the number format.
|
jtulach@1334
|
691 |
* <p>
|
jtulach@1334
|
692 |
* The default implementation throws
|
jtulach@1334
|
693 |
* <code>UnsupportedOperationException</code>.
|
jtulach@1334
|
694 |
*
|
jtulach@1334
|
695 |
* @param currency the new currency to be used by this number format
|
jtulach@1334
|
696 |
* @exception UnsupportedOperationException if the number format class
|
jtulach@1334
|
697 |
* doesn't implement currency formatting
|
jtulach@1334
|
698 |
* @exception NullPointerException if <code>currency</code> is null
|
jtulach@1334
|
699 |
* @since 1.4
|
jtulach@1334
|
700 |
*/
|
jtulach@1334
|
701 |
public void setCurrency(Currency currency) {
|
jtulach@1334
|
702 |
throw new UnsupportedOperationException();
|
jtulach@1334
|
703 |
}
|
jtulach@1334
|
704 |
|
jtulach@1334
|
705 |
/**
|
jtulach@1334
|
706 |
* Gets the {@link java.math.RoundingMode} used in this NumberFormat.
|
jtulach@1334
|
707 |
* The default implementation of this method in NumberFormat
|
jtulach@1334
|
708 |
* always throws {@link java.lang.UnsupportedOperationException}.
|
jtulach@1334
|
709 |
* Subclasses which handle different rounding modes should override
|
jtulach@1334
|
710 |
* this method.
|
jtulach@1334
|
711 |
*
|
jtulach@1334
|
712 |
* @exception UnsupportedOperationException The default implementation
|
jtulach@1334
|
713 |
* always throws this exception
|
jtulach@1334
|
714 |
* @return The <code>RoundingMode</code> used for this NumberFormat.
|
jtulach@1334
|
715 |
* @see #setRoundingMode(RoundingMode)
|
jtulach@1334
|
716 |
* @since 1.6
|
jtulach@1334
|
717 |
*/
|
jtulach@1334
|
718 |
public RoundingMode getRoundingMode() {
|
jtulach@1334
|
719 |
throw new UnsupportedOperationException();
|
jtulach@1334
|
720 |
}
|
jtulach@1334
|
721 |
|
jtulach@1334
|
722 |
/**
|
jtulach@1334
|
723 |
* Sets the {@link java.math.RoundingMode} used in this NumberFormat.
|
jtulach@1334
|
724 |
* The default implementation of this method in NumberFormat always
|
jtulach@1334
|
725 |
* throws {@link java.lang.UnsupportedOperationException}.
|
jtulach@1334
|
726 |
* Subclasses which handle different rounding modes should override
|
jtulach@1334
|
727 |
* this method.
|
jtulach@1334
|
728 |
*
|
jtulach@1334
|
729 |
* @exception UnsupportedOperationException The default implementation
|
jtulach@1334
|
730 |
* always throws this exception
|
jtulach@1334
|
731 |
* @exception NullPointerException if <code>roundingMode</code> is null
|
jtulach@1334
|
732 |
* @param roundingMode The <code>RoundingMode</code> to be used
|
jtulach@1334
|
733 |
* @see #getRoundingMode()
|
jtulach@1334
|
734 |
* @since 1.6
|
jtulach@1334
|
735 |
*/
|
jtulach@1334
|
736 |
public void setRoundingMode(RoundingMode roundingMode) {
|
jtulach@1334
|
737 |
throw new UnsupportedOperationException();
|
jtulach@1334
|
738 |
}
|
jtulach@1334
|
739 |
|
jtulach@1334
|
740 |
// =======================privates===============================
|
jtulach@1334
|
741 |
|
jtulach@1334
|
742 |
private static NumberFormat getInstance(Locale desiredLocale,
|
jtulach@1334
|
743 |
int choice) {
|
jtulach@1334
|
744 |
// Check whether a provider can provide an implementation that's closer
|
jtulach@1334
|
745 |
// to the requested locale than what the Java runtime itself can provide.
|
jtulach@1334
|
746 |
LocaleServiceProviderPool pool =
|
jtulach@1334
|
747 |
LocaleServiceProviderPool.getPool(NumberFormatProvider.class);
|
jtulach@1334
|
748 |
if (pool.hasProviders()) {
|
jtulach@1334
|
749 |
NumberFormat providersInstance = pool.getLocalizedObject(
|
jtulach@1334
|
750 |
NumberFormatGetter.INSTANCE,
|
jtulach@1334
|
751 |
desiredLocale,
|
jtulach@1334
|
752 |
choice);
|
jtulach@1334
|
753 |
if (providersInstance != null) {
|
jtulach@1334
|
754 |
return providersInstance;
|
jtulach@1334
|
755 |
}
|
jtulach@1334
|
756 |
}
|
jtulach@1334
|
757 |
|
jtulach@1334
|
758 |
/* try the cache first */
|
jtulach@1334
|
759 |
String[] numberPatterns = (String[])cachedLocaleData.get(desiredLocale);
|
jtulach@1334
|
760 |
if (numberPatterns == null) { /* cache miss */
|
jtulach@1334
|
761 |
ResourceBundle resource = LocaleData.getNumberFormatData(desiredLocale);
|
jtulach@1334
|
762 |
numberPatterns = resource.getStringArray("NumberPatterns");
|
jtulach@1334
|
763 |
/* update cache */
|
jtulach@1334
|
764 |
cachedLocaleData.put(desiredLocale, numberPatterns);
|
jtulach@1334
|
765 |
}
|
jtulach@1334
|
766 |
|
jtulach@1334
|
767 |
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(desiredLocale);
|
jtulach@1334
|
768 |
int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice;
|
jtulach@1334
|
769 |
DecimalFormat format = new DecimalFormat(numberPatterns[entry], symbols);
|
jtulach@1334
|
770 |
|
jtulach@1334
|
771 |
if (choice == INTEGERSTYLE) {
|
jtulach@1334
|
772 |
format.setMaximumFractionDigits(0);
|
jtulach@1334
|
773 |
format.setDecimalSeparatorAlwaysShown(false);
|
jtulach@1334
|
774 |
format.setParseIntegerOnly(true);
|
jtulach@1334
|
775 |
} else if (choice == CURRENCYSTYLE) {
|
jtulach@1334
|
776 |
format.adjustForCurrencyDefaultFractionDigits();
|
jtulach@1334
|
777 |
}
|
jtulach@1334
|
778 |
|
jtulach@1334
|
779 |
return format;
|
jtulach@1334
|
780 |
}
|
jtulach@1334
|
781 |
|
jtulach@1334
|
782 |
/**
|
jtulach@1334
|
783 |
* First, read in the default serializable data.
|
jtulach@1334
|
784 |
*
|
jtulach@1334
|
785 |
* Then, if <code>serialVersionOnStream</code> is less than 1, indicating that
|
jtulach@1334
|
786 |
* the stream was written by JDK 1.1,
|
jtulach@1334
|
787 |
* set the <code>int</code> fields such as <code>maximumIntegerDigits</code>
|
jtulach@1334
|
788 |
* to be equal to the <code>byte</code> fields such as <code>maxIntegerDigits</code>,
|
jtulach@1334
|
789 |
* since the <code>int</code> fields were not present in JDK 1.1.
|
jtulach@1334
|
790 |
* Finally, set serialVersionOnStream back to the maximum allowed value so that
|
jtulach@1334
|
791 |
* default serialization will work properly if this object is streamed out again.
|
jtulach@1334
|
792 |
*
|
jtulach@1334
|
793 |
* <p>If <code>minimumIntegerDigits</code> is greater than
|
jtulach@1334
|
794 |
* <code>maximumIntegerDigits</code> or <code>minimumFractionDigits</code>
|
jtulach@1334
|
795 |
* is greater than <code>maximumFractionDigits</code>, then the stream data
|
jtulach@1334
|
796 |
* is invalid and this method throws an <code>InvalidObjectException</code>.
|
jtulach@1334
|
797 |
* In addition, if any of these values is negative, then this method throws
|
jtulach@1334
|
798 |
* an <code>InvalidObjectException</code>.
|
jtulach@1334
|
799 |
*
|
jtulach@1334
|
800 |
* @since 1.2
|
jtulach@1334
|
801 |
*/
|
jtulach@1334
|
802 |
private void readObject(ObjectInputStream stream)
|
jtulach@1334
|
803 |
throws IOException, ClassNotFoundException
|
jtulach@1334
|
804 |
{
|
jtulach@1334
|
805 |
stream.defaultReadObject();
|
jtulach@1334
|
806 |
if (serialVersionOnStream < 1) {
|
jtulach@1334
|
807 |
// Didn't have additional int fields, reassign to use them.
|
jtulach@1334
|
808 |
maximumIntegerDigits = maxIntegerDigits;
|
jtulach@1334
|
809 |
minimumIntegerDigits = minIntegerDigits;
|
jtulach@1334
|
810 |
maximumFractionDigits = maxFractionDigits;
|
jtulach@1334
|
811 |
minimumFractionDigits = minFractionDigits;
|
jtulach@1334
|
812 |
}
|
jtulach@1334
|
813 |
if (minimumIntegerDigits > maximumIntegerDigits ||
|
jtulach@1334
|
814 |
minimumFractionDigits > maximumFractionDigits ||
|
jtulach@1334
|
815 |
minimumIntegerDigits < 0 || minimumFractionDigits < 0) {
|
jtulach@1334
|
816 |
throw new InvalidObjectException("Digit count range invalid");
|
jtulach@1334
|
817 |
}
|
jtulach@1334
|
818 |
serialVersionOnStream = currentSerialVersion;
|
jtulach@1334
|
819 |
}
|
jtulach@1334
|
820 |
|
jtulach@1334
|
821 |
/**
|
jtulach@1334
|
822 |
* Write out the default serializable data, after first setting
|
jtulach@1334
|
823 |
* the <code>byte</code> fields such as <code>maxIntegerDigits</code> to be
|
jtulach@1334
|
824 |
* equal to the <code>int</code> fields such as <code>maximumIntegerDigits</code>
|
jtulach@1334
|
825 |
* (or to <code>Byte.MAX_VALUE</code>, whichever is smaller), for compatibility
|
jtulach@1334
|
826 |
* with the JDK 1.1 version of the stream format.
|
jtulach@1334
|
827 |
*
|
jtulach@1334
|
828 |
* @since 1.2
|
jtulach@1334
|
829 |
*/
|
jtulach@1334
|
830 |
private void writeObject(ObjectOutputStream stream)
|
jtulach@1334
|
831 |
throws IOException
|
jtulach@1334
|
832 |
{
|
jtulach@1334
|
833 |
maxIntegerDigits = (maximumIntegerDigits > Byte.MAX_VALUE) ?
|
jtulach@1334
|
834 |
Byte.MAX_VALUE : (byte)maximumIntegerDigits;
|
jtulach@1334
|
835 |
minIntegerDigits = (minimumIntegerDigits > Byte.MAX_VALUE) ?
|
jtulach@1334
|
836 |
Byte.MAX_VALUE : (byte)minimumIntegerDigits;
|
jtulach@1334
|
837 |
maxFractionDigits = (maximumFractionDigits > Byte.MAX_VALUE) ?
|
jtulach@1334
|
838 |
Byte.MAX_VALUE : (byte)maximumFractionDigits;
|
jtulach@1334
|
839 |
minFractionDigits = (minimumFractionDigits > Byte.MAX_VALUE) ?
|
jtulach@1334
|
840 |
Byte.MAX_VALUE : (byte)minimumFractionDigits;
|
jtulach@1334
|
841 |
stream.defaultWriteObject();
|
jtulach@1334
|
842 |
}
|
jtulach@1334
|
843 |
|
jtulach@1334
|
844 |
/**
|
jtulach@1334
|
845 |
* Cache to hold the NumberPatterns of a Locale.
|
jtulach@1334
|
846 |
*/
|
jtulach@1334
|
847 |
private static final Hashtable cachedLocaleData = new Hashtable(3);
|
jtulach@1334
|
848 |
|
jtulach@1334
|
849 |
// Constants used by factory methods to specify a style of format.
|
jtulach@1334
|
850 |
private static final int NUMBERSTYLE = 0;
|
jtulach@1334
|
851 |
private static final int CURRENCYSTYLE = 1;
|
jtulach@1334
|
852 |
private static final int PERCENTSTYLE = 2;
|
jtulach@1334
|
853 |
private static final int SCIENTIFICSTYLE = 3;
|
jtulach@1334
|
854 |
private static final int INTEGERSTYLE = 4;
|
jtulach@1334
|
855 |
|
jtulach@1334
|
856 |
/**
|
jtulach@1334
|
857 |
* True if the grouping (i.e. thousands) separator is used when
|
jtulach@1334
|
858 |
* formatting and parsing numbers.
|
jtulach@1334
|
859 |
*
|
jtulach@1334
|
860 |
* @serial
|
jtulach@1334
|
861 |
* @see #isGroupingUsed
|
jtulach@1334
|
862 |
*/
|
jtulach@1334
|
863 |
private boolean groupingUsed = true;
|
jtulach@1334
|
864 |
|
jtulach@1334
|
865 |
/**
|
jtulach@1334
|
866 |
* The maximum number of digits allowed in the integer portion of a
|
jtulach@1334
|
867 |
* number. <code>maxIntegerDigits</code> must be greater than or equal to
|
jtulach@1334
|
868 |
* <code>minIntegerDigits</code>.
|
jtulach@1334
|
869 |
* <p>
|
jtulach@1334
|
870 |
* <strong>Note:</strong> This field exists only for serialization
|
jtulach@1334
|
871 |
* compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new
|
jtulach@1334
|
872 |
* <code>int</code> field <code>maximumIntegerDigits</code> is used instead.
|
jtulach@1334
|
873 |
* When writing to a stream, <code>maxIntegerDigits</code> is set to
|
jtulach@1334
|
874 |
* <code>maximumIntegerDigits</code> or <code>Byte.MAX_VALUE</code>,
|
jtulach@1334
|
875 |
* whichever is smaller. When reading from a stream, this field is used
|
jtulach@1334
|
876 |
* only if <code>serialVersionOnStream</code> is less than 1.
|
jtulach@1334
|
877 |
*
|
jtulach@1334
|
878 |
* @serial
|
jtulach@1334
|
879 |
* @see #getMaximumIntegerDigits
|
jtulach@1334
|
880 |
*/
|
jtulach@1334
|
881 |
private byte maxIntegerDigits = 40;
|
jtulach@1334
|
882 |
|
jtulach@1334
|
883 |
/**
|
jtulach@1334
|
884 |
* The minimum number of digits allowed in the integer portion of a
|
jtulach@1334
|
885 |
* number. <code>minimumIntegerDigits</code> must be less than or equal to
|
jtulach@1334
|
886 |
* <code>maximumIntegerDigits</code>.
|
jtulach@1334
|
887 |
* <p>
|
jtulach@1334
|
888 |
* <strong>Note:</strong> This field exists only for serialization
|
jtulach@1334
|
889 |
* compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new
|
jtulach@1334
|
890 |
* <code>int</code> field <code>minimumIntegerDigits</code> is used instead.
|
jtulach@1334
|
891 |
* When writing to a stream, <code>minIntegerDigits</code> is set to
|
jtulach@1334
|
892 |
* <code>minimumIntegerDigits</code> or <code>Byte.MAX_VALUE</code>,
|
jtulach@1334
|
893 |
* whichever is smaller. When reading from a stream, this field is used
|
jtulach@1334
|
894 |
* only if <code>serialVersionOnStream</code> is less than 1.
|
jtulach@1334
|
895 |
*
|
jtulach@1334
|
896 |
* @serial
|
jtulach@1334
|
897 |
* @see #getMinimumIntegerDigits
|
jtulach@1334
|
898 |
*/
|
jtulach@1334
|
899 |
private byte minIntegerDigits = 1;
|
jtulach@1334
|
900 |
|
jtulach@1334
|
901 |
/**
|
jtulach@1334
|
902 |
* The maximum number of digits allowed in the fractional portion of a
|
jtulach@1334
|
903 |
* number. <code>maximumFractionDigits</code> must be greater than or equal to
|
jtulach@1334
|
904 |
* <code>minimumFractionDigits</code>.
|
jtulach@1334
|
905 |
* <p>
|
jtulach@1334
|
906 |
* <strong>Note:</strong> This field exists only for serialization
|
jtulach@1334
|
907 |
* compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new
|
jtulach@1334
|
908 |
* <code>int</code> field <code>maximumFractionDigits</code> is used instead.
|
jtulach@1334
|
909 |
* When writing to a stream, <code>maxFractionDigits</code> is set to
|
jtulach@1334
|
910 |
* <code>maximumFractionDigits</code> or <code>Byte.MAX_VALUE</code>,
|
jtulach@1334
|
911 |
* whichever is smaller. When reading from a stream, this field is used
|
jtulach@1334
|
912 |
* only if <code>serialVersionOnStream</code> is less than 1.
|
jtulach@1334
|
913 |
*
|
jtulach@1334
|
914 |
* @serial
|
jtulach@1334
|
915 |
* @see #getMaximumFractionDigits
|
jtulach@1334
|
916 |
*/
|
jtulach@1334
|
917 |
private byte maxFractionDigits = 3; // invariant, >= minFractionDigits
|
jtulach@1334
|
918 |
|
jtulach@1334
|
919 |
/**
|
jtulach@1334
|
920 |
* The minimum number of digits allowed in the fractional portion of a
|
jtulach@1334
|
921 |
* number. <code>minimumFractionDigits</code> must be less than or equal to
|
jtulach@1334
|
922 |
* <code>maximumFractionDigits</code>.
|
jtulach@1334
|
923 |
* <p>
|
jtulach@1334
|
924 |
* <strong>Note:</strong> This field exists only for serialization
|
jtulach@1334
|
925 |
* compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new
|
jtulach@1334
|
926 |
* <code>int</code> field <code>minimumFractionDigits</code> is used instead.
|
jtulach@1334
|
927 |
* When writing to a stream, <code>minFractionDigits</code> is set to
|
jtulach@1334
|
928 |
* <code>minimumFractionDigits</code> or <code>Byte.MAX_VALUE</code>,
|
jtulach@1334
|
929 |
* whichever is smaller. When reading from a stream, this field is used
|
jtulach@1334
|
930 |
* only if <code>serialVersionOnStream</code> is less than 1.
|
jtulach@1334
|
931 |
*
|
jtulach@1334
|
932 |
* @serial
|
jtulach@1334
|
933 |
* @see #getMinimumFractionDigits
|
jtulach@1334
|
934 |
*/
|
jtulach@1334
|
935 |
private byte minFractionDigits = 0;
|
jtulach@1334
|
936 |
|
jtulach@1334
|
937 |
/**
|
jtulach@1334
|
938 |
* True if this format will parse numbers as integers only.
|
jtulach@1334
|
939 |
*
|
jtulach@1334
|
940 |
* @serial
|
jtulach@1334
|
941 |
* @see #isParseIntegerOnly
|
jtulach@1334
|
942 |
*/
|
jtulach@1334
|
943 |
private boolean parseIntegerOnly = false;
|
jtulach@1334
|
944 |
|
jtulach@1334
|
945 |
// new fields for 1.2. byte is too small for integer digits.
|
jtulach@1334
|
946 |
|
jtulach@1334
|
947 |
/**
|
jtulach@1334
|
948 |
* The maximum number of digits allowed in the integer portion of a
|
jtulach@1334
|
949 |
* number. <code>maximumIntegerDigits</code> must be greater than or equal to
|
jtulach@1334
|
950 |
* <code>minimumIntegerDigits</code>.
|
jtulach@1334
|
951 |
*
|
jtulach@1334
|
952 |
* @serial
|
jtulach@1334
|
953 |
* @since 1.2
|
jtulach@1334
|
954 |
* @see #getMaximumIntegerDigits
|
jtulach@1334
|
955 |
*/
|
jtulach@1334
|
956 |
private int maximumIntegerDigits = 40;
|
jtulach@1334
|
957 |
|
jtulach@1334
|
958 |
/**
|
jtulach@1334
|
959 |
* The minimum number of digits allowed in the integer portion of a
|
jtulach@1334
|
960 |
* number. <code>minimumIntegerDigits</code> must be less than or equal to
|
jtulach@1334
|
961 |
* <code>maximumIntegerDigits</code>.
|
jtulach@1334
|
962 |
*
|
jtulach@1334
|
963 |
* @serial
|
jtulach@1334
|
964 |
* @since 1.2
|
jtulach@1334
|
965 |
* @see #getMinimumIntegerDigits
|
jtulach@1334
|
966 |
*/
|
jtulach@1334
|
967 |
private int minimumIntegerDigits = 1;
|
jtulach@1334
|
968 |
|
jtulach@1334
|
969 |
/**
|
jtulach@1334
|
970 |
* The maximum number of digits allowed in the fractional portion of a
|
jtulach@1334
|
971 |
* number. <code>maximumFractionDigits</code> must be greater than or equal to
|
jtulach@1334
|
972 |
* <code>minimumFractionDigits</code>.
|
jtulach@1334
|
973 |
*
|
jtulach@1334
|
974 |
* @serial
|
jtulach@1334
|
975 |
* @since 1.2
|
jtulach@1334
|
976 |
* @see #getMaximumFractionDigits
|
jtulach@1334
|
977 |
*/
|
jtulach@1334
|
978 |
private int maximumFractionDigits = 3; // invariant, >= minFractionDigits
|
jtulach@1334
|
979 |
|
jtulach@1334
|
980 |
/**
|
jtulach@1334
|
981 |
* The minimum number of digits allowed in the fractional portion of a
|
jtulach@1334
|
982 |
* number. <code>minimumFractionDigits</code> must be less than or equal to
|
jtulach@1334
|
983 |
* <code>maximumFractionDigits</code>.
|
jtulach@1334
|
984 |
*
|
jtulach@1334
|
985 |
* @serial
|
jtulach@1334
|
986 |
* @since 1.2
|
jtulach@1334
|
987 |
* @see #getMinimumFractionDigits
|
jtulach@1334
|
988 |
*/
|
jtulach@1334
|
989 |
private int minimumFractionDigits = 0;
|
jtulach@1334
|
990 |
|
jtulach@1334
|
991 |
static final int currentSerialVersion = 1;
|
jtulach@1334
|
992 |
|
jtulach@1334
|
993 |
/**
|
jtulach@1334
|
994 |
* Describes the version of <code>NumberFormat</code> present on the stream.
|
jtulach@1334
|
995 |
* Possible values are:
|
jtulach@1334
|
996 |
* <ul>
|
jtulach@1334
|
997 |
* <li><b>0</b> (or uninitialized): the JDK 1.1 version of the stream format.
|
jtulach@1334
|
998 |
* In this version, the <code>int</code> fields such as
|
jtulach@1334
|
999 |
* <code>maximumIntegerDigits</code> were not present, and the <code>byte</code>
|
jtulach@1334
|
1000 |
* fields such as <code>maxIntegerDigits</code> are used instead.
|
jtulach@1334
|
1001 |
*
|
jtulach@1334
|
1002 |
* <li><b>1</b>: the 1.2 version of the stream format. The values of the
|
jtulach@1334
|
1003 |
* <code>byte</code> fields such as <code>maxIntegerDigits</code> are ignored,
|
jtulach@1334
|
1004 |
* and the <code>int</code> fields such as <code>maximumIntegerDigits</code>
|
jtulach@1334
|
1005 |
* are used instead.
|
jtulach@1334
|
1006 |
* </ul>
|
jtulach@1334
|
1007 |
* When streaming out a <code>NumberFormat</code>, the most recent format
|
jtulach@1334
|
1008 |
* (corresponding to the highest allowable <code>serialVersionOnStream</code>)
|
jtulach@1334
|
1009 |
* is always written.
|
jtulach@1334
|
1010 |
*
|
jtulach@1334
|
1011 |
* @serial
|
jtulach@1334
|
1012 |
* @since 1.2
|
jtulach@1334
|
1013 |
*/
|
jtulach@1334
|
1014 |
private int serialVersionOnStream = currentSerialVersion;
|
jtulach@1334
|
1015 |
|
jtulach@1334
|
1016 |
// Removed "implements Cloneable" clause. Needs to update serialization
|
jtulach@1334
|
1017 |
// ID for backward compatibility.
|
jtulach@1334
|
1018 |
static final long serialVersionUID = -2308460125733713944L;
|
jtulach@1334
|
1019 |
|
jtulach@1334
|
1020 |
|
jtulach@1334
|
1021 |
//
|
jtulach@1334
|
1022 |
// class for AttributedCharacterIterator attributes
|
jtulach@1334
|
1023 |
//
|
jtulach@1334
|
1024 |
/**
|
jtulach@1334
|
1025 |
* Defines constants that are used as attribute keys in the
|
jtulach@1334
|
1026 |
* <code>AttributedCharacterIterator</code> returned
|
jtulach@1334
|
1027 |
* from <code>NumberFormat.formatToCharacterIterator</code> and as
|
jtulach@1334
|
1028 |
* field identifiers in <code>FieldPosition</code>.
|
jtulach@1334
|
1029 |
*
|
jtulach@1334
|
1030 |
* @since 1.4
|
jtulach@1334
|
1031 |
*/
|
jtulach@1334
|
1032 |
public static class Field extends Format.Field {
|
jtulach@1334
|
1033 |
|
jtulach@1334
|
1034 |
// Proclaim serial compatibility with 1.4 FCS
|
jtulach@1334
|
1035 |
private static final long serialVersionUID = 7494728892700160890L;
|
jtulach@1334
|
1036 |
|
jtulach@1334
|
1037 |
// table of all instances in this class, used by readResolve
|
jtulach@1334
|
1038 |
private static final Map instanceMap = new HashMap(11);
|
jtulach@1334
|
1039 |
|
jtulach@1334
|
1040 |
/**
|
jtulach@1334
|
1041 |
* Creates a Field instance with the specified
|
jtulach@1334
|
1042 |
* name.
|
jtulach@1334
|
1043 |
*
|
jtulach@1334
|
1044 |
* @param name Name of the attribute
|
jtulach@1334
|
1045 |
*/
|
jtulach@1334
|
1046 |
protected Field(String name) {
|
jtulach@1334
|
1047 |
super(name);
|
jtulach@1334
|
1048 |
if (this.getClass() == NumberFormat.Field.class) {
|
jtulach@1334
|
1049 |
instanceMap.put(name, this);
|
jtulach@1334
|
1050 |
}
|
jtulach@1334
|
1051 |
}
|
jtulach@1334
|
1052 |
|
jtulach@1334
|
1053 |
/**
|
jtulach@1334
|
1054 |
* Resolves instances being deserialized to the predefined constants.
|
jtulach@1334
|
1055 |
*
|
jtulach@1334
|
1056 |
* @throws InvalidObjectException if the constant could not be resolved.
|
jtulach@1334
|
1057 |
* @return resolved NumberFormat.Field constant
|
jtulach@1334
|
1058 |
*/
|
jtulach@1334
|
1059 |
protected Object readResolve() throws InvalidObjectException {
|
jtulach@1334
|
1060 |
if (this.getClass() != NumberFormat.Field.class) {
|
jtulach@1334
|
1061 |
throw new InvalidObjectException("subclass didn't correctly implement readResolve");
|
jtulach@1334
|
1062 |
}
|
jtulach@1334
|
1063 |
|
jtulach@1334
|
1064 |
Object instance = instanceMap.get(getName());
|
jtulach@1334
|
1065 |
if (instance != null) {
|
jtulach@1334
|
1066 |
return instance;
|
jtulach@1334
|
1067 |
} else {
|
jtulach@1334
|
1068 |
throw new InvalidObjectException("unknown attribute name");
|
jtulach@1334
|
1069 |
}
|
jtulach@1334
|
1070 |
}
|
jtulach@1334
|
1071 |
|
jtulach@1334
|
1072 |
/**
|
jtulach@1334
|
1073 |
* Constant identifying the integer field.
|
jtulach@1334
|
1074 |
*/
|
jtulach@1334
|
1075 |
public static final Field INTEGER = new Field("integer");
|
jtulach@1334
|
1076 |
|
jtulach@1334
|
1077 |
/**
|
jtulach@1334
|
1078 |
* Constant identifying the fraction field.
|
jtulach@1334
|
1079 |
*/
|
jtulach@1334
|
1080 |
public static final Field FRACTION = new Field("fraction");
|
jtulach@1334
|
1081 |
|
jtulach@1334
|
1082 |
/**
|
jtulach@1334
|
1083 |
* Constant identifying the exponent field.
|
jtulach@1334
|
1084 |
*/
|
jtulach@1334
|
1085 |
public static final Field EXPONENT = new Field("exponent");
|
jtulach@1334
|
1086 |
|
jtulach@1334
|
1087 |
/**
|
jtulach@1334
|
1088 |
* Constant identifying the decimal separator field.
|
jtulach@1334
|
1089 |
*/
|
jtulach@1334
|
1090 |
public static final Field DECIMAL_SEPARATOR =
|
jtulach@1334
|
1091 |
new Field("decimal separator");
|
jtulach@1334
|
1092 |
|
jtulach@1334
|
1093 |
/**
|
jtulach@1334
|
1094 |
* Constant identifying the sign field.
|
jtulach@1334
|
1095 |
*/
|
jtulach@1334
|
1096 |
public static final Field SIGN = new Field("sign");
|
jtulach@1334
|
1097 |
|
jtulach@1334
|
1098 |
/**
|
jtulach@1334
|
1099 |
* Constant identifying the grouping separator field.
|
jtulach@1334
|
1100 |
*/
|
jtulach@1334
|
1101 |
public static final Field GROUPING_SEPARATOR =
|
jtulach@1334
|
1102 |
new Field("grouping separator");
|
jtulach@1334
|
1103 |
|
jtulach@1334
|
1104 |
/**
|
jtulach@1334
|
1105 |
* Constant identifying the exponent symbol field.
|
jtulach@1334
|
1106 |
*/
|
jtulach@1334
|
1107 |
public static final Field EXPONENT_SYMBOL = new
|
jtulach@1334
|
1108 |
Field("exponent symbol");
|
jtulach@1334
|
1109 |
|
jtulach@1334
|
1110 |
/**
|
jtulach@1334
|
1111 |
* Constant identifying the percent field.
|
jtulach@1334
|
1112 |
*/
|
jtulach@1334
|
1113 |
public static final Field PERCENT = new Field("percent");
|
jtulach@1334
|
1114 |
|
jtulach@1334
|
1115 |
/**
|
jtulach@1334
|
1116 |
* Constant identifying the permille field.
|
jtulach@1334
|
1117 |
*/
|
jtulach@1334
|
1118 |
public static final Field PERMILLE = new Field("per mille");
|
jtulach@1334
|
1119 |
|
jtulach@1334
|
1120 |
/**
|
jtulach@1334
|
1121 |
* Constant identifying the currency field.
|
jtulach@1334
|
1122 |
*/
|
jtulach@1334
|
1123 |
public static final Field CURRENCY = new Field("currency");
|
jtulach@1334
|
1124 |
|
jtulach@1334
|
1125 |
/**
|
jtulach@1334
|
1126 |
* Constant identifying the exponent sign field.
|
jtulach@1334
|
1127 |
*/
|
jtulach@1334
|
1128 |
public static final Field EXPONENT_SIGN = new Field("exponent sign");
|
jtulach@1334
|
1129 |
}
|
jtulach@1334
|
1130 |
|
jtulach@1334
|
1131 |
/**
|
jtulach@1334
|
1132 |
* Obtains a NumberFormat instance from a NumberFormatProvider implementation.
|
jtulach@1334
|
1133 |
*/
|
jtulach@1334
|
1134 |
private static class NumberFormatGetter
|
jtulach@1334
|
1135 |
implements LocaleServiceProviderPool.LocalizedObjectGetter<NumberFormatProvider,
|
jtulach@1334
|
1136 |
NumberFormat> {
|
jtulach@1334
|
1137 |
private static final NumberFormatGetter INSTANCE = new NumberFormatGetter();
|
jtulach@1334
|
1138 |
|
jtulach@1334
|
1139 |
public NumberFormat getObject(NumberFormatProvider numberFormatProvider,
|
jtulach@1334
|
1140 |
Locale locale,
|
jtulach@1334
|
1141 |
String key,
|
jtulach@1334
|
1142 |
Object... params) {
|
jtulach@1334
|
1143 |
assert params.length == 1;
|
jtulach@1334
|
1144 |
int choice = (Integer)params[0];
|
jtulach@1334
|
1145 |
|
jtulach@1334
|
1146 |
switch (choice) {
|
jtulach@1334
|
1147 |
case NUMBERSTYLE:
|
jtulach@1334
|
1148 |
return numberFormatProvider.getNumberInstance(locale);
|
jtulach@1334
|
1149 |
case PERCENTSTYLE:
|
jtulach@1334
|
1150 |
return numberFormatProvider.getPercentInstance(locale);
|
jtulach@1334
|
1151 |
case CURRENCYSTYLE:
|
jtulach@1334
|
1152 |
return numberFormatProvider.getCurrencyInstance(locale);
|
jtulach@1334
|
1153 |
case INTEGERSTYLE:
|
jtulach@1334
|
1154 |
return numberFormatProvider.getIntegerInstance(locale);
|
jtulach@1334
|
1155 |
default:
|
jtulach@1334
|
1156 |
assert false : choice;
|
jtulach@1334
|
1157 |
}
|
jtulach@1334
|
1158 |
|
jtulach@1334
|
1159 |
return null;
|
jtulach@1334
|
1160 |
}
|
jtulach@1334
|
1161 |
}
|
jtulach@1334
|
1162 |
}
|