ICU-13231 Changing DecimalFormat min/max setters to use the most recent setting instead of always preferring the minimum for backwards compatibility.

X-SVN-Rev: 40186
This commit is contained in:
Shane Carr 2017-06-21 00:44:47 +00:00
parent e9c5e5631b
commit c25183efdd
2 changed files with 94 additions and 1 deletions

View file

@ -1326,12 +1326,21 @@ public class DecimalFormat extends NumberFormat {
* maximum fraction digits. Note that it is not possible to specify maximium integer digits in the
* pattern except in scientific notation.
*
* <p>If minimum and maximum integer, fraction, or significant digits conflict with each other,
* the most recently specified value is used. For example, if there is a formatter with minInt=5,
* and then you set maxInt=3, then minInt will be changed to 3.
*
* @param value The minimum number of digits before the decimal separator.
* @category Rounding
* @stable ICU 2.0
*/
@Override
public synchronized void setMinimumIntegerDigits(int value) {
// For backwards compatibility, conflicting min/max need to keep the most recent setting.
int max = properties.getMaximumIntegerDigits();
if (max >= 0 && max < value) {
properties.setMaximumIntegerDigits(value);
}
properties.setMinimumIntegerDigits(value);
refreshFormatter();
}
@ -1359,12 +1368,20 @@ public class DecimalFormat extends NumberFormat {
* maximum fraction digits. Note that it is not possible to specify maximium integer digits in the
* pattern except in scientific notation.
*
* <p>If minimum and maximum integer, fraction, or significant digits conflict with each other,
* the most recently specified value is used. For example, if there is a formatter with minInt=5,
* and then you set maxInt=3, then minInt will be changed to 3.
*
* @param value The maximum number of digits before the decimal separator.
* @category Rounding
* @stable ICU 2.0
*/
@Override
public synchronized void setMaximumIntegerDigits(int value) {
int min = properties.getMinimumIntegerDigits();
if (min >= 0 && min > value) {
properties.setMinimumIntegerDigits(value);
}
properties.setMaximumIntegerDigits(value);
refreshFormatter();
}
@ -1393,6 +1410,10 @@ public class DecimalFormat extends NumberFormat {
* maximum fraction digits. Note that it is not possible to specify maximium integer digits in the
* pattern except in scientific notation.
*
* <p>If minimum and maximum integer, fraction, or significant digits conflict with each other,
* the most recently specified value is used. For example, if there is a formatter with minInt=5,
* and then you set maxInt=3, then minInt will be changed to 3.
*
* <p>See {@link #setRoundingIncrement} and {@link #setMaximumSignificantDigits} for two other
* ways of specifying rounding strategies.
*
@ -1405,6 +1426,10 @@ public class DecimalFormat extends NumberFormat {
*/
@Override
public synchronized void setMinimumFractionDigits(int value) {
int max = properties.getMaximumFractionDigits();
if (max >= 0 && max < value) {
properties.setMaximumFractionDigits(value);
}
properties.setMinimumFractionDigits(value);
refreshFormatter();
}
@ -1434,6 +1459,10 @@ public class DecimalFormat extends NumberFormat {
* maximum fraction digits. Note that it is not possible to specify maximium integer digits in the
* pattern except in scientific notation.
*
* <p>If minimum and maximum integer, fraction, or significant digits conflict with each other,
* the most recently specified value is used. For example, if there is a formatter with minInt=5,
* and then you set maxInt=3, then minInt will be changed to 3.
*
* @param value The maximum number of integer digits after the decimal separator.
* @see #setRoundingMode
* @category Rounding
@ -1441,6 +1470,10 @@ public class DecimalFormat extends NumberFormat {
*/
@Override
public synchronized void setMaximumFractionDigits(int value) {
int min = properties.getMinimumFractionDigits();
if (min >= 0 && min > value) {
properties.setMinimumFractionDigits(value);
}
properties.setMaximumFractionDigits(value);
refreshFormatter();
}
@ -1503,12 +1536,20 @@ public class DecimalFormat extends NumberFormat {
* <p>For example, if minimum significant digits is 3 and the number is 1.2, the number will be
* printed as "1.20".
*
* <p>If minimum and maximum integer, fraction, or significant digits conflict with each other,
* the most recently specified value is used. For example, if there is a formatter with minInt=5,
* and then you set maxInt=3, then minInt will be changed to 3.
*
* @param value The minimum number of significant digits to display.
* @see #setSignificantDigitsMode
* @category Rounding
* @stable ICU 3.0
*/
public synchronized void setMinimumSignificantDigits(int value) {
int max = properties.getMaximumSignificantDigits();
if (max >= 0 && max < value) {
properties.setMaximumSignificantDigits(value);
}
properties.setMinimumSignificantDigits(value);
refreshFormatter();
}
@ -1532,6 +1573,10 @@ public class DecimalFormat extends NumberFormat {
* <p>For example, if maximum significant digits is 3 and the number is 12345, the number will be
* printed as "12300".
*
* <p>If minimum and maximum integer, fraction, or significant digits conflict with each other,
* the most recently specified value is used. For example, if there is a formatter with minInt=5,
* and then you set maxInt=3, then minInt will be changed to 3.
*
* <p>See {@link #setRoundingIncrement} and {@link #setMaximumFractionDigits} for two other ways
* of specifying rounding strategies.
*
@ -1544,6 +1589,10 @@ public class DecimalFormat extends NumberFormat {
* @stable ICU 3.0
*/
public synchronized void setMaximumSignificantDigits(int value) {
int min = properties.getMinimumSignificantDigits();
if (min >= 0 && min > value) {
properties.setMinimumSignificantDigits(value);
}
properties.setMaximumSignificantDigits(value);
refreshFormatter();
}

View file

@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.AttributedCharacterIterator;
@ -3371,7 +3372,7 @@ public class NumberFormatTest extends TestFmwk {
// For valid array, it is displayed as {min value, max value}
// Tests when "if (minimumIntegerDigits > maximumIntegerDigits)" is true
int[][] cases = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 2, 0 }, { 2, 1 }, { 10, 0 } };
int[] expectedMax = { 1, 1, 1, 2, 2, 10 };
int[] expectedMax = { 1, 1, 0, 0, 1, 0 };
if (cases.length != expectedMax.length) {
errln("Can't continue test case method TestSetMinimumIntegerDigits "
+ "since the test case arrays are unequal.");
@ -4752,6 +4753,49 @@ public class NumberFormatTest extends TestFmwk {
}
}
@Test
public void TestMinMaxOverrides()
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
NoSuchMethodException, SecurityException {
Class<?>[] baseClasses = {NumberFormat.class, NumberFormat.class, DecimalFormat.class};
String[] names = {"Integer", "Fraction", "Significant"};
for (int i = 0; i < 3; i++) {
DecimalFormat df = new DecimalFormat();
Class<?> base = baseClasses[i];
String name = names[i];
Method getMinimum = base.getDeclaredMethod("getMinimum" + name + "Digits");
Method setMinimum = base.getDeclaredMethod("setMinimum" + name + "Digits", Integer.TYPE);
Method getMaximum = base.getDeclaredMethod("getMaximum" + name + "Digits");
Method setMaximum = base.getDeclaredMethod("setMaximum" + name + "Digits", Integer.TYPE);
// Check max overrides min
setMinimum.invoke(df, 2);
assertEquals(name + " getMin A", 2, getMinimum.invoke(df));
setMaximum.invoke(df, 3);
assertEquals(name + " getMin B", 2, getMinimum.invoke(df));
assertEquals(name + " getMax B", 3, getMaximum.invoke(df));
setMaximum.invoke(df, 2);
assertEquals(name + " getMin C", 2, getMinimum.invoke(df));
assertEquals(name + " getMax C", 2, getMaximum.invoke(df));
setMaximum.invoke(df, 1);
assertEquals(name + " getMin D", 1, getMinimum.invoke(df));
assertEquals(name + " getMax D", 1, getMaximum.invoke(df));
// Check min overrides max
setMaximum.invoke(df, 2);
assertEquals(name + " getMax E", 2, getMaximum.invoke(df));
setMinimum.invoke(df, 1);
assertEquals(name + " getMin F", 1, getMinimum.invoke(df));
assertEquals(name + " getMax F", 2, getMaximum.invoke(df));
setMinimum.invoke(df, 2);
assertEquals(name + " getMin G", 2, getMinimum.invoke(df));
assertEquals(name + " getMax G", 2, getMaximum.invoke(df));
setMinimum.invoke(df, 3);
assertEquals(name + " getMin H", 3, getMinimum.invoke(df));
assertEquals(name + " getMax H", 3, getMaximum.invoke(df));
}
}
@Test
public void Test10436() {
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.ENGLISH);