1 7 8 20 21 package java.text; 22 23 import java.io.InvalidObjectException ; 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.math.BigInteger ; 28 import java.util.Currency ; 29 import java.util.HashMap ; 30 import java.util.Hashtable ; 31 import java.util.Locale ; 32 import java.util.Map ; 33 import java.util.ResourceBundle ; 34 import sun.text.resources.LocaleData; 35 36 159 public abstract class NumberFormat extends Format { 160 161 166 public static final int INTEGER_FIELD = 0; 167 168 173 public static final int FRACTION_FIELD = 1; 174 175 203 public StringBuffer format(Object number, 204 StringBuffer toAppendTo, 205 FieldPosition pos) { 206 if (number instanceof Long || number instanceof Integer || 207 number instanceof Short || number instanceof Byte || 208 (number instanceof BigInteger && 209 ((BigInteger )number).bitLength() < 64)) { 210 return format(((Number )number).longValue(), toAppendTo, pos); 211 } else if (number instanceof Number ) { 212 return format(((Number )number).doubleValue(), toAppendTo, pos); 213 } else { 214 throw new IllegalArgumentException ("Cannot format given Object as a Number"); 215 } 216 } 217 218 242 public final Object parseObject(String source, ParsePosition pos) { 243 return parse(source, pos); 244 } 245 246 250 public final String format(double number) { 251 return format(number, new StringBuffer (), 252 DontCareFieldPosition.INSTANCE).toString(); 253 } 254 255 259 public final String format(long number) { 260 return format(number, new StringBuffer (), 261 DontCareFieldPosition.INSTANCE).toString(); 262 } 263 264 268 public abstract StringBuffer format(double number, 269 StringBuffer toAppendTo, 270 FieldPosition pos); 271 272 276 public abstract StringBuffer format(long number, 277 StringBuffer toAppendTo, 278 FieldPosition pos); 279 280 291 public abstract Number parse(String source, ParsePosition parsePosition); 292 293 305 public Number parse(String source) throws ParseException { 306 ParsePosition parsePosition = new ParsePosition (0); 307 Number result = parse(source, parsePosition); 308 if (parsePosition.index == 0) { 309 throw new ParseException ("Unparseable number: \"" + source + "\"", 310 parsePosition.errorIndex); 311 } 312 return result; 313 } 314 315 323 public boolean isParseIntegerOnly() { 324 return parseIntegerOnly; 325 } 326 327 331 public void setParseIntegerOnly(boolean value) { 332 parseIntegerOnly = value; 333 } 334 335 337 342 public final static NumberFormat getInstance() { 343 return getInstance(Locale.getDefault(), NUMBERSTYLE); 344 } 345 346 351 public static NumberFormat getInstance(Locale inLocale) { 352 return getInstance(inLocale, NUMBERSTYLE); 353 } 354 355 358 public final static NumberFormat getNumberInstance() { 359 return getInstance(Locale.getDefault(), NUMBERSTYLE); 360 } 361 362 365 public static NumberFormat getNumberInstance(Locale inLocale) { 366 return getInstance(inLocale, NUMBERSTYLE); 367 } 368 369 380 public final static NumberFormat getIntegerInstance() { 381 return getInstance(Locale.getDefault(), INTEGERSTYLE); 382 } 383 384 396 public static NumberFormat getIntegerInstance(Locale inLocale) { 397 return getInstance(inLocale, INTEGERSTYLE); 398 } 399 400 403 public final static NumberFormat getCurrencyInstance() { 404 return getInstance(Locale.getDefault(), CURRENCYSTYLE); 405 } 406 407 410 public static NumberFormat getCurrencyInstance(Locale inLocale) { 411 return getInstance(inLocale, CURRENCYSTYLE); 412 } 413 414 417 public final static NumberFormat getPercentInstance() { 418 return getInstance(Locale.getDefault(), PERCENTSTYLE); 419 } 420 421 424 public static NumberFormat getPercentInstance(Locale inLocale) { 425 return getInstance(inLocale, PERCENTSTYLE); 426 } 427 428 431 final static NumberFormat getScientificInstance() { 432 return getInstance(Locale.getDefault(), SCIENTIFICSTYLE); 433 } 434 435 438 static NumberFormat getScientificInstance(Locale inLocale) { 439 return getInstance(inLocale, SCIENTIFICSTYLE); 440 } 441 442 452 public static Locale [] getAvailableLocales() { 453 return LocaleData.getAvailableLocales("NumberPatterns"); 454 } 455 456 459 public int hashCode() { 460 return maximumIntegerDigits * 37 + maxFractionDigits; 461 } 463 464 467 public boolean equals(Object obj) { 468 if (obj == null) { 469 return false; 470 } 471 if (this == obj) { 472 return true; 473 } 474 if (getClass() != obj.getClass()) { 475 return false; 476 } 477 NumberFormat other = (NumberFormat ) obj; 478 return (maximumIntegerDigits == other.maximumIntegerDigits 479 && minimumIntegerDigits == other.minimumIntegerDigits 480 && maximumFractionDigits == other.maximumFractionDigits 481 && minimumFractionDigits == other.minimumFractionDigits 482 && groupingUsed == other.groupingUsed 483 && parseIntegerOnly == other.parseIntegerOnly); 484 } 485 486 489 public Object clone() { 490 NumberFormat other = (NumberFormat ) super.clone(); 491 return other; 492 } 493 494 501 public boolean isGroupingUsed() { 502 return groupingUsed; 503 } 504 505 509 public void setGroupingUsed(boolean newValue) { 510 groupingUsed = newValue; 511 } 512 513 518 public int getMaximumIntegerDigits() { 519 return maximumIntegerDigits; 520 } 521 522 533 public void setMaximumIntegerDigits(int newValue) { 534 maximumIntegerDigits = Math.max(0,newValue); 535 if (minimumIntegerDigits > maximumIntegerDigits) { 536 minimumIntegerDigits = maximumIntegerDigits; 537 } 538 } 539 540 545 public int getMinimumIntegerDigits() { 546 return minimumIntegerDigits; 547 } 548 549 560 public void setMinimumIntegerDigits(int newValue) { 561 minimumIntegerDigits = Math.max(0,newValue); 562 if (minimumIntegerDigits > maximumIntegerDigits) { 563 maximumIntegerDigits = minimumIntegerDigits; 564 } 565 } 566 567 572 public int getMaximumFractionDigits() { 573 return maximumFractionDigits; 574 } 575 576 587 public void setMaximumFractionDigits(int newValue) { 588 maximumFractionDigits = Math.max(0,newValue); 589 if (maximumFractionDigits < minimumFractionDigits) { 590 minimumFractionDigits = maximumFractionDigits; 591 } 592 } 593 594 599 public int getMinimumFractionDigits() { 600 return minimumFractionDigits; 601 } 602 603 614 public void setMinimumFractionDigits(int newValue) { 615 minimumFractionDigits = Math.max(0,newValue); 616 if (maximumFractionDigits < minimumFractionDigits) { 617 maximumFractionDigits = minimumFractionDigits; 618 } 619 } 620 621 636 public Currency getCurrency() { 637 throw new UnsupportedOperationException (); 638 } 639 640 654 public void setCurrency(Currency currency) { 655 throw new UnsupportedOperationException (); 656 } 657 658 660 private static NumberFormat getInstance(Locale desiredLocale, 661 int choice) { 662 663 String [] numberPatterns = (String [])cachedLocaleData.get(desiredLocale); 664 if (numberPatterns == null) { 665 ResourceBundle resource = LocaleData.getLocaleElements(desiredLocale); 666 numberPatterns = resource.getStringArray("NumberPatterns"); 667 668 cachedLocaleData.put(desiredLocale, numberPatterns); 669 } 670 671 DecimalFormatSymbols symbols = new DecimalFormatSymbols (desiredLocale); 672 int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice; 673 DecimalFormat format = new DecimalFormat (numberPatterns[entry], symbols); 674 675 if (choice == INTEGERSTYLE) { 676 format.setMaximumFractionDigits(0); 677 format.setDecimalSeparatorAlwaysShown(false); 678 format.setParseIntegerOnly(true); 679 } else if (choice == CURRENCYSTYLE) { 680 format.adjustForCurrencyDefaultFractionDigits(); 681 } 682 683 return format; 684 } 685 686 706 private void readObject(ObjectInputStream stream) 707 throws IOException , ClassNotFoundException 708 { 709 stream.defaultReadObject(); 710 if (serialVersionOnStream < 1) { 711 maximumIntegerDigits = maxIntegerDigits; 713 minimumIntegerDigits = minIntegerDigits; 714 maximumFractionDigits = maxFractionDigits; 715 minimumFractionDigits = minFractionDigits; 716 } 717 if (minimumIntegerDigits > maximumIntegerDigits || 718 minimumFractionDigits > maximumFractionDigits || 719 minimumIntegerDigits < 0 || minimumFractionDigits < 0) { 720 throw new InvalidObjectException ("Digit count range invalid"); 721 } 722 serialVersionOnStream = currentSerialVersion; 723 } 724 725 734 private void writeObject(ObjectOutputStream stream) 735 throws IOException 736 { 737 maxIntegerDigits = (maximumIntegerDigits > Byte.MAX_VALUE) ? 738 Byte.MAX_VALUE : (byte)maximumIntegerDigits; 739 minIntegerDigits = (minimumIntegerDigits > Byte.MAX_VALUE) ? 740 Byte.MAX_VALUE : (byte)minimumIntegerDigits; 741 maxFractionDigits = (maximumFractionDigits > Byte.MAX_VALUE) ? 742 Byte.MAX_VALUE : (byte)maximumFractionDigits; 743 minFractionDigits = (minimumFractionDigits > Byte.MAX_VALUE) ? 744 Byte.MAX_VALUE : (byte)minimumFractionDigits; 745 stream.defaultWriteObject(); 746 } 747 748 751 private static final Hashtable cachedLocaleData = new Hashtable (3); 752 753 private static final int NUMBERSTYLE = 0; 755 private static final int CURRENCYSTYLE = 1; 756 private static final int PERCENTSTYLE = 2; 757 private static final int SCIENTIFICSTYLE = 3; 758 private static final int INTEGERSTYLE = 4; 759 760 767 private boolean groupingUsed = true; 768 769 785 private byte maxIntegerDigits = 40; 786 787 803 private byte minIntegerDigits = 1; 804 805 821 private byte maxFractionDigits = 3; 823 839 private byte minFractionDigits = 0; 840 841 847 private boolean parseIntegerOnly = false; 848 849 851 860 private int maximumIntegerDigits = 40; 861 862 871 private int minimumIntegerDigits = 1; 872 873 882 private int maximumFractionDigits = 3; 884 893 private int minimumFractionDigits = 0; 894 895 static final int currentSerialVersion = 1; 896 897 918 private int serialVersionOnStream = currentSerialVersion; 919 920 static final long serialVersionUID = -2308460125733713944L; 923 924 925 936 public static class Field extends Format.Field { 937 938 private static final long serialVersionUID = 7494728892700160890L; 940 941 private static final Map instanceMap = new HashMap (11); 943 944 950 protected Field(String name) { 951 super(name); 952 if (this.getClass() == NumberFormat.Field .class) { 953 instanceMap.put(name, this); 954 } 955 } 956 957 963 protected Object readResolve() throws InvalidObjectException { 964 if (this.getClass() != NumberFormat.Field .class) { 965 throw new InvalidObjectException ("subclass didn't correctly implement readResolve"); 966 } 967 968 Object instance = instanceMap.get(getName()); 969 if (instance != null) { 970 return instance; 971 } else { 972 throw new InvalidObjectException ("unknown attribute name"); 973 } 974 } 975 976 979 public static final Field INTEGER = new Field("integer"); 980 981 984 public static final Field FRACTION = new Field("fraction"); 985 986 989 public static final Field EXPONENT = new Field("exponent"); 990 991 994 public static final Field DECIMAL_SEPARATOR = 995 new Field("decimal separator"); 996 997 1000 public static final Field SIGN = new Field("sign"); 1001 1002 1005 public static final Field GROUPING_SEPARATOR = 1006 new Field("grouping separator"); 1007 1008 1011 public static final Field EXPONENT_SYMBOL = new 1012 Field("exponent symbol"); 1013 1014 1017 public static final Field PERCENT = new Field("percent"); 1018 1019 1022 public static final Field PERMILLE = new Field("per mille"); 1023 1024 1027 public static final Field CURRENCY = new Field("currency"); 1028 1029 1032 public static final Field EXPONENT_SIGN = new Field("exponent sign"); 1033 } 1034} 1035 | Popular Tags |