Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import org.apache.fesod.sheet.converters.longconverter.LongBooleanConverter;
import org.apache.fesod.sheet.converters.longconverter.LongNumberConverter;
import org.apache.fesod.sheet.converters.longconverter.LongStringConverter;
import org.apache.fesod.sheet.converters.monthday.MonthDayStringConverter;
import org.apache.fesod.sheet.converters.offsetdatetime.OffsetDateTimeDateConverter;
import org.apache.fesod.sheet.converters.offsetdatetime.OffsetDateTimeNumberConverter;
import org.apache.fesod.sheet.converters.offsetdatetime.OffsetDateTimeStringConverter;
Expand Down Expand Up @@ -137,6 +138,7 @@ private static void initAllConverter() {

putAllConverter(new LocalTimeNumberConverter());
putAllConverter(new LocalTimeStringConverter());
putAllConverter(new MonthDayStringConverter());
putAllConverter(new SqlTimeNumberConverter());
putAllConverter(new SqlTimeStringConverter());
putAllConverter(new TimestampNumberConverter());
Expand Down Expand Up @@ -189,6 +191,7 @@ private static void initDefaultWriteConverter() {
putWriteConverter(new LocalDateDateConverter());
putWriteConverter(new LocalTimeDateConverter());
putWriteConverter(new InstantStringConverter());
putWriteConverter(new MonthDayStringConverter());
putWriteConverter(new SqlTimeDateConverter());
putWriteConverter(new TimestampDateConverter());
putWriteConverter(new OffsetDateTimeDateConverter());
Expand Down Expand Up @@ -217,6 +220,7 @@ private static void initDefaultWriteConverter() {
putWriteStringConverter(new LocalDateTimeStringConverter());
putWriteStringConverter(new LocalTimeStringConverter());
putWriteStringConverter(new InstantStringConverter());
putWriteStringConverter(new MonthDayStringConverter());
putWriteStringConverter(new SqlTimeStringConverter());
putWriteStringConverter(new TimestampStringConverter());
putWriteStringConverter(new OffsetDateTimeStringConverter());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fesod.sheet.converters.monthday;

import java.time.MonthDay;
import org.apache.fesod.sheet.converters.Converter;
import org.apache.fesod.sheet.enums.CellDataTypeEnum;
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
import org.apache.fesod.sheet.metadata.data.ReadCellData;
import org.apache.fesod.sheet.metadata.data.WriteCellData;
import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
import org.apache.fesod.sheet.util.DateUtils;

/**
* MonthDay and string converter
*/
public class MonthDayStringConverter implements Converter<MonthDay> {

@Override
public Class<?> supportJavaTypeKey() {
return MonthDay.class;
}

@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}

@Override
public MonthDay convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return DateUtils.parseMonthDay(
cellData.getStringValue(), customFormat(contentProperty), globalConfiguration.getLocale());
}

@Override
public WriteCellData<?> convertToExcelData(
MonthDay value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return new WriteCellData<>(
DateUtils.format(value, customFormat(contentProperty), globalConfiguration.getLocale()));
}

private String customFormat(ExcelContentProperty contentProperty) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return null;
}
return contentProperty.getDateTimeFormatProperty().getFormat();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.MonthDay;
import java.time.OffsetDateTime;
import java.time.Year;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -91,6 +92,12 @@ public class DateUtils {

public static String defaultYearFormat = "yyyy";

/**
* The optional {@code --} prefix reads both the ISO form ({@code --10-31}) and the bare form ({@code 10-31}),
* so no lenient fallback pattern is needed when parsing.
*/
public static String defaultMonthDayFormat = "[--]MM-dd";

public static final String DEFAULT_LOCAL_TIME_FORMAT = TIME_FORMAT_8;

public static final int SECONDS_PER_MINUTE = 60;
Expand Down Expand Up @@ -164,6 +171,21 @@ public static LocalTime parseLocalTime(String timeString, String timeFormat, Loc
return LocalTime.parse(timeString, getCacheDateTimeFormat(timeFormat, local));
}

/**
* convert string to month day
*
* @param monthDayString month day string, e.g. --10-31
* @param monthDayFormat month day format, empty means ISO representation of a month-day
* @param local local
* @return
*/
public static MonthDay parseMonthDay(String monthDayString, String monthDayFormat, Locale local) {
if (StringUtils.isEmpty(monthDayFormat)) {
monthDayFormat = defaultMonthDayFormat;
}
return MonthDay.parse(monthDayString, getCacheDateTimeFormat(monthDayFormat, local));
}

/**
* convert string to date, requiring the string to carry an explicit offset
*
Expand Down Expand Up @@ -347,6 +369,24 @@ public static String format(LocalTime time, String timeFormat, Locale local) {
return time.format(getCacheDateTimeFormat(timeFormat, local));
}

/**
* Format month day
*
* @param monthDay MonthDay
* @param monthDayFormat month day format, empty means ISO representation of a month-day
* @param local local
* @return format string
*/
public static String format(MonthDay monthDay, String monthDayFormat, Locale local) {
if (monthDay == null) {
return null;
}
if (StringUtils.isEmpty(monthDayFormat)) {
monthDayFormat = defaultMonthDayFormat;
}
return monthDay.format(getCacheDateTimeFormat(monthDayFormat, local));
}

/**
* convert string to year
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fesod.sheet.converter;

import java.time.LocalDate;
import java.time.MonthDay;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.fesod.sheet.testkit.Tags;
import org.apache.fesod.sheet.testkit.base.AbstractExcelTest;
import org.apache.fesod.sheet.testkit.enums.ExcelFormat;
import org.apache.fesod.sheet.testkit.helpers.RoundTripHelper;
import org.apache.fesod.sheet.testkit.params.ExcelFormatSource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.params.ParameterizedTest;

/**
* Round-trip tests for {@link org.apache.fesod.sheet.converters.monthday.MonthDayStringConverter} over all Excel
* formats, covering the default ISO format, a user-configured {@code DateTimeFormat}, null values, boundary values and
* coexistence with other date converters.
*/
@Tag(Tags.ROUND_TRIP)
public class MonthDayConverterDataTest extends AbstractExcelTest {

@ParameterizedTest
@ExcelFormatSource
void readAndWriteMonthDay(ExcelFormat format) throws Exception {
MonthDayWriteData data = new MonthDayWriteData();
data.setBirthday(MonthDay.of(10, 31));

List<MonthDayWriteData> result = RoundTripHelper.writeAndRead(
createTempFile(format), MonthDayWriteData.class, Collections.singletonList(data));

Assertions.assertEquals(1, result.size());
Assertions.assertEquals(MonthDay.of(10, 31), result.get(0).getBirthday());
}

@ParameterizedTest
@ExcelFormatSource
void readAndWriteMonthDayWithDateTimeFormat(ExcelFormat format) throws Exception {
MonthDayWithFormatData data = new MonthDayWithFormatData();
data.setBirthday(MonthDay.of(2, 29));

List<MonthDayWithFormatData> result = RoundTripHelper.writeAndRead(
createTempFile(format), MonthDayWithFormatData.class, Collections.singletonList(data));

Assertions.assertEquals(1, result.size());
Assertions.assertEquals(MonthDay.of(2, 29), result.get(0).getBirthday());
}

@ParameterizedTest
@ExcelFormatSource
void readAndWriteNullMonthDay(ExcelFormat format) throws Exception {
MonthDayMixedData data = new MonthDayMixedData();
data.setDate(LocalDate.of(2024, 3, 15));

List<MonthDayMixedData> result = RoundTripHelper.writeAndRead(
createTempFile(format), MonthDayMixedData.class, Collections.singletonList(data));

Assertions.assertEquals(1, result.size());
Assertions.assertNull(result.get(0).getBirthday());
Assertions.assertEquals(LocalDate.of(2024, 3, 15), result.get(0).getDate());
}

@ParameterizedTest
@ExcelFormatSource
void readAndWriteBoundaryMonthDays(ExcelFormat format) throws Exception {
List<MonthDayWriteData> data = new ArrayList<>();
for (String value : new String[] {"--01-01", "--02-29", "--12-31"}) {
MonthDayWriteData row = new MonthDayWriteData();
row.setBirthday(MonthDay.parse(value));
data.add(row);
}

List<MonthDayWriteData> result =
RoundTripHelper.writeAndRead(createTempFile(format), MonthDayWriteData.class, data);

Assertions.assertEquals(3, result.size());
Assertions.assertEquals(MonthDay.of(1, 1), result.get(0).getBirthday());
Assertions.assertEquals(MonthDay.of(2, 29), result.get(1).getBirthday());
Assertions.assertEquals(MonthDay.of(12, 31), result.get(2).getBirthday());
}

@ParameterizedTest
@ExcelFormatSource
void readAndWriteMonthDayAlongsideLocalDate(ExcelFormat format) throws Exception {
MonthDayMixedData data = new MonthDayMixedData();
data.setBirthday(MonthDay.of(3, 15));
data.setDate(LocalDate.of(2024, 3, 15));

List<MonthDayMixedData> result = RoundTripHelper.writeAndRead(
createTempFile(format), MonthDayMixedData.class, Collections.singletonList(data));

Assertions.assertEquals(1, result.size());
Assertions.assertEquals(MonthDay.of(3, 15), result.get(0).getBirthday());
Assertions.assertEquals(LocalDate.of(2024, 3, 15), result.get(0).getDate());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fesod.sheet.converter;

import java.time.LocalDate;
import java.time.MonthDay;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.fesod.sheet.annotation.ExcelProperty;

/**
* Data model for testing that {@link MonthDay} conversion coexists with other date converters in the same row.
*/
@Getter
@Setter
@EqualsAndHashCode
public class MonthDayMixedData {

@ExcelProperty("Birthday")
private MonthDay birthday;

@ExcelProperty("Date")
private LocalDate date;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fesod.sheet.converter;

import java.time.MonthDay;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.fesod.sheet.annotation.ExcelProperty;
import org.apache.fesod.sheet.annotation.format.DateTimeFormat;

/**
* Data model for testing {@link MonthDay} conversion with a user-configured {@link DateTimeFormat}.
*/
@Getter
@Setter
@EqualsAndHashCode
public class MonthDayWithFormatData {

@ExcelProperty("Birthday")
@DateTimeFormat("MM/dd")
private MonthDay birthday;
}
Loading
Loading