@@ -2756,7 +2756,10 @@ public void setGroupingUsed(boolean newValue) {
2756
2756
/**
2757
2757
* Return the grouping size. Grouping size is the number of digits between
2758
2758
* 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)}.
2760
2763
*
2761
2764
* @return the grouping size
2762
2765
* @see #setGroupingSize
@@ -2770,16 +2773,28 @@ public int getGroupingSize () {
2770
2773
/**
2771
2774
* Set the grouping size. Grouping size is the number of digits between
2772
2775
* 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>
2775
2781
* 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}.
2776
2785
*
2777
2786
* @param newValue the new grouping size
2778
2787
* @see #getGroupingSize
2779
2788
* @see java.text.NumberFormat#setGroupingUsed
2780
2789
* @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}
2781
2792
*/
2782
2793
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
+ }
2783
2798
groupingSize = (byte )newValue ;
2784
2799
fastPathCheckNeeded = true ;
2785
2800
}
@@ -3906,6 +3921,12 @@ private void readObject(ObjectInputStream stream)
3906
3921
// Didn't have exponential fields
3907
3922
useExponentialNotation = false ;
3908
3923
}
3924
+
3925
+ // Restore the invariant value if groupingSize is invalid.
3926
+ if (groupingSize < 0 ) {
3927
+ groupingSize = 3 ;
3928
+ }
3929
+
3909
3930
serialVersionOnStream = currentSerialVersion ;
3910
3931
}
3911
3932
@@ -4009,14 +4030,15 @@ private void readObject(ObjectInputStream stream)
4009
4030
4010
4031
/**
4011
4032
* 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
4013
4035
* {@code NumberFormat.groupingUsed} is true.
4014
4036
*
4015
4037
* @serial
4016
4038
* @see #getGroupingSize
4017
4039
* @see java.text.NumberFormat#isGroupingUsed
4018
4040
*/
4019
- private byte groupingSize = 3 ; // invariant, > 0 if useThousands
4041
+ private byte groupingSize = 3 ; // invariant, 0 - 127, if groupingUsed
4020
4042
4021
4043
/**
4022
4044
* If true, forces the decimal separator to always appear in a formatted
0 commit comments