Skip to content

Commit

Permalink
8321545: Override toString() for Format subclasses
Browse files Browse the repository at this point in the history
Reviewed-by: naoto, rriggs
  • Loading branch information
Justin Lu committed Jan 23, 2024
1 parent edfee7f commit 96607df
Show file tree
Hide file tree
Showing 12 changed files with 419 additions and 7 deletions.
13 changes: 12 additions & 1 deletion src/java.base/share/classes/java/text/ChoiceFormat.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -614,6 +614,17 @@ public int hashCode() {
return result;
}

/**
* {@return a string identifying this {@code ChoiceFormat}, for debugging}
*/
@Override
public String toString() {
return
"""
ChoiceFormat [pattern: "%s"]
""".formatted(toPattern());
}

/**
* Compares the specified object with this {@code ChoiceFormat} for equality.
* Returns true if the object is also a {@code ChoiceFormat} and the
Expand Down
13 changes: 12 additions & 1 deletion src/java.base/share/classes/java/text/CompactNumberFormat.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -2391,6 +2391,17 @@ public int hashCode() {
+ Boolean.hashCode(parseBigDecimal);
}

/**
* {@return a string identifying this {@code CompactNumberFormat}, for debugging}
*/
@Override
public String toString() {
return
"""
CompactNumberFormat [locale: "%s", decimal pattern: "%s", compact patterns: "%s"]
""".formatted(symbols.getLocale().getDisplayName(), decimalPattern, Arrays.toString(compactPatterns));
}

/**
* Creates and returns a copy of this {@code CompactNumberFormat}
* instance.
Expand Down
13 changes: 12 additions & 1 deletion src/java.base/share/classes/java/text/DecimalFormat.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -2996,6 +2996,17 @@ public int hashCode() {
// just enough fields for a reasonable distribution
}

/**
* {@return a string identifying this {@code DecimalFormat}, for debugging}
*/
@Override
public String toString() {
return
"""
DecimalFormat [locale: "%s", pattern: "%s"]
""".formatted(symbols.getLocale().getDisplayName(), toPattern());
}

