DecimalFormat.java (14193:92280897299f) DecimalFormat.java (15733:f4463c138103)
1/*
1/*
2 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
2 * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *

--- 952 unchanged lines hidden (view full) ---

963 * - Decimal separator not mandatory,
964 * - No use of exponential notation,
965 * - minimumIntegerDigits is exactly 1 and maximumIntegerDigits at least 10
966 * - For number of fractional digits, the exact values found in the default case:
967 * Currency : min = max = 2.
968 * Decimal : min = 0. max = 3.
969 *
970 */
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *

--- 952 unchanged lines hidden (view full) ---

963 * - Decimal separator not mandatory,
964 * - No use of exponential notation,
965 * - minimumIntegerDigits is exactly 1 and maximumIntegerDigits at least 10
966 * - For number of fractional digits, the exact values found in the default case:
967 * Currency : min = max = 2.
968 * Decimal : min = 0. max = 3.
969 *
970 */
971 private void checkAndSetFastPathStatus() {
971 private boolean checkAndSetFastPathStatus() {
972
973 boolean fastPathWasOn = isFastPath;
974
975 if ((roundingMode == RoundingMode.HALF_EVEN) &&
976 (isGroupingUsed()) &&
977 (groupingSize == 3) &&
978 (multiplier == 1) &&
979 (!decimalSeparatorAlwaysShown) &&

--- 13 unchanged lines hidden (view full) ---

993 isFastPath = false;
994 } else if ((minimumFractionDigits != 0) ||
995 (maximumFractionDigits != 3))
996 isFastPath = false;
997 }
998 } else
999 isFastPath = false;
1000
972
973 boolean fastPathWasOn = isFastPath;
974
975 if ((roundingMode == RoundingMode.HALF_EVEN) &&
976 (isGroupingUsed()) &&
977 (groupingSize == 3) &&
978 (multiplier == 1) &&
979 (!decimalSeparatorAlwaysShown) &&

--- 13 unchanged lines hidden (view full) ---

993 isFastPath = false;
994 } else if ((minimumFractionDigits != 0) ||
995 (maximumFractionDigits != 3))
996 isFastPath = false;
997 }
998 } else
999 isFastPath = false;
1000
1001 resetFastPathData(fastPathWasOn);
1002 fastPathCheckNeeded = false;
1003
1004 /*
1005 * Returns true after successfully checking the fast path condition and
1006 * setting the fast path data. The return value is used by the
1007 * fastFormat() method to decide whether to call the resetFastPathData
1008 * method to reinitialize fast path data or is it already initialized
1009 * in this method.
1010 */
1011 return true;
1012 }
1013
1014 private void resetFastPathData(boolean fastPathWasOn) {
1001 // Since some instance properties may have changed while still falling
1002 // in the fast-path case, we need to reinitialize fastPathData anyway.
1003 if (isFastPath) {
1004 // We need to instantiate fastPathData if not already done.
1015 // Since some instance properties may have changed while still falling
1016 // in the fast-path case, we need to reinitialize fastPathData anyway.
1017 if (isFastPath) {
1018 // We need to instantiate fastPathData if not already done.
1005 if (fastPathData == null)
1019 if (fastPathData == null) {
1006 fastPathData = new FastPathData();
1020 fastPathData = new FastPathData();
1021 }
1007
1008 // Sets up the locale specific constants used when formatting.
1009 // '0' is our default representation of zero.
1010 fastPathData.zeroDelta = symbols.getZeroDigit() - '0';
1011 fastPathData.groupingChar = symbols.getGroupingSeparator();
1012
1013 // Sets up fractional constants related to currency/decimal pattern.
1022
1023 // Sets up the locale specific constants used when formatting.
1024 // '0' is our default representation of zero.
1025 fastPathData.zeroDelta = symbols.getZeroDigit() - '0';
1026 fastPathData.groupingChar = symbols.getGroupingSeparator();
1027
1028 // Sets up fractional constants related to currency/decimal pattern.
1014 fastPathData.fractionalMaxIntBound = (isCurrencyFormat) ? 99 : 999;
1015 fastPathData.fractionalScaleFactor = (isCurrencyFormat) ? 100.0d : 1000.0d;
1029 fastPathData.fractionalMaxIntBound = (isCurrencyFormat)
1030 ? 99 : 999;
1031 fastPathData.fractionalScaleFactor = (isCurrencyFormat)
1032 ? 100.0d : 1000.0d;
1016
1017 // Records the need for adding prefix or suffix
1033
1034 // Records the need for adding prefix or suffix
1018 fastPathData.positiveAffixesRequired =
1019 (positivePrefix.length() != 0) || (positiveSuffix.length() != 0);
1020 fastPathData.negativeAffixesRequired =
1021 (negativePrefix.length() != 0) || (negativeSuffix.length() != 0);
1035 fastPathData.positiveAffixesRequired
1036 = (positivePrefix.length() != 0)
1037 || (positiveSuffix.length() != 0);
1038 fastPathData.negativeAffixesRequired
1039 = (negativePrefix.length() != 0)
1040 || (negativeSuffix.length() != 0);
1022
1023 // Creates a cached char container for result, with max possible size.
1024 int maxNbIntegralDigits = 10;
1025 int maxNbGroups = 3;
1041
1042 // Creates a cached char container for result, with max possible size.
1043 int maxNbIntegralDigits = 10;
1044 int maxNbGroups = 3;
1026 int containerSize =
1027 Math.max(positivePrefix.length(), negativePrefix.length()) +
1028 maxNbIntegralDigits + maxNbGroups + 1 + maximumFractionDigits +
1029 Math.max(positiveSuffix.length(), negativeSuffix.length());
1045 int containerSize
1046 = Math.max(positivePrefix.length(), negativePrefix.length())
1047 + maxNbIntegralDigits + maxNbGroups + 1
1048 + maximumFractionDigits
1049 + Math.max(positiveSuffix.length(), negativeSuffix.length());
1030
1031 fastPathData.fastPathContainer = new char[containerSize];
1032
1033 // Sets up prefix and suffix char arrays constants.
1034 fastPathData.charsPositiveSuffix = positiveSuffix.toCharArray();
1035 fastPathData.charsNegativeSuffix = negativeSuffix.toCharArray();
1036 fastPathData.charsPositivePrefix = positivePrefix.toCharArray();
1037 fastPathData.charsNegativePrefix = negativePrefix.toCharArray();
1038
1039 // Sets up fixed index positions for integral and fractional digits.
1040 // Sets up decimal point in cached result container.
1050
1051 fastPathData.fastPathContainer = new char[containerSize];
1052
1053 // Sets up prefix and suffix char arrays constants.
1054 fastPathData.charsPositiveSuffix = positiveSuffix.toCharArray();
1055 fastPathData.charsNegativeSuffix = negativeSuffix.toCharArray();
1056 fastPathData.charsPositivePrefix = positivePrefix.toCharArray();
1057 fastPathData.charsNegativePrefix = negativePrefix.toCharArray();
1058
1059 // Sets up fixed index positions for integral and fractional digits.
1060 // Sets up decimal point in cached result container.
1041 int longestPrefixLength =
1042 Math.max(positivePrefix.length(), negativePrefix.length());
1043 int decimalPointIndex =
1044 maxNbIntegralDigits + maxNbGroups + longestPrefixLength;
1061 int longestPrefixLength
1062 = Math.max(positivePrefix.length(),
1063 negativePrefix.length());
1064 int decimalPointIndex
1065 = maxNbIntegralDigits + maxNbGroups + longestPrefixLength;
1045
1066
1046 fastPathData.integralLastIndex = decimalPointIndex - 1;
1067 fastPathData.integralLastIndex = decimalPointIndex - 1;
1047 fastPathData.fractionalFirstIndex = decimalPointIndex + 1;
1068 fastPathData.fractionalFirstIndex = decimalPointIndex + 1;
1048 fastPathData.fastPathContainer[decimalPointIndex] =
1049 isCurrencyFormat ?
1050 symbols.getMonetaryDecimalSeparator() :
1051 symbols.getDecimalSeparator();
1069 fastPathData.fastPathContainer[decimalPointIndex]
1070 = isCurrencyFormat
1071 ? symbols.getMonetaryDecimalSeparator()
1072 : symbols.getDecimalSeparator();
1052
1053 } else if (fastPathWasOn) {
1054 // Previous state was fast-path and is no more.
1055 // Resets cached array constants.
1056 fastPathData.fastPathContainer = null;
1057 fastPathData.charsPositiveSuffix = null;
1058 fastPathData.charsNegativeSuffix = null;
1059 fastPathData.charsPositivePrefix = null;
1060 fastPathData.charsNegativePrefix = null;
1061 }
1073
1074 } else if (fastPathWasOn) {
1075 // Previous state was fast-path and is no more.
1076 // Resets cached array constants.
1077 fastPathData.fastPathContainer = null;
1078 fastPathData.charsPositiveSuffix = null;
1079 fastPathData.charsNegativeSuffix = null;
1080 fastPathData.charsPositivePrefix = null;
1081 fastPathData.charsNegativePrefix = null;
1082 }
1062
1063 fastPathCheckNeeded = false;
1064 }
1065
1066 /**
1067 * Returns true if rounding-up must be done on {@code scaledFractionalPartAsInt},
1068 * false otherwise.
1069 *
1070 * This is a utility method that takes correct half-even rounding decision on
1071 * passed fractional value at the scaled decimal point (2 digits for currency

--- 477 unchanged lines hidden (view full) ---

1549 *
1550 * Otherwise returns null by convention since fast-path can't be exercized.
1551 *
1552 * @param d The double value to be formatted
1553 *
1554 * @return the formatted result for {@code d} as a string.
1555 */
1556 String fastFormat(double d) {
1083 }
1084
1085 /**
1086 * Returns true if rounding-up must be done on {@code scaledFractionalPartAsInt},
1087 * false otherwise.
1088 *
1089 * This is a utility method that takes correct half-even rounding decision on
1090 * passed fractional value at the scaled decimal point (2 digits for currency

--- 477 unchanged lines hidden (view full) ---

1568 *
1569 * Otherwise returns null by convention since fast-path can't be exercized.
1570 *
1571 * @param d The double value to be formatted
1572 *
1573 * @return the formatted result for {@code d} as a string.
1574 */
1575 String fastFormat(double d) {
1576 boolean isDataSet = false;
1557 // (Re-)Evaluates fast-path status if needed.
1577 // (Re-)Evaluates fast-path status if needed.
1558 if (fastPathCheckNeeded)
1559 checkAndSetFastPathStatus();
1578 if (fastPathCheckNeeded) {
1579 isDataSet = checkAndSetFastPathStatus();
1580 }
1560
1561 if (!isFastPath )
1562 // DecimalFormat instance is not in a fast-path state.
1563 return null;
1564
1565 if (!Double.isFinite(d))
1566 // Should not use fast-path for Infinity and NaN.
1567 return null;

--- 7 unchanged lines hidden (view full) ---

1575 } else if (d == 0.0d) {
1576 negative = (Math.copySign(1.0d, d) == -1.0d);
1577 d = +0.0d;
1578 }
1579
1580 if (d > MAX_INT_AS_DOUBLE)
1581 // Filters out values that are outside expected fast-path range
1582 return null;
1581
1582 if (!isFastPath )
1583 // DecimalFormat instance is not in a fast-path state.
1584 return null;
1585
1586 if (!Double.isFinite(d))
1587 // Should not use fast-path for Infinity and NaN.
1588 return null;

