1 8 package com.ibm.icu.text; 9 10 import java.io.IOException ; 11 import java.io.InvalidObjectException ; 12 import java.io.ObjectInputStream ; 13 import java.io.ObjectOutputStream ; 14 import java.math.BigInteger ; 15 import java.text.FieldPosition ; 16 import java.text.Format ; 17 import java.text.ParseException ; 18 import java.text.ParsePosition ; 19 import java.util.Collections ; 20 import java.util.Locale ; 21 import java.util.MissingResourceException ; 22 import java.util.Set ; 23 24 import com.ibm.icu.impl.ICUResourceBundle; 25 import com.ibm.icu.util.Currency; 26 import com.ibm.icu.util.CurrencyAmount; 27 import com.ibm.icu.util.ULocale; 28 import com.ibm.icu.util.UResourceBundle; 29 30 155 public abstract class NumberFormat extends UFormat { 156 157 private static final int NUMBERSTYLE = 0; 159 private static final int CURRENCYSTYLE = 1; 160 private static final int PERCENTSTYLE = 2; 161 private static final int SCIENTIFICSTYLE = 3; 162 private static final int INTEGERSTYLE = 4; 163 164 170 public static final int INTEGER_FIELD = 0; 171 172 178 public static final int FRACTION_FIELD = 1; 179 180 186 public StringBuffer format(Object number, 187 StringBuffer toAppendTo, 188 FieldPosition pos) 189 { 190 if (number instanceof Long ) { 191 return format(((Long )number).longValue(), toAppendTo, pos); 192 } else if (number instanceof BigInteger ) { 193 return format((BigInteger ) number, toAppendTo, pos); 194 } else if (number instanceof com.ibm.icu.math.BigDecimal) { 199 return format((com.ibm.icu.math.BigDecimal) number, toAppendTo, pos); 200 } else if (number instanceof CurrencyAmount) { 201 return format((CurrencyAmount)number, toAppendTo, pos); 202 } else if (number instanceof Number ) { 203 return format(((Number )number).doubleValue(), toAppendTo, pos); 204 } else { 205 throw new IllegalArgumentException ("Cannot format given Object as a Number"); 206 } 207 } 208 209 212 public final Object parseObject(String source, 213 ParsePosition parsePosition) 214 { 215 return parse(source, parsePosition); 216 } 217 218 223 public final String format(double number) { 224 return format(number,new StringBuffer (), 225 new FieldPosition (0)).toString(); 226 } 227 228 233 public final String format(long number) { 234 StringBuffer buf = new StringBuffer (19); 235 FieldPosition pos = new FieldPosition (0); 236 format(number, buf, pos); 237 return buf.toString(); 238 } 239 240 245 public final String format(BigInteger number) { 246 return format(number, new StringBuffer (), 247 new FieldPosition (0)).toString(); 248 } 249 250 262 267 public final String format(com.ibm.icu.math.BigDecimal number) { 268 return format(number, new StringBuffer (), 269 new FieldPosition (0)).toString(); 270 } 271 272 277 public final String format(CurrencyAmount currAmt) { 278 return format(currAmt, new StringBuffer (), 279 new FieldPosition (0)).toString(); 280 } 281 282 287 public abstract StringBuffer format(double number, 288 StringBuffer toAppendTo, 289 FieldPosition pos); 290 291 296 public abstract StringBuffer format(long number, 297 StringBuffer toAppendTo, 298 FieldPosition pos); 299 300 306 public abstract StringBuffer format(BigInteger number, 307 StringBuffer toAppendTo, 308 FieldPosition pos); 309 321 327 public abstract StringBuffer format(com.ibm.icu.math.BigDecimal number, 328 StringBuffer toAppendTo, 329 FieldPosition pos); 330 331 337 public StringBuffer format(CurrencyAmount currAmt, 338 StringBuffer toAppendTo, 339 FieldPosition pos) { 340 Currency save = getCurrency(), curr = currAmt.getCurrency(); 342 boolean same = curr.equals(save); 343 if (!same) setCurrency(curr); 344 format(currAmt.getNumber(), toAppendTo, pos); 345 if (!same) setCurrency(save); 346 return toAppendTo; 347 } 348 349 361 public abstract Number parse(String text, ParsePosition parsePosition); 362 363 374 public Number parse(String text) throws ParseException { 376 ParsePosition parsePosition = new ParsePosition (0); 377 Number result = parse(text, parsePosition); 378 if (parsePosition.getIndex() == 0) { 379 throw new ParseException ("Unparseable number: \"" + text + '"', 380 parsePosition.getErrorIndex()); 381 } 382 return result; 383 } 384 385 404 CurrencyAmount parseCurrency(String text, ParsePosition pos) { 405 Number n = parse(text, pos); 407 return n == null ? null : new CurrencyAmount(n, getEffectiveCurrency()); 408 } 409 410 420 public boolean isParseIntegerOnly() { 421 return parseIntegerOnly; 422 } 423 424 430 public void setParseIntegerOnly(boolean value) { 431 parseIntegerOnly = value; 432 } 433 434 454 public void setParseStrict(boolean value) { 455 parseStrict = value; 456 } 457 458 465 public boolean isParseStrict() { 466 return parseStrict; 467 } 468 469 471 479 public final static NumberFormat getInstance() { 481 return getInstance(ULocale.getDefault(), NUMBERSTYLE); 482 } 483 484 491 public static NumberFormat getInstance(Locale inLocale) { 492 return getInstance(ULocale.forLocale(inLocale), NUMBERSTYLE); 493 } 494 495 503 public static NumberFormat getInstance(ULocale inLocale) { 504 return getInstance(inLocale, NUMBERSTYLE); 505 } 506 507 511 public final static NumberFormat getNumberInstance() { 512 return getInstance(ULocale.getDefault(), NUMBERSTYLE); 513 } 514 515 519 public static NumberFormat getNumberInstance(Locale inLocale) { 520 return getInstance(ULocale.forLocale(inLocale), NUMBERSTYLE); 521 } 522 523 528 public static NumberFormat getNumberInstance(ULocale inLocale) { 529 return getInstance(inLocale, NUMBERSTYLE); 530 } 531 532 543 public final static NumberFormat getIntegerInstance() { 545 return getInstance(ULocale.getDefault(), INTEGERSTYLE); 546 } 547 548 560 public static NumberFormat getIntegerInstance(Locale inLocale) { 562 return getInstance(ULocale.forLocale(inLocale), INTEGERSTYLE); 563 } 564 565 578 public static NumberFormat getIntegerInstance(ULocale inLocale) { 579 return getInstance(inLocale, INTEGERSTYLE); 580 } 581 582 587 public final static NumberFormat getCurrencyInstance() { 588 return getInstance(ULocale.getDefault(), CURRENCYSTYLE); 589 } 590 591 596 public static NumberFormat getCurrencyInstance(Locale inLocale) { 597 return getInstance(ULocale.forLocale(inLocale), CURRENCYSTYLE); 598 } 599 600 606 public static NumberFormat getCurrencyInstance(ULocale inLocale) { 607 return getInstance(inLocale, CURRENCYSTYLE); 608 } 609 610 615 public final static NumberFormat getPercentInstance() { 616 return getInstance(ULocale.getDefault(), PERCENTSTYLE); 617 } 618 619 624 public static NumberFormat getPercentInstance(Locale inLocale) { 625 return getInstance(ULocale.forLocale(inLocale), PERCENTSTYLE); 626 } 627 628 634 public static NumberFormat getPercentInstance(ULocale inLocale) { 635 return getInstance(inLocale, PERCENTSTYLE); 636 } 637 638 644 public final static NumberFormat getScientificInstance() { 645 return getInstance(ULocale.getDefault(), SCIENTIFICSTYLE); 646 } 647 648 654 public static NumberFormat getScientificInstance(Locale inLocale) { 655 return getInstance(ULocale.forLocale(inLocale), SCIENTIFICSTYLE); 656 } 657 658 665 public static NumberFormat getScientificInstance(ULocale inLocale) { 666 return getInstance(inLocale, SCIENTIFICSTYLE); 667 } 668 669 683 public static abstract class NumberFormatFactory { 684 688 public static final int FORMAT_NUMBER = NUMBERSTYLE; 689 690 694 public static final int FORMAT_CURRENCY = CURRENCYSTYLE; 695 696 700 public static final int FORMAT_PERCENT = PERCENTSTYLE; 701 702 706 public static final int FORMAT_SCIENTIFIC = SCIENTIFICSTYLE; 707 708 712 public static final int FORMAT_INTEGER = INTEGERSTYLE; 713 714 721 public boolean visible() { 723 return true; 724 } 725 727 733 public abstract Set getSupportedLocaleNames(); 734 735 748 public NumberFormat createFormat(ULocale loc, int formatType) { 749 return createFormat(loc.toLocale(), formatType); 750 } 751 752 765 public NumberFormat createFormat(Locale loc, int formatType) { 766 return createFormat(ULocale.forLocale(loc), formatType); 767 } 768 769 772 protected NumberFormatFactory() { 773 } 774 } 775 776 780 public static abstract class SimpleNumberFormatFactory extends NumberFormatFactory { 781 final Set localeNames; 782 final boolean visible; 783 784 787 public SimpleNumberFormatFactory(Locale locale) { 788 this(locale, true); 789 } 790 791 794 public SimpleNumberFormatFactory(Locale locale, boolean visible) { 795 localeNames = Collections.singleton(ULocale.forLocale(locale).getBaseName()); 796 this.visible = visible; 797 } 798 799 803 public SimpleNumberFormatFactory(ULocale locale) { 804 this(locale, true); 805 } 806 807 811 public SimpleNumberFormatFactory(ULocale locale, boolean visible) { 812 localeNames = Collections.singleton(locale.getBaseName()); 813 this.visible = visible; 814 } 815 816 819 public final boolean visible() { 820 return visible; 821 } 822 823 826 public final Set getSupportedLocaleNames() { 827 return localeNames; 828 } 829 } 830 831 static abstract class NumberFormatShim { 833 abstract Locale [] getAvailableLocales(); 834 abstract ULocale[] getAvailableULocales(); 835 abstract Object registerFactory(NumberFormatFactory f); 836 abstract boolean unregister(Object k); 837 abstract NumberFormat createInstance(ULocale l, int k); 838 } 839 840 private static NumberFormatShim shim; 841 private static NumberFormatShim getShim() { 842 if (shim == null) { 847 try { 848 Class cls = Class.forName("com.ibm.icu.text.NumberFormatServiceShim"); 849 shim = (NumberFormatShim)cls.newInstance(); 850 } 851 catch (MissingResourceException e){ 852 throw e; 853 } 854 catch (Exception e) { 855 throw new RuntimeException (e.getMessage()); 858 } 860 } 861 return shim; 862 } 863 864 869 public static Locale [] getAvailableLocales() { 870 if (shim == null) { 871 return ICUResourceBundle.getAvailableLocales(ICUResourceBundle.ICU_BASE_NAME); 872 } 873 return getShim().getAvailableLocales(); 874 } 875 876 882 public static ULocale[] getAvailableULocales() { 883 if (shim == null) { 884 return ICUResourceBundle.getAvailableULocales(ICUResourceBundle.ICU_BASE_NAME); 885 } 886 return getShim().getAvailableULocales(); 887 } 888 889 897 public static Object registerFactory(NumberFormatFactory factory) { 898 if (factory == null) { 899 throw new IllegalArgumentException ("factory must not be null"); 900 } 901 return getShim().registerFactory(factory); 902 } 903 904 911 public static boolean unregister(Object registryKey) { 912 if (registryKey == null) { 913 throw new IllegalArgumentException ("registryKey must not be null"); 914 } 915 916 if (shim == null) { 917 return false; 918 } 919 920 return shim.unregister(registryKey); 921 } 922 923 925 929 public int hashCode() { 930 return maximumIntegerDigits * 37 + maxFractionDigits; 931 } 933 934 942 public boolean equals(Object obj) { 943 if (obj == null) return false; 944 if (this == obj) 945 return true; 946 if (getClass() != obj.getClass()) 947 return false; 948 NumberFormat other = (NumberFormat) obj; 949 return maximumIntegerDigits == other.maximumIntegerDigits 950 && minimumIntegerDigits == other.minimumIntegerDigits 951 && maximumFractionDigits == other.maximumFractionDigits 952 && minimumFractionDigits == other.minimumFractionDigits 953 && groupingUsed == other.groupingUsed 954 && parseIntegerOnly == other.parseIntegerOnly 955 && parseStrict == other.parseStrict; 956 } 957 958 962 public Object clone() 963 { 964 NumberFormat other = (NumberFormat) super.clone(); 965 return other; 966 } 967 968 978 public boolean isGroupingUsed() { 979 return groupingUsed; 980 } 981 982 989 public void setGroupingUsed(boolean newValue) { 990 groupingUsed = newValue; 991 } 992 993 1002 public int getMaximumIntegerDigits() { 1003 return maximumIntegerDigits; 1004 } 1005 1006 1018 public void setMaximumIntegerDigits(int newValue) { 1019 maximumIntegerDigits = Math.max(0,newValue); 1020 if (minimumIntegerDigits > maximumIntegerDigits) 1021 minimumIntegerDigits = maximumIntegerDigits; 1022 } 1023 1024 1034 public int getMinimumIntegerDigits() { 1035 return minimumIntegerDigits; 1036 } 1037 1038 1050 public void setMinimumIntegerDigits(int newValue) { 1051 minimumIntegerDigits = Math.max(0,newValue); 1052 if (minimumIntegerDigits > maximumIntegerDigits) 1053 maximumIntegerDigits = minimumIntegerDigits; 1054 } 1055 1056 1066 public int getMaximumFractionDigits() { 1067 return maximumFractionDigits; 1068 } 1069 1070 1082 public void setMaximumFractionDigits(int newValue) { 1083 maximumFractionDigits = Math.max(0,newValue); 1084 if (maximumFractionDigits < minimumFractionDigits) 1085 minimumFractionDigits = maximumFractionDigits; 1086 } 1087 1088 1098 public int getMinimumFractionDigits() { 1099 return minimumFractionDigits; 1100 } 1101 1102 1114 public void setMinimumFractionDigits(int newValue) { 1115 minimumFractionDigits = Math.max(0,newValue); 1116 if (maximumFractionDigits < minimumFractionDigits) 1117 maximumFractionDigits = minimumFractionDigits; 1118 } 1119 1120 1130 public void setCurrency(Currency theCurrency) { 1131 currency = theCurrency; 1132 } 1133 1134 1139 public Currency getCurrency() { 1140 return currency; 1141 } 1142 1143 1151 protected Currency getEffectiveCurrency() { 1152 Currency c = getCurrency(); 1153 if (c == null) { 1154 ULocale uloc = getLocale(ULocale.VALID_LOCALE); 1155 if (uloc == null) { 1156 uloc = ULocale.getDefault(); 1157 } 1158 c = Currency.getInstance(uloc); 1159 } 1160 return c; 1161 } 1162 1163 1165 private static NumberFormat getInstance(ULocale desiredLocale, int choice) { 1167 return getShim().createInstance(desiredLocale, choice); 1174 } 1175 1176 static NumberFormat createInstance(ULocale desiredLocale, int choice) { 1178 String pattern = getPattern(desiredLocale, choice); 1179 DecimalFormatSymbols symbols = new DecimalFormatSymbols(desiredLocale); 1180 1181 if(choice == CURRENCYSTYLE){ 1184 String temp = symbols.getCurrencyPattern(); 1185 if(temp!=null){ 1186 pattern = temp; 1187 } 1188 } 1189 1190 DecimalFormat format = new DecimalFormat(pattern, symbols); 1191 1193 1196 if (choice == INTEGERSTYLE) { 1199 format.setMaximumFractionDigits(0); 1200 format.setDecimalSeparatorAlwaysShown(false); 1201 format.setParseIntegerOnly(true); 1202 } 1203 1204 ULocale valid = symbols.getLocale(ULocale.VALID_LOCALE); 1208 ULocale actual = symbols.getLocale(ULocale.ACTUAL_LOCALE); 1209 format.setLocale(valid, actual); 1210 1211 return format; 1212 } 1213 1214 1221 protected static String getPattern(Locale forLocale, int choice) { 1222 return getPattern(ULocale.forLocale(forLocale), choice); 1223 } 1224 1225 1233 protected static String getPattern(ULocale forLocale, int choice) { 1234 1235 1251 1252 if (choice == SCIENTIFICSTYLE) { 1255 1257 return "#E0"; 1258 } 1260 1271 ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle. 1273 getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, forLocale); 1274 String [] numberPatterns = rb.getStringArray("NumberPatterns"); 1275 1276 1287 1288 1291 int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice; return numberPatterns[entry]; } 1294 1295 1306 private void readObject(ObjectInputStream stream) 1307 throws IOException , ClassNotFoundException 1308 { 1309 stream.defaultReadObject(); 1310 if (serialVersionOnStream < 1) { 1313 maximumIntegerDigits = maxIntegerDigits; 1315 minimumIntegerDigits = minIntegerDigits; 1316 maximumFractionDigits = maxFractionDigits; 1317 minimumFractionDigits = minFractionDigits; 1318 } 1319 1323 if (minimumIntegerDigits > maximumIntegerDigits || 1324 minimumFractionDigits > maximumFractionDigits || 1325 minimumIntegerDigits < 0 || minimumFractionDigits < 0) { 1326 throw new InvalidObjectException ("Digit count range invalid"); 1327 } 1328 serialVersionOnStream = currentSerialVersion; 1329 } 1330 1331 1338 private void writeObject(ObjectOutputStream stream) 1339 throws IOException 1340 { 1341 maxIntegerDigits = (maximumIntegerDigits > Byte.MAX_VALUE) ? Byte.MAX_VALUE : 1342 (byte)maximumIntegerDigits; 1343 minIntegerDigits = (minimumIntegerDigits > Byte.MAX_VALUE) ? Byte.MAX_VALUE : 1344 (byte)minimumIntegerDigits; 1345 maxFractionDigits = (maximumFractionDigits > Byte.MAX_VALUE) ? Byte.MAX_VALUE : 1346 (byte)maximumFractionDigits; 1347 minFractionDigits = (minimumFractionDigits > Byte.MAX_VALUE) ? Byte.MAX_VALUE : 1348 (byte)minimumFractionDigits; 1349 stream.defaultWriteObject(); 1350 } 1351 1352 1358 1361 1362 1369 private boolean groupingUsed = true; 1370 1371 1387 private byte maxIntegerDigits = 40; 1388 1389 1405 private byte minIntegerDigits = 1; 1406 1407 1423 private byte maxFractionDigits = 3; 1425 1441 private byte minFractionDigits = 0; 1442 1443 1449 private boolean parseIntegerOnly = false; 1450 1451 1453 1461 private int maximumIntegerDigits = 40; 1462 1463 1471 private int minimumIntegerDigits = 1; 1472 1473 1481 private int maximumFractionDigits = 3; 1483 1491 private int minimumFractionDigits = 0; 1492 1493 1499 private Currency currency; 1500 1501 static final int currentSerialVersion = 1; 1502 1503 1523 private int serialVersionOnStream = currentSerialVersion; 1524 1525 private static final long serialVersionUID = -2308460125733713944L; 1528 1529 1535 public NumberFormat() { 1536 } 1537 1538 private boolean parseStrict; 1540 1541} 1653 | Popular Tags |