-
Notifications
You must be signed in to change notification settings - Fork 530
8250802: Refactor StringConverter and its subclasses #1880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nlisker
wants to merge
8
commits into
openjdk:master
Choose a base branch
from
nlisker:8250802_Refactor_StringConverter_and_its_subclasses
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d6c3ce3
Parent classes
nlisker b88e385
Primitive-related converters
nlisker 96d6021
Format converter
nlisker 9ded0d7
Number and subclasses converters
nlisker 90aed8d
Date/Time converters
nlisker 2b610c6
LocalDate/Time converters
nlisker 4e8bb92
review comments 1
nlisker 3448438
review comments 2
nlisker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
modules/javafx.base/src/main/java/javafx/util/converter/BaseTemporalStringConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright (c) 2025, 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. Oracle designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Oracle in the LICENSE file that accompanied this code. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package javafx.util.converter; | ||
|
||
import java.time.chrono.Chronology; | ||
import java.time.chrono.IsoChronology; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeFormatterBuilder; | ||
import java.time.format.DecimalStyle; | ||
import java.time.format.FormatStyle; | ||
import java.time.temporal.Temporal; | ||
import java.time.temporal.TemporalQuery; | ||
import java.util.Locale; | ||
import java.util.Objects; | ||
|
||
/// Base class for the java.time types converters. | ||
abstract class BaseTemporalStringConverter<T extends Temporal> extends BaseStringConverter<T> { | ||
|
||
private static final Locale DEFAULT_LOCALE = Locale.getDefault(Locale.Category.FORMAT); | ||
private static final Chronology DEFAULT_CHRONO = IsoChronology.INSTANCE; | ||
|
||
private final DateTimeFormatter formatter; | ||
private final DateTimeFormatter parser; | ||
|
||
protected BaseTemporalStringConverter(FormatStyle dateStyle, FormatStyle timeStyle, Locale locale, Chronology chrono) { | ||
locale = Objects.requireNonNullElse(locale, DEFAULT_LOCALE); | ||
chrono = Objects.requireNonNullElse(chrono, DEFAULT_CHRONO); | ||
formatter = createFormatter(dateStyle, timeStyle, locale, chrono); | ||
parser = createParser(dateStyle, timeStyle, locale, chrono); | ||
} | ||
|
||
protected BaseTemporalStringConverter(DateTimeFormatter formatter, DateTimeFormatter parser, | ||
FormatStyle dateStyle, FormatStyle timeStyle) { | ||
this.formatter = formatter != null ? formatter : | ||
createFormatter(dateStyle, timeStyle, DEFAULT_LOCALE, DEFAULT_CHRONO); | ||
this.parser = parser != null ? parser : | ||
formatter != null ? formatter : | ||
createParser(dateStyle, timeStyle, DEFAULT_LOCALE, DEFAULT_CHRONO); | ||
} | ||
|
||
private final DateTimeFormatter createParser(FormatStyle dateStyle, FormatStyle timeStyle, Locale locale, Chronology chrono) { | ||
String pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(dateStyle, timeStyle, chrono, locale); | ||
return new DateTimeFormatterBuilder().parseLenient() | ||
.appendPattern(pattern) | ||
.toFormatter() | ||
.withChronology(chrono) | ||
.withDecimalStyle(DecimalStyle.of(locale)); | ||
} | ||
|
||
/// To satisfy the requirements of date formatters as described in, e.g., | ||
/// [LocalDateStringConverter#LocalDateStringConverter()], the method checks if there's a need to modify the pattern. | ||
private final DateTimeFormatter createFormatter(FormatStyle dateStyle, FormatStyle timeStyle, Locale locale, Chronology chrono) { | ||
if (dateStyle != null) { | ||
String pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(dateStyle, timeStyle, chrono, locale); | ||
if (pattern.contains("yy") && !pattern.contains("yyy")) { | ||
// Modify pattern to show four-digit year, including leading zeros. | ||
String newPattern = pattern.replace("yy", "yyyy"); | ||
return DateTimeFormatter.ofPattern(newPattern).withDecimalStyle(DecimalStyle.of(locale)); | ||
} | ||
} | ||
return getLocalizedFormatter(dateStyle, timeStyle) | ||
.withLocale(locale) | ||
.withChronology(chrono) | ||
.withDecimalStyle(DecimalStyle.of(locale)); | ||
} | ||
|
||
abstract DateTimeFormatter getLocalizedFormatter(FormatStyle dateStyle, FormatStyle timeStyle); | ||
|
||
@Override | ||
String toStringFromNonNull(T value) { | ||
return formatter.format(value); | ||
} | ||
|
||
@Override | ||
T fromNonEmptyString(String string) { | ||
return parser.parse(string, getTemporalQuery()); | ||
} | ||
|
||
abstract TemporalQuery<T> getTemporalQuery(); | ||
|
||
/// For tests only | ||
DateTimeFormatter getFormatter() { | ||
return formatter; | ||
} | ||
|
||
/// For tests only | ||
DateTimeFormatter getParser() { | ||
return parser; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parameter reassignment here. It all looks like fields, but only the last two are. Fields could be prefixed with
this.
to make this clearer, but IMHO parameter assignment is always bad.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IDEs can show fields and variables differently so it's very easy to discern. Perhaps it depends on your text editor settings. Adding "this" tends to be more noise than it's worth in my opinion. Can change if there's an agreement.