Source Code Cross Referenced for NumberFormat.java in  » Apache-Harmony-Java-SE » java-package » java » text » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » java package » java.text 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* 
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package java.text;
019:
020:        import java.io.IOException;
021:        import java.io.InvalidObjectException;
022:        import java.io.ObjectInputStream;
023:        import java.io.ObjectOutputStream;
024:        import java.io.ObjectStreamField;
025:        import java.util.Currency;
026:        import java.util.Locale;
027:
028:        import org.apache.harmony.text.internal.nls.Messages;
029:
030:        /**
031:         * NumberFormat is the abstract superclass of Formats which format and parse
032:         * Numbers.
033:         */
034:        public abstract class NumberFormat extends Format {
035:
036:            private static final long serialVersionUID = -2308460125733713944L;
037:
038:            /**
039:             * Field constant.
040:             */
041:            public static final int INTEGER_FIELD = 0;
042:
043:            /**
044:             * Field constant.
045:             */
046:            public static final int FRACTION_FIELD = 1;
047:
048:            private boolean groupingUsed = true, parseIntegerOnly = false;
049:
050:            private int maximumIntegerDigits = 40, minimumIntegerDigits = 1,
051:                    maximumFractionDigits = 3, minimumFractionDigits = 0;
052:
053:            /**
054:             * Constructs a new instance of DateFormat.
055:             */
056:            public NumberFormat() {
057:            }
058:
059:            /**
060:             * Answers a new NumberFormat with the same properties as this NumberFormat.
061:             * 
062:             * @return a shallow copy of this NumberFormat
063:             * 
064:             * @see java.lang.Cloneable
065:             */
066:            @Override
067:            public Object clone() {
068:                return super .clone();
069:            }
070:
071:            /**
072:             * Compares the specified object to this NumberFormat and answer if they are
073:             * equal. The object must be an instance of NumberFormat and have the same
074:             * properties.
075:             * 
076:             * @param object
077:             *            the object to compare with this object
078:             * @return true if the specified object is equal to this NumberFormat, false
079:             *         otherwise
080:             * 
081:             * @see #hashCode
082:             */
083:            @Override
084:            public boolean equals(Object object) {
085:                if (object == this ) {
086:                    return true;
087:                }
088:                if (!(object instanceof  NumberFormat)) {
089:                    return false;
090:                }
091:                NumberFormat obj = (NumberFormat) object;
092:                return groupingUsed == obj.groupingUsed
093:                        && parseIntegerOnly == obj.parseIntegerOnly
094:                        && maximumFractionDigits == obj.maximumFractionDigits
095:                        && maximumIntegerDigits == obj.maximumIntegerDigits
096:                        && minimumFractionDigits == obj.minimumFractionDigits
097:                        && minimumIntegerDigits == obj.minimumIntegerDigits;
098:            }
099:
100:            /**
101:             * Formats the specified double using the rules of this NumberFormat.
102:             * 
103:             * @param value
104:             *            the double to format
105:             * @return the formatted String
106:             */
107:            public final String format(double value) {
108:                return format(value, new StringBuffer(), new FieldPosition(0))
109:                        .toString();
110:            }
111:
112:            /**
113:             * Formats the double value into the specified StringBuffer using the rules
114:             * of this NumberFormat. If the field specified by the FieldPosition is
115:             * formatted, set the begin and end index of the formatted field in the
116:             * FieldPosition.
117:             * 
118:             * @param value
119:             *            the double to format
120:             * @param buffer
121:             *            the StringBuffer
122:             * @param field
123:             *            the FieldPosition
124:             * @return the StringBuffer parameter <code>buffer</code>
125:             */
126:            public abstract StringBuffer format(double value,
127:                    StringBuffer buffer, FieldPosition field);
128:
129:            /**
130:             * Formats the specified long using the rules of this NumberFormat.
131:             * 
132:             * @param value
133:             *            the long to format
134:             * @return the formatted String
135:             */
136:            public final String format(long value) {
137:                return format(value, new StringBuffer(), new FieldPosition(0))
138:                        .toString();
139:            }
140:
141:            /**
142:             * Formats the long value into the specified StringBuffer using the rules of
143:             * this NumberFormat. If the field specified by the FieldPosition is
144:             * formatted, set the begin and end index of the formatted field in the
145:             * FieldPosition.
146:             * 
147:             * @param value
148:             *            the long to format
149:             * @param buffer
150:             *            the StringBuffer
151:             * @param field
152:             *            the FieldPosition
153:             * @return the StringBuffer parameter <code>buffer</code>
154:             */
155:            public abstract StringBuffer format(long value,
156:                    StringBuffer buffer, FieldPosition field);
157:
158:            /**
159:             * Formats the specified object into the specified StringBuffer using the
160:             * rules of this DateFormat. If the field specified by the FieldPosition is
161:             * formatted, set the begin and end index of the formatted field in the
162:             * FieldPosition.
163:             * 
164:             * @param object
165:             *            the object to format, must be a Number
166:             * @param buffer
167:             *            the StringBuffer
168:             * @param field
169:             *            the FieldPosition
170:             * @return the StringBuffer parameter <code>buffer</code>
171:             * 
172:             * @exception IllegalArgumentException
173:             *                when the object is not a Number
174:             */
175:            @Override
176:            public StringBuffer format(Object object, StringBuffer buffer,
177:                    FieldPosition field) {
178:                if (object instanceof  Number) {
179:                    double dv = ((Number) object).doubleValue();
180:                    long lv = ((Number) object).longValue();
181:                    if (dv == lv) {
182:                        return format(lv, buffer, field);
183:                    }
184:                    return format(dv, buffer, field);
185:                }
186:                throw new IllegalArgumentException();
187:            }
188:
189:            /**
190:             * Gets the list of installed Locales which support NumberFormat.
191:             * 
192:             * @return an array of Locale
193:             */
194:            public static Locale[] getAvailableLocales() {
195:                return Locale.getAvailableLocales();
196:            }
197:
198:            /**
199:             * Answers the currency used by this number format
200:             * <p>
201:             * This implementation throws UnsupportedOperationException, concrete sub
202:             * classes should override if they support currency formatting.
203:             * <p>
204:             * 
205:             * @return currency currency that was set in getInstance() or in
206:             *         setCurrency(), or null
207:             * @throws java.lang.UnsupportedOperationException
208:             */
209:            public Currency getCurrency() {
210:                throw new UnsupportedOperationException();
211:            }
212:
213:            /**
214:             * Answers a NumberFormat for formatting and parsing currency for the
215:             * default Locale.
216:             * 
217:             * @return a NumberFormat
218:             */
219:            public final static NumberFormat getCurrencyInstance() {
220:                return getCurrencyInstance(Locale.getDefault());
221:            }
222:
223:            /**
224:             * Answers a NumberFormat for formatting and parsing currency for the
225:             * specified Locale.
226:             * 
227:             * @param locale
228:             *            the Locale
229:             * @return a NumberFormat
230:             */
231:            public static NumberFormat getCurrencyInstance(Locale locale) {
232:                com.ibm.icu.text.DecimalFormat icuFormat = (com.ibm.icu.text.DecimalFormat) com.ibm.icu.text.NumberFormat
233:                        .getCurrencyInstance(locale);
234:                String pattern = icuFormat.toPattern();
235:                return new DecimalFormat(pattern, new DecimalFormatSymbols(
236:                        locale));
237:            }
238:
239:            /**
240:             * Answers a NumberFormat for formatting and parsing integers for the
241:             * default Locale.
242:             * 
243:             * @return a NumberFormat
244:             */
245:            public final static NumberFormat getIntegerInstance() {
246:                return getIntegerInstance(Locale.getDefault());
247:            }
248:
249:            /**
250:             * Answers a NumberFormat for formatting and parsing integers for the
251:             * specified Locale.
252:             * 
253:             * @param locale
254:             *            the Locale
255:             * @return a NumberFormat
256:             */
257:            public static NumberFormat getIntegerInstance(Locale locale) {
258:                com.ibm.icu.text.DecimalFormat icuFormat = (com.ibm.icu.text.DecimalFormat) com.ibm.icu.text.NumberFormat
259:                        .getIntegerInstance(locale);
260:                String pattern = icuFormat.toPattern();
261:                DecimalFormat format = new DecimalFormat(pattern,
262:                        new DecimalFormatSymbols(locale));
263:                format.setParseIntegerOnly(true);
264:                return format;
265:
266:            }
267:
268:            /**
269:             * Answers a NumberFormat for formatting and parsing numbers for the default
270:             * Locale.
271:             * 
272:             * @return a NumberFormat
273:             */
274:            public final static NumberFormat getInstance() {
275:                return getNumberInstance();
276:            }
277:
278:            /**
279:             * Answers a NumberFormat for formatting and parsing numbers for the
280:             * specified Locale.
281:             * 
282:             * @param locale
283:             *            the Locale
284:             * @return a NumberFormat
285:             */
286:            public static NumberFormat getInstance(Locale locale) {
287:                return getNumberInstance(locale);
288:            }
289:
290:            /**
291:             * Answers the maximum number of fraction digits that are printed when
292:             * formatting. If the maximum is less than the number of fraction digits,
293:             * the least significant digits are truncated.
294:             * 
295:             * @return the maximum number of fraction digits
296:             */
297:            public int getMaximumFractionDigits() {
298:                return maximumFractionDigits;
299:            }
300:
301:            /**
302:             * Answers the maximum number of integer digits that are printed when
303:             * formatting. If the maximum is less than the number of integer digits, the
304:             * most significant digits are truncated.
305:             * 
306:             * @return the maximum number of integer digits
307:             */
308:            public int getMaximumIntegerDigits() {
309:                return maximumIntegerDigits;
310:            }
311:
312:            /**
313:             * Answers the minimum number of fraction digits that are printed when
314:             * formatting.
315:             * 
316:             * @return the minimum number of fraction digits
317:             */
318:            public int getMinimumFractionDigits() {
319:                return minimumFractionDigits;
320:            }
321:
322:            /**
323:             * Answers the minimum number of integer digits that are printed when
324:             * formatting.
325:             * 
326:             * @return the minimum number of integer digits
327:             */
328:            public int getMinimumIntegerDigits() {
329:                return minimumIntegerDigits;
330:            }
331:
332:            /**
333:             * Answers a NumberFormat for formatting and parsing numbers for the default
334:             * Locale.
335:             * 
336:             * @return a NumberFormat
337:             */
338:            public final static NumberFormat getNumberInstance() {
339:                return getNumberInstance(Locale.getDefault());
340:            }
341:
342:            /**
343:             * Answers a NumberFormat for formatting and parsing numbers for the
344:             * specified Locale.
345:             * 
346:             * @param locale
347:             *            the Locale
348:             * @return a NumberFormat
349:             */
350:            public static NumberFormat getNumberInstance(Locale locale) {
351:                com.ibm.icu.text.DecimalFormat icuFormat = (com.ibm.icu.text.DecimalFormat) com.ibm.icu.text.NumberFormat
352:                        .getNumberInstance(locale);
353:                String pattern = icuFormat.toPattern();
354:                return new DecimalFormat(pattern, new DecimalFormatSymbols(
355:                        locale));
356:            }
357:
358:            /**
359:             * Answers a NumberFormat for formatting and parsing percentages for the
360:             * default Locale.
361:             * 
362:             * @return a NumberFormat
363:             */
364:            public final static NumberFormat getPercentInstance() {
365:                return getPercentInstance(Locale.getDefault());
366:            }
367:
368:            /**
369:             * Answers a NumberFormat for formatting and parsing percentages for the
370:             * specified Locale.
371:             * 
372:             * @param locale
373:             *            the Locale
374:             * @return a NumberFormat
375:             */
376:            public static NumberFormat getPercentInstance(Locale locale) {
377:                com.ibm.icu.text.DecimalFormat icuFormat = (com.ibm.icu.text.DecimalFormat) com.ibm.icu.text.NumberFormat
378:                        .getPercentInstance(locale);
379:                String pattern = icuFormat.toPattern();
380:                return new DecimalFormat(pattern, new DecimalFormatSymbols(
381:                        locale));
382:            }
383:
384:            /**
385:             * Answers an integer hash code for the receiver. Objects which are equal
386:             * answer the same value for this method.
387:             * 
388:             * @return the receiver's hash
389:             * 
390:             * @see #equals
391:             */
392:            @Override
393:            public int hashCode() {
394:                return (groupingUsed ? 1231 : 1237)
395:                        + (parseIntegerOnly ? 1231 : 1237)
396:                        + maximumFractionDigits + maximumIntegerDigits
397:                        + minimumFractionDigits + minimumIntegerDigits;
398:            }
399:
400:            /**
401:             * Answers whether this NumberFormat formats and parses numbers using a
402:             * grouping separator.
403:             * 
404:             * @return true when a grouping separator is used, false otherwise
405:             */
406:            public boolean isGroupingUsed() {
407:                return groupingUsed;
408:            }
409:
410:            /**
411:             * Answers whether this NumberFormat only parses integer numbers. Parsing
412:             * stops if a decimal separator is encountered.
413:             * 
414:             * @return true if this NumberFormat only parses integers, false for parsing
415:             *         integers or fractions
416:             */
417:            public boolean isParseIntegerOnly() {
418:                return parseIntegerOnly;
419:            }
420:
421:            /**
422:             * Parse a Number from the specified String using the rules of this
423:             * NumberFormat.
424:             * 
425:             * @param string
426:             *            the String to parse
427:             * @return the Number resulting from the parse
428:             * 
429:             * @exception ParseException
430:             *                when an error occurs during parsing
431:             */
432:            public Number parse(String string) throws ParseException {
433:                ParsePosition pos = new ParsePosition(0);
434:                Number number = parse(string, pos);
435:                if (pos.getErrorIndex() != -1 || pos.getIndex() == 0) {
436:                    throw new ParseException(null, pos.getErrorIndex());
437:                }
438:                return number;
439:            }
440:
441:            /**
442:             * Parse a Number from the specified String starting at the index specified
443:             * by the ParsePosition. If the string is successfully parsed, the index of
444:             * the ParsePosition is updated to the index following the parsed text.
445:             * 
446:             * @param string
447:             *            the String to parse
448:             * @param position
449:             *            the ParsePosition, updated on return with the index following
450:             *            the parsed text, or on error the index is unchanged and the
451:             *            error index is set to the index where the error occurred
452:             * @return the Number resulting from the parse, or null if there is an error
453:             */
454:            public abstract Number parse(String string, ParsePosition position);
455:
456:            /**
457:             * Parse a Number from the specified String starting at the index specified
458:             * by the ParsePosition. If the string is successfully parsed, the index of
459:             * the ParsePosition is updated to the index following the parsed text.
460:             * 
461:             * @param string
462:             *            the String to parse
463:             * @param position
464:             *            the ParsePosition, updated on return with the index following
465:             *            the parsed text, or on error the index is unchanged and the
466:             *            error index is set to the index where the error occurred
467:             * @return the Number resulting from the parse, or null if there is an error
468:             */
469:            @Override
470:            public final Object parseObject(String string,
471:                    ParsePosition position) {
472:                if (position == null) {
473:                    // text.1A=position is null
474:                    throw new NullPointerException(Messages
475:                            .getString("text.1A")); //$NON-NLS-1$
476:                }
477:
478:                try {
479:                    return parse(string, position);
480:                } catch (Exception e) {
481:                    return null;
482:                }
483:            }
484:
485:            /**
486:             * Sets the currency used by this number format when formatting currency
487:             * values.
488:             * <p>
489:             * The min and max fraction digits remain the same.
490:             * <p>
491:             * This implementation throws UnsupportedOperationException, concrete sub
492:             * classes should override if they support currency formatting.
493:             * <p>
494:             * 
495:             * @param currency
496:             *            the new Currency
497:             * @throws java.lang.UnsupportedOperationException
498:             */
499:            public void setCurrency(Currency currency) {
500:                throw new UnsupportedOperationException();
501:            }
502:
503:            /**
504:             * Sets whether this NumberFormat formats and parses numbers using a
505:             * grouping separator.
506:             * 
507:             * @param value
508:             *            true when a grouping separator is used, false otherwise
509:             */
510:            public void setGroupingUsed(boolean value) {
511:                groupingUsed = value;
512:            }
513:
514:            /**
515:             * Sets the maximum number of fraction digits that are printed when
516:             * formatting. If the maximum is less than the number of fraction digits,
517:             * the least significant digits are truncated.
518:             * 
519:             * @param value
520:             *            the maximum number of fraction digits
521:             */
522:            public void setMaximumFractionDigits(int value) {
523:                maximumFractionDigits = value < 0 ? 0 : value;
524:                if (maximumFractionDigits < minimumFractionDigits) {
525:                    minimumFractionDigits = maximumFractionDigits;
526:                }
527:            }
528:
529:            /**
530:             * Used to specify the new maximum count of integer digits that are printed
531:             * when formatting. If the maximum is less than the number of integer
532:             * digits, the most significant digits are truncated.
533:             * 
534:             * @param value
535:             *            the new maximum number of integer numerals for display
536:             */
537:            public void setMaximumIntegerDigits(int value) {
538:                maximumIntegerDigits = value < 0 ? 0 : value;
539:                if (maximumIntegerDigits < minimumIntegerDigits) {
540:                    minimumIntegerDigits = maximumIntegerDigits;
541:                }
542:            }
543:
544:            /**
545:             * Sets the minimum number of fraction digits that are printed when
546:             * formatting.
547:             * 
548:             * @param value
549:             *            the minimum number of fraction digits
550:             */
551:            public void setMinimumFractionDigits(int value) {
552:                minimumFractionDigits = value < 0 ? 0 : value;
553:                if (maximumFractionDigits < minimumFractionDigits) {
554:                    maximumFractionDigits = minimumFractionDigits;
555:                }
556:            }
557:
558:            /**
559:             * Sets the minimum number of integer digits that are printed when
560:             * formatting.
561:             * 
562:             * @param value
563:             *            the minimum number of integer digits
564:             */
565:            public void setMinimumIntegerDigits(int value) {
566:                minimumIntegerDigits = value < 0 ? 0 : value;
567:                if (maximumIntegerDigits < minimumIntegerDigits) {
568:                    maximumIntegerDigits = minimumIntegerDigits;
569:                }
570:            }
571:
572:            /**
573:             * Specifies if this NumberFormat should only parse numbers as integers or
574:             * else as any kind of number. If this is called with a <code>true</code>
575:             * value then subsequent parsing attempts will stop if a decimal separator
576:             * is encountered.
577:             * 
578:             * @param value
579:             *            <code>true</code> to only parse integers, <code>false</code>
580:             *            to parse integers and fractions
581:             */
582:            public void setParseIntegerOnly(boolean value) {
583:                parseIntegerOnly = value;
584:            }
585:
586:            private static final ObjectStreamField[] serialPersistentFields = {
587:                    new ObjectStreamField("groupingUsed", Boolean.TYPE), //$NON-NLS-1$
588:                    new ObjectStreamField("maxFractionDigits", Byte.TYPE), //$NON-NLS-1$
589:                    new ObjectStreamField("maximumFractionDigits", Integer.TYPE), //$NON-NLS-1$
590:                    new ObjectStreamField("maximumIntegerDigits", Integer.TYPE), //$NON-NLS-1$
591:                    new ObjectStreamField("maxIntegerDigits", Byte.TYPE), //$NON-NLS-1$
592:                    new ObjectStreamField("minFractionDigits", Byte.TYPE), //$NON-NLS-1$
593:                    new ObjectStreamField("minimumFractionDigits", Integer.TYPE), //$NON-NLS-1$
594:                    new ObjectStreamField("minimumIntegerDigits", Integer.TYPE), //$NON-NLS-1$
595:                    new ObjectStreamField("minIntegerDigits", Byte.TYPE), //$NON-NLS-1$
596:                    new ObjectStreamField("parseIntegerOnly", Boolean.TYPE), //$NON-NLS-1$
597:                    new ObjectStreamField("serialVersionOnStream", Integer.TYPE), }; //$NON-NLS-1$
598:
599:            private void writeObject(ObjectOutputStream stream)
600:                    throws IOException {
601:                ObjectOutputStream.PutField fields = stream.putFields();
602:                fields.put("groupingUsed", groupingUsed); //$NON-NLS-1$
603:                fields
604:                        .put(
605:                                "maxFractionDigits", //$NON-NLS-1$
606:                                maximumFractionDigits < Byte.MAX_VALUE ? (byte) maximumFractionDigits
607:                                        : Byte.MAX_VALUE);
608:                fields.put("maximumFractionDigits", maximumFractionDigits); //$NON-NLS-1$
609:                fields.put("maximumIntegerDigits", maximumIntegerDigits); //$NON-NLS-1$
610:                fields
611:                        .put(
612:                                "maxIntegerDigits", //$NON-NLS-1$
613:                                maximumIntegerDigits < Byte.MAX_VALUE ? (byte) maximumIntegerDigits
614:                                        : Byte.MAX_VALUE);
615:                fields
616:                        .put(
617:                                "minFractionDigits", //$NON-NLS-1$
618:                                minimumFractionDigits < Byte.MAX_VALUE ? (byte) minimumFractionDigits
619:                                        : Byte.MAX_VALUE);
620:                fields.put("minimumFractionDigits", minimumFractionDigits); //$NON-NLS-1$
621:                fields.put("minimumIntegerDigits", minimumIntegerDigits); //$NON-NLS-1$
622:                fields
623:                        .put(
624:                                "minIntegerDigits", //$NON-NLS-1$
625:                                minimumIntegerDigits < Byte.MAX_VALUE ? (byte) minimumIntegerDigits
626:                                        : Byte.MAX_VALUE);
627:                fields.put("parseIntegerOnly", parseIntegerOnly); //$NON-NLS-1$
628:                fields.put("serialVersionOnStream", 1); //$NON-NLS-1$
629:                stream.writeFields();
630:            }
631:
632:            private void readObject(ObjectInputStream stream)
633:                    throws IOException, ClassNotFoundException {
634:                ObjectInputStream.GetField fields = stream.readFields();
635:                groupingUsed = fields.get("groupingUsed", true); //$NON-NLS-1$
636:                parseIntegerOnly = fields.get("parseIntegerOnly", false); //$NON-NLS-1$
637:                if (fields.get("serialVersionOnStream", 0) == 0) { //$NON-NLS-1$
638:                    maximumFractionDigits = fields.get(
639:                            "maxFractionDigits", (byte) 3); //$NON-NLS-1$
640:                    maximumIntegerDigits = fields.get(
641:                            "maxIntegerDigits", (byte) 40); //$NON-NLS-1$
642:                    minimumFractionDigits = fields.get(
643:                            "minFractionDigits", (byte) 0); //$NON-NLS-1$
644:                    minimumIntegerDigits = fields.get(
645:                            "minIntegerDigits", (byte) 1); //$NON-NLS-1$
646:                } else {
647:                    maximumFractionDigits = fields.get(
648:                            "maximumFractionDigits", 3); //$NON-NLS-1$
649:                    maximumIntegerDigits = fields.get(
650:                            "maximumIntegerDigits", 40); //$NON-NLS-1$
651:                    minimumFractionDigits = fields.get(
652:                            "minimumFractionDigits", 0); //$NON-NLS-1$
653:                    minimumIntegerDigits = fields
654:                            .get("minimumIntegerDigits", 1); //$NON-NLS-1$
655:                }
656:                if (minimumIntegerDigits > maximumIntegerDigits
657:                        || minimumFractionDigits > maximumFractionDigits) {
658:                    // text.00=min digits greater than max digits
659:                    throw new InvalidObjectException(Messages
660:                            .getString("text.00")); //$NON-NLS-1$
661:                }
662:                if (minimumIntegerDigits < 0 || maximumIntegerDigits < 0
663:                        || minimumFractionDigits < 0
664:                        || maximumFractionDigits < 0) {
665:                    // text.01=min or max digits negative
666:                    throw new InvalidObjectException(Messages
667:                            .getString("text.01")); //$NON-NLS-1$
668:                }
669:            }
670:
671:            /**
672:             * The instances of this inner class are used as attribute keys and values
673:             * in AttributedCharacterIterator that
674:             * NumberFormat.formatToCharacterIterator() method returns.
675:             * <p>
676:             * There is no public constructor to this class, the only instances are the
677:             * constants defined here.
678:             * <p>
679:             */
680:            public static class Field extends Format.Field {
681:
682:                private static final long serialVersionUID = 7494728892700160890L;
683:
684:                public static final Field SIGN = new Field("sign"); //$NON-NLS-1$
685:
686:                public static final Field INTEGER = new Field("integer"); //$NON-NLS-1$
687:
688:                public static final Field FRACTION = new Field("fraction"); //$NON-NLS-1$
689:
690:                public static final Field EXPONENT = new Field("exponent"); //$NON-NLS-1$
691:
692:                public static final Field EXPONENT_SIGN = new Field(
693:                        "exponent sign"); //$NON-NLS-1$
694:
695:                public static final Field EXPONENT_SYMBOL = new Field(
696:                        "exponent symbol"); //$NON-NLS-1$
697:
698:                public static final Field DECIMAL_SEPARATOR = new Field(
699:                        "decimal separator"); //$NON-NLS-1$
700:
701:                public static final Field GROUPING_SEPARATOR = new Field(
702:                        "grouping separator"); //$NON-NLS-1$
703:
704:                public static final Field PERCENT = new Field("percent"); //$NON-NLS-1$
705:
706:                public static final Field PERMILLE = new Field("per mille"); //$NON-NLS-1$
707:
708:                public static final Field CURRENCY = new Field("currency"); //$NON-NLS-1$
709:
710:                /**
711:                 * Constructs a new instance of NumberFormat.Field with the given field
712:                 * name.
713:                 */
714:                protected Field(String fieldName) {
715:                    super (fieldName);
716:                }
717:
718:                /**
719:                 * serialization method resolve instances to the constant
720:                 * NumberFormat.Field values
721:                 */
722:                @Override
723:                protected Object readResolve() throws InvalidObjectException {
724:                    if (this .equals(INTEGER)) {
725:                        return INTEGER;
726:                    }
727:                    if (this .equals(FRACTION)) {
728:                        return FRACTION;
729:                    }
730:                    if (this .equals(EXPONENT)) {
731:                        return EXPONENT;
732:                    }
733:                    if (this .equals(EXPONENT_SIGN)) {
734:                        return EXPONENT_SIGN;
735:                    }
736:                    if (this .equals(EXPONENT_SYMBOL)) {
737:                        return EXPONENT_SYMBOL;
738:                    }
739:                    if (this .equals(CURRENCY)) {
740:                        return CURRENCY;
741:                    }
742:                    if (this .equals(DECIMAL_SEPARATOR)) {
743:                        return DECIMAL_SEPARATOR;
744:                    }
745:                    if (this .equals(GROUPING_SEPARATOR)) {
746:                        return GROUPING_SEPARATOR;
747:                    }
748:                    if (this .equals(PERCENT)) {
749:                        return PERCENT;
750:                    }
751:                    if (this .equals(PERMILLE)) {
752:                        return PERMILLE;
753:                    }
754:                    if (this .equals(SIGN)) {
755:                        return SIGN;
756:                    }
757:                    // text.02=Unknown attribute
758:                    throw new InvalidObjectException(Messages
759:                            .getString("text.02")); //$NON-NLS-1$
760:                }
761:            }
762:
763:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.