KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > impl > DateNumberFormat


1 //##header 1189099963000 FOUNDATION
2
/*
3 *******************************************************************************
4 * Copyright (C) 2007, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *******************************************************************************
7 */

8 package com.ibm.icu.impl;
9
10 import java.io.IOException JavaDoc;
11 import java.io.ObjectInputStream JavaDoc;
12 import java.math.BigInteger JavaDoc;
13 import java.text.FieldPosition JavaDoc;
14 import java.text.ParsePosition JavaDoc;
15
16 import com.ibm.icu.lang.UCharacter;
17 import com.ibm.icu.math.BigDecimal;
18 import com.ibm.icu.text.NumberFormat;
19 import com.ibm.icu.util.ULocale;
20 import com.ibm.icu.util.UResourceBundle;
21
22 /*
23  * NumberFormat implementation dedicated/optimized for DateFormat,
24  * used by SimpleDateFormat implementation.
25  */

26 public final class DateNumberFormat extends NumberFormat {
27
28     private static final long serialVersionUID = -6315692826916346953L;
29
30     private char zeroDigit;
31     private char minusSign;
32     private boolean positiveOnly = false;
33
34     private transient char[] decimalBuf = new char[20]; // 20 digits is good enough to store Long.MAX_VALUE
35

36     private static SimpleCache CACHE = new SimpleCache();
37
38     private int maxIntDigits;
39     private int minIntDigits;
40  
41     public DateNumberFormat(ULocale loc) {
42         initialize(loc);
43     }
44
45     public DateNumberFormat(char zeroDigit, char minusSign) {
46         this.zeroDigit = zeroDigit;
47         this.minusSign = minusSign;
48     }
49
50     private void initialize(ULocale loc) {
51         char[] elems = (char[])CACHE.get(loc);
52         if (elems == null) {
53             // Missed cache
54
ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, loc);
55             String JavaDoc[] numberElements = rb.getStringArray("NumberElements");
56             elems = new char[2];
57             elems[0] = numberElements[4].charAt(0);
58             elems[1] = numberElements[6].charAt(0);
59             CACHE.put(loc, elems);
60         }
61         zeroDigit = elems[0];
62         minusSign = elems[1];
63     }
64
65     public void setMaximumIntegerDigits(int newValue) {
66         maxIntDigits = newValue;
67     }
68
69     public int getMaximumIntegerDigits() {
70         return maxIntDigits;
71     }
72
73     public void setMinimumIntegerDigits(int newValue) {
74         minIntDigits = newValue;
75     }
76
77     public int getMinimumIntegerDigits() {
78         return minIntDigits;
79     }
80
81     /* For supporting SimpleDateFormat.parseInt */
82     public void setParsePositiveOnly(boolean isPositiveOnly) {
83         positiveOnly = isPositiveOnly;
84     }
85
86     public char getZeroDigit() {
87         return zeroDigit;
88     }
89
90     public StringBuffer JavaDoc format(double number, StringBuffer JavaDoc toAppendTo,
91             FieldPosition JavaDoc pos) {
92         throw new UnsupportedOperationException JavaDoc("StringBuffer format(double, StringBuffer, FieldPostion) is not implemented");
93     }
94
95     public StringBuffer JavaDoc format(long numberL, StringBuffer JavaDoc toAppendTo,
96             FieldPosition JavaDoc pos) {
97
98         if (numberL < 0) {
99             // negative
100
toAppendTo.append(minusSign);
101         }
102
103         // Note: NumberFormat used by DateFormat only uses int numbers.
104
// Remainder operation on 32bit platform using long is significantly slower
105
// than int. So, this method casts long number into int.
106
int number = (int)numberL;
107
108         int limit = decimalBuf.length < maxIntDigits ? decimalBuf.length : maxIntDigits;
109         int index = limit - 1;
110         while (true) {
111             decimalBuf[index] = (char)((number % 10) + zeroDigit);
112             number /= 10;
113             if (index == 0 || number == 0) {
114                 break;
115             }
116             index--;
117         }
118         int padding = minIntDigits - (limit - index);
119         for (; padding > 0; padding--) {
120             decimalBuf[--index] = zeroDigit;
121         }
122         int length = limit - index;
123         toAppendTo.append(decimalBuf, index, length);
124         pos.setBeginIndex(0);
125         if (pos.getField() == NumberFormat.INTEGER_FIELD) {
126             pos.setEndIndex(length);
127         } else {
128             pos.setEndIndex(0);
129         }
130         return toAppendTo;
131     }
132     
133     public StringBuffer JavaDoc format(BigInteger JavaDoc number, StringBuffer JavaDoc toAppendTo,
134             FieldPosition JavaDoc pos) {
135         throw new UnsupportedOperationException JavaDoc("StringBuffer format(BigInteger, StringBuffer, FieldPostion) is not implemented");
136     }
137
138 //#ifndef FOUNDATION
139
//## public StringBuffer format(java.math.BigDecimal number, StringBuffer toAppendTo,
140
//## FieldPosition pos) {
141
//## throw new UnsupportedOperationException("StringBuffer format(BigDecimal, StringBuffer, FieldPostion) is not implemented");
142
//## }
143
//#endif
144

145     public StringBuffer JavaDoc format(BigDecimal number,
146             StringBuffer JavaDoc toAppendTo, FieldPosition JavaDoc pos) {
147         throw new UnsupportedOperationException JavaDoc("StringBuffer format(BigDecimal, StringBuffer, FieldPostion) is not implemented");
148     }
149
150     /*
151      * Note: This method only parse integer numbers which can be represented by long
152      */

153     public Number JavaDoc parse(String JavaDoc text, ParsePosition JavaDoc parsePosition) {
154         long num = 0;
155         boolean sawNumber = false;
156         boolean negative = false;
157         int base = parsePosition.getIndex();
158         int offset = 0;
159         for (; base + offset < text.length(); offset++) {
160             char ch = text.charAt(base + offset);
161             if (offset == 0 && ch == minusSign) {
162                 if (positiveOnly) {
163                     break;
164                 }
165                 negative = true;
166             } else {
167                 int digit = ch - zeroDigit;
168                 if (digit < 0 || 9 < digit) {
169                     digit = UCharacter.digit(ch);
170                 }
171                 if (0 <= digit && digit <= 9) {
172                     sawNumber = true;
173                     num = num * 10 + digit;
174                 } else {
175                     break;
176                 }
177             }
178         }
179         Number JavaDoc result = null;
180         if (sawNumber) {
181             num = negative ? num * (-1) : num;
182             result = new Long JavaDoc(num);
183             parsePosition.setIndex(base + offset);
184         }
185         return result;
186     }
187
188     public boolean equals(Object JavaDoc obj) {
189         if (obj == null) {
190             return false;
191         }
192         if (!super.equals(obj)) {
193             return false;
194         }
195         DateNumberFormat other = (DateNumberFormat)obj;
196         if (this.maxIntDigits == other.maxIntDigits
197                 && this.minIntDigits == other.minIntDigits
198                 && this.zeroDigit == other.zeroDigit
199                 && this.minusSign == other.minusSign
200                 && this.positiveOnly == other.positiveOnly) {
201             return true;
202         }
203         return false;
204     }
205
206     private void readObject(ObjectInputStream JavaDoc stream) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
207         stream.defaultReadObject();
208         // re-allocate the work buffer
209
decimalBuf = new char[20];
210     }
211 }
212
213 //eof
214
Popular Tags