Skip to content

Commit 96607df

Browse files
author
Justin Lu
committedJan 23, 2024
8321545: Override toString() for Format subclasses
Reviewed-by: naoto, rriggs
1 parent edfee7f commit 96607df

File tree

12 files changed

+419
-7
lines changed

12 files changed

+419
-7
lines changed
 

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -614,6 +614,17 @@ public int hashCode() {
614614
return result;
615615
}
616616

617+
/**
618+
* {@return a string identifying this {@code ChoiceFormat}, for debugging}
619+
*/
620+
@Override
621+
public String toString() {
622+
return
623+
"""
624+
ChoiceFormat [pattern: "%s"]
625+
""".formatted(toPattern());
626+
}
627+
617628
/**
618629
* Compares the specified object with this {@code ChoiceFormat} for equality.
619630
* Returns true if the object is also a {@code ChoiceFormat} and the

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -2391,6 +2391,17 @@ public int hashCode() {
23912391
+ Boolean.hashCode(parseBigDecimal);
23922392
}
23932393

2394+
/**
2395+
* {@return a string identifying this {@code CompactNumberFormat}, for debugging}
2396+
*/
2397+
@Override
2398+
public String toString() {
2399+
return
2400+
"""
2401+
CompactNumberFormat [locale: "%s", decimal pattern: "%s", compact patterns: "%s"]
2402+
""".formatted(symbols.getLocale().getDisplayName(), decimalPattern, Arrays.toString(compactPatterns));
2403+
}
2404+
23942405
/**
23952406
* Creates and returns a copy of this {@code CompactNumberFormat}
23962407
* instance.

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -2996,6 +2996,17 @@ public int hashCode() {
29962996
// just enough fields for a reasonable distribution
29972997
}
29982998

2999+
/**
3000+
* {@return a string identifying this {@code DecimalFormat}, for debugging}
3001+
*/
3002+
@Override
3003+
public String toString() {
3004+
return
3005+
"""
3006+
DecimalFormat [locale: "%s", pattern: "%s"]
3007+
""".formatted(symbols.getLocale().getDisplayName(), toPattern());
3008+
}
3009+
29993010
/**
30003011
* Synthesizes a pattern string that represents the current state
30013012
* of this Format object.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -530,7 +530,7 @@ public int hashCode() {
530530
}
531531

532532
/**
533-
* {@inheritDoc}
533+
* {@return a string identifying this {@code ListFormat}, for debugging}
534534
*/
535535
@Override
536536
public String toString() {

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1184,6 +1184,17 @@ public int hashCode() {
11841184
return pattern.hashCode(); // enough for reasonable distribution
11851185
}
11861186

1187+
/**
1188+
* {@return a string identifying this {@code MessageFormat}, for debugging}
1189+
*/
1190+
@Override
1191+
public String toString() {
1192+
return
1193+
"""
1194+
MessageFormat [locale: %s, pattern: "%s"]
1195+
""".formatted(locale == null ? null : '"' + locale.getDisplayName() + '"', toPattern());
1196+
}
1197+
11871198

11881199
/**
11891200
* Defines constants that are used as attribute keys in the

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -2422,6 +2422,17 @@ public int hashCode()
24222422
// just enough fields for a reasonable distribution
24232423
}
24242424

2425+
/**
2426+
* {@return a string identifying this {@code SimpleDateFormat}, for debugging}
2427+
*/
2428+
@Override
2429+
public String toString() {
2430+
return
2431+
"""
2432+
SimpleDateFormat [locale: %s, pattern: "%s"]
2433+
""".formatted(locale == null ? null : '"' + locale.getDisplayName() + '"', toPattern());
2434+
}
2435+
24252436
/**
24262437
* Compares the specified object with this {@code SimpleDateFormat} for equality.
24272438
* Returns true if the object is also a {@code SimpleDateFormat} and the
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2024, 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 8321545
27+
* @summary Ensure value returned by overridden toString method is as expected
28+
* @run junit ToStringTest
29+
*/
30+
31+
import java.text.ChoiceFormat;
32+
33+
import org.junit.jupiter.api.Test;
34+
35+
import static org.junit.jupiter.api.Assertions.assertEquals;
36+
37+
public class ToStringTest {
38+
39+
// Check a normal expected value. There is no null locale test as
40+
// ChoiceFormat is not localized.
41+
@Test
42+
public void normalValueTest() {
43+
String expectedStr = "ChoiceFormat [pattern: \"1.0#foo\"]\n";
44+
var c = new ChoiceFormat("1.0#foo");
45+
assertEquals(expectedStr, c.toString());
46+
}
47+
48+
// Ensure toString throws no exception when ChoiceFormat is created
49+
// with empty arrays
50+
@Test
51+
public void oddValueTest() {
52+
var c2 = new ChoiceFormat(new double[]{}, new String[]{});
53+
System.out.println(c2);
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2024, 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 8321545
27+
* @summary Ensure value returned by overridden toString method is as expected
28+
* @run junit ToStringTest
29+
*/
30+
31+
import java.text.CompactNumberFormat;
32+
import java.text.DecimalFormatSymbols;
33+
import java.text.NumberFormat;
34+
import java.util.Locale;
35+
36+
import org.junit.jupiter.api.Test;
37+
38+
import static org.junit.jupiter.api.Assertions.assertEquals;
39+
40+
public class ToStringTest {
41+
42+
// Check a normal expected value. There is no null test as the getInstance
43+
// methods and constructor (which takes DFS) throw NPE if the locale param is null.
44+
@Test
45+
public void expectedValueTest() {
46+
String expectedStr = "CompactNumberFormat [locale: \"English (Canada)\", decimal pattern: \"#,##0.###\", compact patterns: \"[, , , {one:0' 'thousand other:0' 'thousand}, {one:00' 'thousand other:00' 'thousand}, {one:000' 'thousand other:000' 'thousand}, {one:0' 'million other:0' 'million}, {one:00' 'million other:00' 'million}, {one:000' 'million other:000' 'million}, {one:0' 'billion other:0' 'billion}, {one:00' 'billion other:00' 'billion}, {one:000' 'billion other:000' 'billion}, {one:0' 'trillion other:0' 'trillion}, {one:00' 'trillion other:00' 'trillion}, {one:000' 'trillion other:000' 'trillion}]\"]\n";
47+
var c = NumberFormat.getCompactNumberInstance(Locale.CANADA, NumberFormat.Style.LONG);
48+
assertEquals(expectedStr, c.toString());
49+
}
50+
51+
// Check an odd value with empty decimal pattern and compact patterns.
52+
@Test
53+
public void oddValueTest() {
54+
String expectedStr = "CompactNumberFormat [locale: \"English (Canada)\", decimal pattern: \"\", compact patterns: \"[]\"]\n";
55+
var c = new CompactNumberFormat("", new DecimalFormatSymbols(Locale.CANADA), new String[]{});
56+
assertEquals(expectedStr, c.toString());
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2024, 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 8321545
27+
* @library /java/text/testlib
28+
* @summary Ensure value returned by overridden toString method is as expected
29+
* @run junit ToStringTest
30+
*/
31+
32+
import java.io.InputStream;
33+
import java.io.ObjectInputStream;
34+
import java.text.DateFormat;
35+
import java.text.DateFormatSymbols;
36+
import java.text.SimpleDateFormat;
37+
import java.util.Locale;
38+
39+
import org.junit.jupiter.api.Test;
40+
41+
import static org.junit.jupiter.api.Assertions.assertEquals;
42+
43+
public class ToStringTest {
44+
45+
// Check a normal expected value
46+
@Test
47+
public void normalValueTest() {
48+
String expectedStr =
49+
"SimpleDateFormat [locale: \"English (Canada)\", pattern: \"MMM d, y\"]\n";
50+
var s = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.CANADA);
51+
assertEquals(expectedStr, s.toString());
52+
}
53+
54+
// Check an odd value. SimpleDateFormat constructor that takes DFS, will use
55+
// the default locale, not the locale from the DFS.
56+
@Test
57+
public void oddValueTest() {
58+
String expectedStr =
59+
"SimpleDateFormat [locale: \"" + Locale.getDefault().getDisplayName() + "\", pattern: \"MMM d, y\"]\n";
60+
var s = new SimpleDateFormat("MMM d, y", new DateFormatSymbols(Locale.JAPAN));
61+
assertEquals(expectedStr, s.toString());
62+
}
63+
64+
65+
// Check the expected value when the locale is null. This is only possible
66+
// via an older SimpleDateFormat that was deserialized. The current constructor
67+
// will throw NPE if locale is null.
68+
@Test
69+
public void nullLocaleTest() {
70+
String expectedStr =
71+
"SimpleDateFormat [locale: null, pattern: \"yyyy.MM.dd E hh.mm.ss zzz\"]\n";
72+
// Borrowed from DateFormatSymbolsSerializationTest
73+
SimpleDateFormat s;
74+
try (InputStream is = HexDumpReader.getStreamFromHexDump("SDFserialized.ser.txt");
75+
ObjectInputStream iStream = new ObjectInputStream(is)) {
76+
s = (SimpleDateFormat)iStream.readObject();
77+
assertEquals(expectedStr, s.toString());
78+
} catch (Exception e) {
79+
System.out.println("Error building stream from deserialized simple date format");
80+
}
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2024, 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 8321545
27+
* @summary Ensure value returned by overridden toString method is as expected
28+
* @run junit ToStringTest
29+
*/
30+
31+
import java.text.DecimalFormat;
32+
import java.text.DecimalFormatSymbols;
33+
import java.text.NumberFormat;
34+
import java.util.Locale;
35+
36+
import org.junit.jupiter.api.Test;
37+
38+
import static org.junit.jupiter.api.Assertions.assertEquals;
39+
40+
public class ToStringTest {
41+
42+
// Check a normal expected value. There is no null locale test as
43+
// DecimalFormatSymbols will throw an exception when created with a null locale.
44+
@Test
45+
public void expectedValueTest() {
46+
String expectedStr =
47+
"DecimalFormat [locale: \"English (Canada)\", pattern: \"foo#00.00bar\"]\n";
48+
var d = new DecimalFormat("foo#00.00bar", new DecimalFormatSymbols(Locale.CANADA));
49+
assertEquals(expectedStr, d.toString());
50+
51+
String expectedStr2 =
52+
"DecimalFormat [locale: \"English (Canada)\", pattern: \"#,##0.###\"]\n";
53+
var d2 = NumberFormat.getInstance(Locale.CANADA);
54+
assertEquals(expectedStr2, d2.toString());
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2024, 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 8321545
27+
* @summary Ensure value returned by overridden toString method is as expected
28+
* @run junit ToStringTest
29+
*/
30+
31+
import java.text.ListFormat;
32+
import java.util.Locale;
33+
34+
import org.junit.jupiter.api.Test;
35+
36+
import static org.junit.jupiter.api.Assertions.assertEquals;
37+
38+
public class ToStringTest {
39+
40+
// Check a normal expected value. There is no null test as the getInstance
41+
// methods throw NPE if any arguments are null.
42+
@Test
43+
public void expectedValueTest() {
44+
String expectedStr =
45+
"ListFormat [locale: \"English (Canada)\", start: \"{0}, {1}\", middle: \"{0}, {1}\", end: \"{0} and {1}\", two: \"{0} and {1}\", three: \"{0}, {1} and {2}\"]\n";
46+
var l = ListFormat.getInstance(Locale.CANADA, ListFormat.Type.STANDARD, ListFormat.Style.FULL);
47+
assertEquals(expectedStr, l.toString());
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2024, 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 8321545
27+
* @summary Ensure value returned by overridden toString method is as expected
28+
* @run junit ToStringTest
29+
*/
30+
31+
import java.text.MessageFormat;
32+
import java.util.Locale;
33+
34+
import org.junit.jupiter.api.Test;
35+
36+
import static org.junit.jupiter.api.Assertions.assertEquals;
37+
38+
public class ToStringTest {
39+
40+
// Check a normal expected value
41+
@Test
42+
public void expectedValueTest() {
43+
String expectedStr =
44+
"MessageFormat [locale: \"English (Canada)\", pattern: \"foo {0}\"]\n";
45+
var m = new MessageFormat("foo {0}", Locale.CANADA);
46+
assertEquals(expectedStr, m.toString());
47+
}
48+
49+
// Check the expected value when the locale is null
50+
@Test
51+
public void nullLocaleTest() {
52+
String expectedStr =
53+
"MessageFormat [locale: null, pattern: \"foo {0}\"]\n";
54+
var m = new MessageFormat("foo {0}", null);
55+
assertEquals(expectedStr, m.toString());
56+
}
57+
}

0 commit comments

Comments
 (0)
Please sign in to comment.