--- 7 unchanged lines hidden (view full) ---

1596 } else if (d == 0.0d) {
1597 negative = (Math.copySign(1.0d, d) == -1.0d);
1598 d = +0.0d;
1599 }
1600
1601 if (d > MAX_INT_AS_DOUBLE)
1602 // Filters out values that are outside expected fast-path range
1603 return null;
1583 else
1604 else {
1605 if (!isDataSet) {
1606 /*
1607 * If the fast path data is not set through
1608 * checkAndSetFastPathStatus() and fulfil the
1609 * fast path conditions then reset the data
1610 * directly through resetFastPathData()
1611 */
1612 resetFastPathData(isFastPath);
1613 }
1584 fastDoubleFormat(d, negative);
1585
1614 fastDoubleFormat(d, negative);
1615
1616 }
1617
1618
1586 // Returns a new string from updated fastPathContainer.
1587 return new String(fastPathData.fastPathContainer,
1588 fastPathData.firstUsedIndex,
1589 fastPathData.lastFreeIndex - fastPathData.firstUsedIndex);
1590
1591 }
1592
1593 // ======== End fast-path formating logic for double =========================

--- 2597 unchanged lines hidden ---
1619 // Returns a new string from updated fastPathContainer.
1620 return new String(fastPathData.fastPathContainer,
1621 fastPathData.firstUsedIndex,
1622 fastPathData.lastFreeIndex - fastPathData.firstUsedIndex);
1623
1624 }
1625
1626 // ======== End fast-path formating logic for double =========================

--- 2597 unchanged lines hidden ---