/**
* Synthesizes a pattern string that represents the current state
* of this Format object.
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/text/ListFormat.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -530,7 +530,7 @@ public int hashCode() {
}

/**
* {@inheritDoc}
* {@return a string identifying this {@code ListFormat}, for debugging}
*/
@Override
public String toString() {
Expand Down
13 changes: 12 additions & 1 deletion src/java.base/share/classes/java/text/MessageFormat.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -1184,6 +1184,17 @@ public int hashCode() {
return pattern.hashCode(); // enough for reasonable distribution
}

/**
* {@return a string identifying this {@code MessageFormat}, for debugging}
*/
@Override
public String toString() {
return
"""
MessageFormat [locale: %s, pattern: "%s"]
""".formatted(locale == null ? null : '"' + locale.getDisplayName() + '"', toPattern());
}


/**
* Defines constants that are used as attribute keys in the
Expand Down
13 changes: 12 additions & 1 deletion src/java.base/share/classes/java/text/SimpleDateFormat.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -2422,6 +2422,17 @@ public int hashCode()
// just enough fields for a reasonable distribution
}

/**
* {@return a string identifying this {@code SimpleDateFormat}, for debugging}
*/
@Override
public String toString() {
return
"""
SimpleDateFormat [locale: %s, pattern: "%s"]
""".formatted(locale == null ? null : '"' + locale.getDisplayName() + '"', toPattern());
}

/**
* Compares the specified object with this {@code SimpleDateFormat} for equality.
* Returns true if the object is also a {@code SimpleDateFormat} and the
Expand Down
55 changes: 55 additions & 0 deletions test/jdk/java/text/Format/ChoiceFormat/ToStringTest.java
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8321545
* @summary Ensure value returned by overridden toString method is as expected
* @run junit ToStringTest
*/

import java.text.ChoiceFormat;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ToStringTest {

// Check a normal expected value. There is no null locale test as
// ChoiceFormat is not localized.
@Test
public void normalValueTest() {
String expectedStr = "ChoiceFormat [pattern: \"1.0#foo\"]\n";
var c = new ChoiceFormat("1.0#foo");
assertEquals(expectedStr, c.toString());
}

// Ensure toString throws no exception when ChoiceFormat is created
// with empty arrays
@Test
public void oddValueTest() {
var c2 = new ChoiceFormat(new double[]{}, new String[]{});
System.out.println(c2);
}
}
58 changes: 58 additions & 0 deletions test/jdk/java/text/Format/CompactNumberFormat/ToStringTest.java
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8321545
* @summary Ensure value returned by overridden toString method is as expected
* @run junit ToStringTest
*/

import java.text.CompactNumberFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ToStringTest {

// Check a normal expected value. There is no null test as the getInstance
// methods and constructor (which takes DFS) throw NPE if the locale param is null.
@Test
public void expectedValueTest() {
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";
var c = NumberFormat.getCompactNumberInstance(Locale.CANADA, NumberFormat.Style.LONG);
assertEquals(expectedStr, c.toString());
}

// Check an odd value with empty decimal pattern and compact patterns.
@Test
public void oddValueTest() {
String expectedStr = "CompactNumberFormat [locale: \"English (Canada)\", decimal pattern: \"\", compact patterns: \"[]\"]\n";
var c = new CompactNumberFormat("", new DecimalFormatSymbols(Locale.CANADA), new String[]{});
assertEquals(expectedStr, c.toString());
}
}
82 changes: 82 additions & 0 deletions test/jdk/java/text/Format/DateFormat/ToStringTest.java
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8321545
* @library /java/text/testlib
* @summary Ensure value returned by overridden toString method is as expected
* @run junit ToStringTest
*/

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Locale;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ToStringTest {

// Check a normal expected value
@Test
public void normalValueTest() {
String expectedStr =
"SimpleDateFormat [locale: \"English (Canada)\", pattern: \"MMM d, y\"]\n";
var s = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.CANADA);
assertEquals(expectedStr, s.toString());
}

// Check an odd value. SimpleDateFormat constructor that takes DFS, will use
// the default locale, not the locale from the DFS.
@Test
public void oddValueTest() {
String expectedStr =
"SimpleDateFormat [locale: \"" + Locale.getDefault().getDisplayName() + "\", pattern: \"MMM d, y\"]\n";
var s = new SimpleDateFormat("MMM d, y", new DateFormatSymbols(Locale.JAPAN));
assertEquals(expectedStr, s.toString());
}


// Check the expected value when the locale is null. This is only possible
// via an older SimpleDateFormat that was deserialized. The current constructor
// will throw NPE if locale is null.
@Test
public void nullLocaleTest() {
String expectedStr =
"SimpleDateFormat [locale: null, pattern: \"yyyy.MM.dd E hh.mm.ss zzz\"]\n";
// Borrowed from DateFormatSymbolsSerializationTest
SimpleDateFormat s;
try (InputStream is = HexDumpReader.getStreamFromHexDump("SDFserialized.ser.txt");
ObjectInputStream iStream = new ObjectInputStream(is)) {
s = (SimpleDateFormat)iStream.readObject();
assertEquals(expectedStr, s.toString());
} catch (Exception e) {
System.out.println("Error building stream from deserialized simple date format");
}
}
}
56 changes: 56 additions & 0 deletions test/jdk/java/text/Format/DecimalFormat/ToStringTest.java
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8321545
* @summary Ensure value returned by overridden toString method is as expected
* @run junit ToStringTest
*/

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ToStringTest {

// Check a normal expected value. There is no null locale test as
// DecimalFormatSymbols will throw an exception when created with a null locale.
@Test
public void expectedValueTest() {
String expectedStr =
"DecimalFormat [locale: \"English (Canada)\", pattern: \"foo#00.00bar\"]\n";
var d = new DecimalFormat("foo#00.00bar", new DecimalFormatSymbols(Locale.CANADA));
assertEquals(expectedStr, d.toString());

String expectedStr2 =
"DecimalFormat [locale: \"English (Canada)\", pattern: \"#,##0.###\"]\n";
var d2 = NumberFormat.getInstance(Locale.CANADA);
assertEquals(expectedStr2, d2.toString());
}
}

1 comment on commit 96607df

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.