Skip to content

feat: add Character and Year converters (#1017) - #1087

Open
BigDataDZ wants to merge 3 commits into
apache:mainfrom
BigDataDZ:feat/character-year-converters
Open

feat: add Character and Year converters (#1017)#1087
BigDataDZ wants to merge 3 commits into
apache:mainfrom
BigDataDZ:feat/character-year-converters

Conversation

@BigDataDZ

Copy link
Copy Markdown

Purpose of the pull request

Implements two commonly used JDK 8 compatible converters requested by the community task #1017:
java.lang.Character and java.time.Year. Proposal comments:
#1017 (comment) and the amendment below it
(UUID / YearMonth / Instant were proposed by other volunteers in parallel and are left to
them).

Related: #1017

What's changed?

  • CharacterStringConverter (org.apache.fesod.sheet.converters.charconverter): bidirectional
    Character <-> STRING. Reading an empty string yields null; strings longer than one character
    throw (wrapped into ExcelDataConvertException by the framework) instead of being silently
    truncated.
  • YearStringConverter (org.apache.fesod.sheet.converters.year): bidirectional Year <->
    STRING, default format uuuu, honors @DateTimeFormat custom patterns and
    GlobalConfiguration.locale.

Both follow the existing *StringConverter pattern (e.g. LocalTimeStringConverter) and are
registered in DefaultConverterLoader under putAllConverter, putWriteConverter and
putWriteStringConverter, so they work on read, xlsx write and CSV write alike.

Each converter ships with JUnit 5 unit tests (11 tests total: type keys, round-trips, default and
custom formats, and error cases).

Checklist

  • I have read the Contributor Guide.
  • I have written the necessary doc or comment.
  • I have added the necessary unit tests and all cases have passed.

Signed-off-by: BigDataDZ <76271875+BigDataDZ@users.noreply.github.com>
@Override
public WriteCellData<?> convertToExcelData(
Character value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return new WriteCellData<>(String.valueOf(value));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please consider directly using value.toString().

Comment on lines +60 to +67
private static DateTimeFormatter getFormatter(
ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return DEFAULT_FORMATTER;
}
return DateTimeFormatter.ofPattern(
contentProperty.getDateTimeFormatProperty().getFormat(), globalConfiguration.getLocale());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since the caching logic of DateTimeFormatter has already been implemented in DateUtils through DATE_TIME_FORMATTER_THREAD_LOCAL, can we integrate it into DateUtils (for example, DateUtils#parseYear, #format(Year...))?

*/
public class YearStringConverter implements Converter<Year> {

private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ofPattern("uuuu");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

While "uuuu" can output a minus sign when formatting, it enforces a fixed 4-character width during parsing and will throw an exception on signed years like "-45". As a result, its parsing behavior is practically no different from "yyyy".

If the intent is to support BCE/negative years and variable-length inputs, changing the pattern to "u" would be better. Otherwise, if negative years are not in scope, would it be cleaner to stick with the conventional "yyyy"?

Signed-off-by: BigDataDZ <76271875+BigDataDZ@users.noreply.github.com>
public static final String DATE_FORMAT_19_FORWARD_SLASH = "yyyy/MM/dd HH:mm:ss";
public static final String TIME_FORMAT_5 = "HH:mm";
public static final String TIME_FORMAT_8 = "HH:mm:ss";
public static final String DEFAULT_YEAR_FORMAT = "yyyy";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Recommended to follow the field naming convention.

Signed-off-by: BigDataDZ <76271875+BigDataDZ@users.noreply.github.com>
@BigDataDZ

Copy link
Copy Markdown
Author

Done in 254a11e — renamed to DateUtils.defaultYearFormat, following the sibling fields
defaultDateFormat / defaultLocalDateFormat, and parseYear / format(Year, ...) now fall
back to it when no explicit format is given, so it works as a global default just like the other
two. Tests re-run green (11/11 targeted, 927/927 full suite).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants