Skip to content

Commit 406c2a9

Browse files
committedOct 15, 2019
8212749: DecimalFormat.setGroupingSize(int) allows setting negative grouping size
8231984: Clarify semantics of DecimalFormat.getGroupingSize(0) Reviewed-by: rriggs
1 parent 65a6012 commit 406c2a9

File tree

2 files changed

+101
-5
lines changed

2 files changed

+101
-5
lines changed
 

‎src/java.base/share/classes/java/text/DecimalFormat.java

+27-5
Original file line numberDiff line numberDiff line change
@@ -2756,7 +2756,10 @@ public void setGroupingUsed(boolean newValue) {
27562756
/**
27572757
* Return the grouping size. Grouping size is the number of digits between
27582758
* grouping separators in the integer portion of a number. For example,
2759-
* in the number "123,456.78", the grouping size is 3.
2759+
* in the number "123,456.78", the grouping size is 3. Grouping size of
2760+
* zero designates that grouping is not used, which provides the same
2761+
* formatting as if calling {@link #setGroupingUsed(boolean)
2762+
* setGroupingUsed(false)}.
27602763
*
27612764
* @return the grouping size
27622765
* @see #setGroupingSize
@@ -2770,16 +2773,28 @@ public int getGroupingSize () {
27702773
/**
27712774
* Set the grouping size. Grouping size is the number of digits between
27722775
* grouping separators in the integer portion of a number. For example,
2773-
* in the number "123,456.78", the grouping size is 3.
2774-
* <br>
2776+
* in the number "123,456.78", the grouping size is 3. Grouping size of
2777+
* zero designates that grouping is not used, which provides the same
2778+
* formatting as if calling {@link #setGroupingUsed(boolean)
2779+
* setGroupingUsed(false)}.
2780+
* <p>
27752781
* The value passed in is converted to a byte, which may lose information.
2782+
* Values that are negative or greater than
2783+
* {@link java.lang.Byte#MAX_VALUE Byte.MAX_VALUE}, will throw an
2784+
* {@code IllegalArgumentException}.
27762785
*
27772786
* @param newValue the new grouping size
27782787
* @see #getGroupingSize
27792788
* @see java.text.NumberFormat#setGroupingUsed
27802789
* @see java.text.DecimalFormatSymbols#setGroupingSeparator
2790+
* @throws IllegalArgumentException if {@code newValue} is negative or
2791+
* greater than {@link java.lang.Byte#MAX_VALUE Byte.MAX_VALUE}
27812792
*/
27822793
public void setGroupingSize (int newValue) {
2794+
if (newValue < 0 || newValue > Byte.MAX_VALUE) {
2795+
throw new IllegalArgumentException(
2796+
"newValue is out of valid range. value: " + newValue);
2797+
}
27832798
groupingSize = (byte)newValue;
27842799
fastPathCheckNeeded = true;
27852800
}
@@ -3906,6 +3921,12 @@ private void readObject(ObjectInputStream stream)
39063921
// Didn't have exponential fields
39073922
useExponentialNotation = false;
39083923
}
3924+
3925+
// Restore the invariant value if groupingSize is invalid.
3926+
if (groupingSize < 0) {
3927+
groupingSize = 3;
3928+
}
3929+
39093930
serialVersionOnStream = currentSerialVersion;
39103931
}
39113932

@@ -4009,14 +4030,15 @@ private void readObject(ObjectInputStream stream)
40094030

40104031
/**
40114032
* The number of digits between grouping separators in the integer
4012-
* portion of a number. Must be greater than 0 if
4033+
* portion of a number. Must be non-negative and less than or equal to
4034+
* {@link java.lang.Byte#MAX_VALUE Byte.MAX_VALUE} if
40134035
* {@code NumberFormat.groupingUsed} is true.
40144036
*
40154037
* @serial
40164038
* @see #getGroupingSize
40174039
* @see java.text.NumberFormat#isGroupingUsed
40184040
*/
4019-
private byte groupingSize = 3; // invariant, > 0 if useThousands
4041+
private byte groupingSize = 3; // invariant, 0 - 127, if groupingUsed
40204042

40214043
/**
40224044
* If true, forces the decimal separator to always appear in a formatted
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2019, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8212749
27+
* @summary test whether input value check for
28+
* DecimalFormat.setGroupingSize(int) works correctly.
29+
* @run testng/othervm SetGroupingSizeTest
30+
*/
31+
32+
import java.text.DecimalFormat;
33+
34+
import static org.testng.Assert.assertEquals;
35+
import org.testng.annotations.DataProvider;
36+
import org.testng.annotations.Test;
37+
38+
@Test
39+
public class SetGroupingSizeTest {
40+
41+
@DataProvider
42+
public static Object[][] validGroupingSizes() {
43+
return new Object[][] {
44+
{ 0 },
45+
{ Byte.MAX_VALUE },
46+
};
47+
}
48+
49+
@DataProvider
50+
public static Object[][] invalidGroupingSizes() {
51+
return new Object[][] {
52+
{ Byte.MIN_VALUE - 1 },
53+
{ Byte.MIN_VALUE },
54+
{ -1 },
55+
{ Byte.MAX_VALUE + 1 },
56+
{ Integer.MIN_VALUE },
57+
{ Integer.MAX_VALUE },
58+
};
59+
}
60+
61+
@Test(dataProvider = "validGroupingSizes")
62+
public void test_validGroupingSize(int newVal) {
63+
DecimalFormat df = new DecimalFormat();
64+
df.setGroupingSize(newVal);
65+
assertEquals(df.getGroupingSize(), newVal);
66+
}
67+
68+
@Test(dataProvider = "invalidGroupingSizes",
69+
expectedExceptions = IllegalArgumentException.class)
70+
public void test_invalidGroupingSize(int newVal) {
71+
DecimalFormat df = new DecimalFormat();
72+
df.setGroupingSize(newVal);
73+
}
74+
}

0 commit comments

Comments
 (0)
Failed to load comments.