1 8 package com.ibm.icu.impl; 9 10 import java.io.IOException ; 11 import java.io.ObjectInputStream ; 12 import java.math.BigInteger ; 13 import java.text.FieldPosition ; 14 import java.text.ParsePosition ; 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 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]; 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 ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, loc); 55 String [] 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 82 public void setParsePositiveOnly(boolean isPositiveOnly) { 83 positiveOnly = isPositiveOnly; 84 } 85 86 public char getZeroDigit() { 87 return zeroDigit; 88 } 89 90 public StringBuffer format(double number, StringBuffer toAppendTo, 91 FieldPosition pos) { 92 throw new UnsupportedOperationException ("StringBuffer format(double, StringBuffer, FieldPostion) is not implemented"); 93 } 94 95 public StringBuffer format(long numberL, StringBuffer toAppendTo, 96 FieldPosition pos) { 97 98 if (numberL < 0) { 99 toAppendTo.append(minusSign); 101 } 102 103 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 format(BigInteger number, StringBuffer toAppendTo, 134 FieldPosition pos) { 135 throw new UnsupportedOperationException ("StringBuffer format(BigInteger, StringBuffer, FieldPostion) is not implemented"); 136 } 137 138 145 public StringBuffer format(BigDecimal number, 146 StringBuffer toAppendTo, FieldPosition pos) { 147 throw new UnsupportedOperationException ("StringBuffer format(BigDecimal, StringBuffer, FieldPostion) is not implemented"); 148 } 149 150 153 public Number parse(String text, ParsePosition 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 result = null; 180 if (sawNumber) { 181 num = negative ? num * (-1) : num; 182 result = new Long (num); 183 parsePosition.setIndex(base + offset); 184 } 185 return result; 186 } 187 188 public boolean equals(Object 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 stream) throws IOException , ClassNotFoundException { 207 stream.defaultReadObject(); 208 decimalBuf = new char[20]; 210 } 211 } 212 213 | Popular Tags |