Skip to content

feat: improve datetime and enum converter #49

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
wants to merge 1 commit into
base: master
Choose a base branch
from
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 @@ -30,6 +30,7 @@
import java.nio.file.Path;
import java.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
Expand Down Expand Up @@ -68,6 +69,7 @@
import org.apache.commons.beanutils2.converters.FileConverter;
import org.apache.commons.beanutils2.converters.FloatConverter;
import org.apache.commons.beanutils2.converters.InetAddressConverter;
import org.apache.commons.beanutils2.converters.InstantConverter;
import org.apache.commons.beanutils2.converters.IntegerConverter;
import org.apache.commons.beanutils2.converters.LocalDateConverter;
import org.apache.commons.beanutils2.converters.LocalDateTimeConverter;
Expand Down Expand Up @@ -144,6 +146,7 @@
* <li>java.sql.Date (no default value)</li>
* <li>java.sql.Time (no default value)</li>
* <li>java.sql.Timestamp (no default value)</li>
* <li>java.time.Instant (no default value)</li>
* <li>java.time.LocalDate (no default value)</li>
* <li>java.time.LocalDateTime (no default value)</li>
* <li>java.time.LocalTime (no default value)</li>
Expand Down Expand Up @@ -500,6 +503,7 @@ private void registerArrays(final boolean throwException, final int defaultArray
registerArrayConverter(Dimension.class, new DimensionConverter(), throwException, defaultArraySize);
registerArrayConverter(File.class, new FileConverter(), throwException, defaultArraySize);
registerArrayConverter(InetAddress.class, new InetAddressConverter(), throwException, defaultArraySize);
registerArrayConverter(Instant.class, new InstantConverter(), throwException, defaultArraySize);
registerArrayConverter(Path.class, new PathConverter(), throwException, defaultArraySize);
registerArrayConverter(java.sql.Date.class, new SqlDateConverter(), throwException, defaultArraySize);
registerArrayConverter(java.sql.Time.class, new SqlTimeConverter(), throwException, defaultArraySize);
Expand Down Expand Up @@ -536,6 +540,7 @@ private void registerArrays(final boolean throwException, final int defaultArray
* <li>{@code java.util.Date.class} - {@link DateConverter}</li>
* <li>{@code java.util.Calendar.class} - {@link CalendarConverter}</li>
* <li>{@code File.class} - {@link FileConverter}</li>
* <li>{@code Instant.class} - {@link InstantConverter}</li>
* <li>{@code Path.class} - {@link PathConverter}</li>
* <li>{@code java.sql.Date.class} - {@link SqlDateConverter}</li>
* <li>{@code java.sql.Time.class} - {@link SqlTimeConverter}</li>
Expand Down Expand Up @@ -571,6 +576,7 @@ private void registerOther(final boolean throwException) {
register(Calendar.class, throwException ? new CalendarConverter() : new CalendarConverter(null));
register(File.class, throwException ? new FileConverter() : new FileConverter(null));
register(InetAddress.class, throwException ? new InetAddressConverter() : new InetAddressConverter(null));
register(Instant.class, throwException ? new InstantConverter() : new InstantConverter(null));
register(Path.class, throwException ? new PathConverter() : new PathConverter(null));
register(java.sql.Date.class, throwException ? new SqlDateConverter() : new SqlDateConverter(null));
register(java.sql.Time.class, throwException ? new SqlTimeConverter() : new SqlTimeConverter(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ protected String convertToString(final Object value) {
@Override
protected <T> T convertToType(final Class<T> type, final Object value) throws Exception {
if (Character.class.equals(type) || Character.TYPE.equals(type)) {
return type.cast(Character.valueOf(value.toString().charAt(0)));
final String stringValue = toString(value);

if (stringValue.isEmpty()) {
throw new IllegalArgumentException("Value must not be empty");
}

return type.cast(Character.valueOf(stringValue.charAt(0)));
}

throw conversionException(type, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -135,6 +136,8 @@ protected String convertToString(final Object value) {
} else if (value instanceof TemporalAccessor) {
// Backstop for other TemporalAccessor implementations.
date = Date.from(Instant.from((TemporalAccessor) value));
} else if (value instanceof Instant) {
date = Date.from((Instant) value);
}

String result = null;
Expand Down Expand Up @@ -169,6 +172,7 @@ protected String convertToString(final Object value) {
* <li>{@link java.time.LocalDate}</li>
* <li>{@link java.time.LocalDateTime}</li>
* <li>{@link java.time.OffsetDateTime}</li>
* <li>{@link java.time.Instant}</li>
* <li>{@link java.time.ZonedDateTime}</li>
* <li>{@link java.sql.Date}</li>
* <li>{@link java.sql.Time}</li>
Expand Down Expand Up @@ -247,6 +251,11 @@ protected <T> T convertToType(final Class<T> targetType, final Object value) thr
return toDate(targetType, date.toInstant().toEpochMilli());
}

if (value instanceof Instant) {
final Instant date = (Instant) value;
return toDate(targetType, date.toEpochMilli());
}

// Convert all other types to String & handle
final String stringValue = toTrim(value);
if (stringValue.isEmpty()) {
Expand Down Expand Up @@ -555,6 +564,10 @@ private <T> T toDate(final Class<T> type, final long value) {
return type.cast(offsetDateTime);
}

if (type.equals(Instant.class)) {
return type.cast(Instant.ofEpochMilli(value));
}

// java.util.Calendar
if (type.equals(Calendar.class)) {
Calendar calendar = null;
Expand Down Expand Up @@ -587,6 +600,7 @@ private <T> T toDate(final Class<T> type, final long value) {
* <li>{@link java.sql.Date}</li>
* <li>{@link java.sql.Time}</li>
* <li>{@link java.sql.Timestamp}</li>
* <li>{@link java.time.Instant}</li>
* </ul>
* <p>
* <strong>N.B.</strong> No default String conversion mechanism is provided for {@link java.util.Date} and {@link java.util.Calendar} type.
Expand Down Expand Up @@ -624,6 +638,14 @@ private <T> T toDate(final Class<T> type, final String value) {
}
}

if (type.equals(Instant.class)) {
try {
return type.cast(Instant.parse(value));
} catch (final DateTimeParseException ex) {
throw new ConversionException("String must be in ISO-8601 format to create a java.time.Instant");
}
}

final String msg = toString(getClass()) + " does not support default String to '" + toString(type) + "' conversion.";
if (log().isWarnEnabled()) {
log().warn(" " + msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public DurationConverter(final Duration defaultValue) {
@Override
protected <T> T convertToType(final Class<T> type, final Object value) throws Throwable {
if (Duration.class.equals(type)) {
return type.cast(Duration.parse(String.valueOf(value)));
return type.cast(Duration.parse(toString(value)));
}

throw conversionException(type, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package org.apache.commons.beanutils2.converters;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* {@link org.apache.commons.beanutils2.Converter} implementation that handles conversion to and from <strong>java.lang.Enum</strong> objects.
* <p>
Expand All @@ -28,6 +31,11 @@
*/
public final class EnumConverter<E extends Enum<E>> extends AbstractConverter<Enum<E>> {

/** Matches if a given input is an enum string. */
private static final Pattern ENUM_PATTERN = Pattern.compile(
"((?:[a-z\\d.]+)*)\\.([A-Za-z\\d]+)[#.]([A-Z\\d_]+)"
);

/**
* Constructs a <strong>java.lang.Enum</strong> <em>Converter</em> that throws a {@code ConversionException} if an error occurs.
*/
Expand Down Expand Up @@ -59,15 +67,37 @@ public EnumConverter(final Enum<E> defaultValue) {
@Override
protected <R> R convertToType(final Class<R> type, final Object value) throws Throwable {
if (Enum.class.isAssignableFrom(type)) {
final String enumValue = String.valueOf(value);
final R[] constants = type.getEnumConstants();
if (constants == null) {
throw conversionException(type, value);
final String stringValue = toString(value);

try {
return type.cast((Enum) Enum.valueOf((Class) type, stringValue));
} catch (IllegalArgumentException ex) {
// Continue to check fully qualified name.
}

Matcher matcher = ENUM_PATTERN.matcher(stringValue);

if (!matcher.matches()) {
throw new IllegalArgumentException(
"Value doesn't follow Enum naming convention, expecting value like: java.time.DayOfWeek.MONDAY");
}
for (final R candidate : constants) {
if (((Enum) candidate).name().equalsIgnoreCase(enumValue)) {
return candidate;

String className = matcher.group(1) + "." + matcher.group(2);

try {
Class classForName = Class.forName(className);

if (!classForName.isEnum()) {
throw new IllegalArgumentException("Value isn't an enumerated type.");
}

if (!type.isAssignableFrom(classForName)) {
throw new IllegalArgumentException("Class is not the required type.");
}

return type.cast((Enum) Enum.valueOf(classForName, matcher.group(3)));
} catch (ClassNotFoundException ex) {
throw new IllegalArgumentException("Class \"" + className + "\" doesn't exist.", ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.commons.beanutils2.converters;

import java.time.Instant;

/**
* {@link org.apache.commons.beanutils2.Converter} implementation that handles conversion to and from {@link Instant} objects.
* <p>
* Can be configured to either return a <i>default value</i> or throw a {@code ConversionException} if a conversion error occurs.
* </p>
*
* @since 2.0
* @see Instant
*/
public final class InstantConverter extends DateTimeConverter<Instant> {

/**
* Constructs a {@link Instant} <i>Converter</i> that throws a {@code ConversionException} if an error occurs.
*/
public InstantConverter() {
super();
}

/**
* Constructs a {@link Instant} <i>Converter</i> that returns a default value if an error occurs.
*
* @param defaultValue The default value to be returned if the value to be converted is missing or an error occurs converting the value.
*/
public InstantConverter(final Instant defaultValue) {
super(defaultValue);
}

/**
* Gets the default type this {@code Converter} handles.
*
* @return Default type this {@code Converter} handles.
*/
@Override
protected Class<Instant> getDefaultType() {
return Instant.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public PeriodConverter(final Period defaultValue) {
@Override
protected <T> T convertToType(final Class<T> type, final Object value) throws Throwable {
if (Period.class.equals(type)) {
return type.cast(Period.parse(String.valueOf(value)));
return type.cast(Period.parse(toString(value)));
}

throw conversionException(type, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,23 @@
*/
public class CharacterConverterTest {

/** Sets Up */
private Converter<Character> converter;

@BeforeEach
public void setUp() throws Exception {
converter = new CharacterConverter();
}

/** Tear Down */
@AfterEach
public void tearDown() throws Exception {
converter = null;
}

/**
* Tests whether the primitive char class can be passed as target type.
*/
@Test
public void testConvertToChar() {
final Converter<Character> converter = new CharacterConverter();
assertEquals(Character.valueOf('F'), converter.convert(Character.TYPE, "FOO"), "Wrong result");
}

Expand All @@ -54,7 +55,6 @@ public void testConvertToChar() {
*/
@Test
public void testConvertToCharacter() {
final Converter<Character> converter = new CharacterConverter();
assertEquals(Character.valueOf('N'), converter.convert(Character.class, Character.valueOf('N')), "Character Test");
assertEquals(Character.valueOf('F'), converter.convert(Character.class, "FOO"), "String Test");
assertEquals(Character.valueOf('3'), converter.convert(Character.class, Integer.valueOf(321)), "Integer Test");
Expand All @@ -65,7 +65,6 @@ public void testConvertToCharacter() {
*/
@Test
public void testConvertToCharacterNullNoDefault() {
final Converter<Character> converter = new CharacterConverter();
assertThrows(ConversionException.class, () -> converter.convert(Character.class, null));
}

Expand All @@ -75,8 +74,6 @@ public void testConvertToCharacterNullNoDefault() {
@Test
@SuppressWarnings("unchecked") // testing raw conversion
public void testConvertToString() {

final Converter<Character> converter = new CharacterConverter();
@SuppressWarnings("rawtypes")
final Converter raw = converter;

Expand All @@ -90,10 +87,7 @@ public void testConvertToString() {
* Tries a conversion to an unsupported type.
*/
@Test
@SuppressWarnings("unchecked") // tests failure so allow mismatch
public void testConvertToUnsupportedType() {
@SuppressWarnings("rawtypes") // tests failure so allow mismatch
final Converter converter = new CharacterConverter();
assertThrows(ConversionException.class, () -> converter.convert(Integer.class, "Test"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.time.DayOfWeek;
import java.util.concurrent.TimeUnit;

import org.apache.commons.beanutils2.ConversionException;
import org.apache.commons.beanutils2.Converter;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -79,4 +82,38 @@ public void testSimpleConversion() throws Exception {
public void testUnsupportedType() {
assertThrows(ConversionException.class, () -> converter.convert(Integer.class, "http://www.apache.org"));
}

@Test
public void testConvertTimeUnit() {
final TimeUnit expected = TimeUnit.NANOSECONDS;
final Enum actual = converter.convert(Enum.class, "java.util.concurrent.TimeUnit.NANOSECONDS");
assertEquals(expected, actual);
}

@Test
public void testConvertDayOfWeek() {
final DayOfWeek expected = DayOfWeek.MONDAY;
final DayOfWeek actual = converter.convert(DayOfWeek.class, "java.time.DayOfWeek#MONDAY");
assertEquals(expected, actual);
}

@Test
public void testConvertMismatchingEnumType() {
assertThrows(ConversionException.class, () -> converter.convert(TimeUnit.class, "java.time.DayOfWeek#MONDAY"));
}

@Test
public void testBrokenNamingConvention() {
assertThrows(ConversionException.class, () -> converter.convert(Enum.class, "JAVA-TIME-DAYOFWEEK#MONDAY"));
}

@Test
public void testNonEnumClasses() {
assertThrows(ConversionException.class, () -> converter.convert(Enum.class, "java.lang.String#MONDAY"));
}

@Test
public void testNonExistingClasses() {
assertThrows(ConversionException.class, () -> converter.convert(Enum.class, "java.lang.does.not.exist#MONDAY"));
}
}
Loading