Skip to content

Commit

Permalink
Polish StringToObjectConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Dec 18, 2023
1 parent 62f9a67 commit 14114f5
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static <T> T convert(String source, Class<T> targetType, ClassLoader clas

Class<?> targetTypeToUse = toWrapperType(targetType);
Optional<StringToObjectConverter> converter = stringToObjectConverters.stream().filter(
candidate -> candidate.canConvert(targetTypeToUse)).findFirst();
candidate -> candidate.canConvertTo(targetTypeToUse)).findFirst();
if (converter.isPresent()) {
try {
ClassLoader classLoaderToUse = classLoader != null ? classLoader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class FallbackStringToObjectConverter implements StringToObjectConverter {
= new ConcurrentHashMap<>(64);

@Override
public boolean canConvert(Class<?> targetType) {
public boolean canConvertTo(Class<?> targetType) {
return findFactoryExecutable(targetType) != NULL_EXECUTABLE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class StringToBooleanConverter implements StringToObjectConverter {

@Override
public boolean canConvert(Class<?> targetType) {
public boolean canConvertTo(Class<?> targetType) {
return targetType == Boolean.class;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class StringToCharacterConverter implements StringToObjectConverter {

@Override
public boolean canConvert(Class<?> targetType) {
public boolean canConvertTo(Class<?> targetType) {
return targetType == Character.class;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class StringToClassConverter implements StringToObjectConverter {

@Override
public boolean canConvert(Class<?> targetType) {
public boolean canConvertTo(Class<?> targetType) {
return targetType == Class.class;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class StringToCommonJavaTypesConverter implements StringToObjectConverter {
}

@Override
public boolean canConvert(Class<?> targetType) {
public boolean canConvertTo(Class<?> targetType) {
return CONVERTERS.containsKey(targetType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class StringToEnumConverter implements StringToObjectConverter {

@Override
public boolean canConvert(Class<?> targetType) {
public boolean canConvertTo(Class<?> targetType) {
return targetType.isEnum();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class StringToJavaTimeConverter implements StringToObjectConverter {
}

@Override
public boolean canConvert(Class<?> targetType) {
public boolean canConvertTo(Class<?> targetType) {
return CONVERTERS.containsKey(targetType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class StringToNumberConverter implements StringToObjectConverter {
}

@Override
public boolean canConvert(Class<?> targetType) {
public boolean canConvertTo(Class<?> targetType) {
return CONVERTERS.containsKey(targetType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ interface StringToObjectConverter {
* supplied target type (which is guaranteed to be a wrapper type for
* primitives &mdash; for example, {@link Integer} instead of {@code int}).
*/
boolean canConvert(Class<?> targetType);
boolean canConvertTo(Class<?> targetType);

/**
* Convert the supplied {@link String} to the supplied target type (which is
* guaranteed to be a wrapper type for primitives &mdash; for example,
* {@link Integer} instead of {@code int}).
*
* <p>This method will only be invoked in {@link #canConvertTo(Class)}
* returned {@code true} for the same target type.
*/
Object convert(String source, Class<?> targetType) throws Exception;

Expand All @@ -35,6 +38,9 @@ interface StringToObjectConverter {
* guaranteed to be a wrapper type for primitives &mdash; for example,
* {@link Integer} instead of {@code int}).
*
* <p>This method will only be invoked in {@link #canConvertTo(Class)}
* returned {@code true} for the same target type.
*
* <p>The default implementation simply delegates to {@link #convert(String, Class)}.
* Can be overridden by concrete implementations of this interface that need
* access to the supplied {@link ClassLoader}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ void convertsStringToNewspaperViaConstructorIgnoringMultipleFactoryMethods() thr
@Test
@DisplayName("Cannot convert String to Diary because Diary has neither a static factory method nor a factory constructor")
void cannotConvertStringToDiary() {
assertThat(converter.canConvert(Diary.class)).isFalse();
assertThat(converter.canConvertTo(Diary.class)).isFalse();
}

@Test
@DisplayName("Cannot convert String to Magazine because Magazine has multiple static factory methods")
void cannotConvertStringToMagazine() {
assertThat(converter.canConvert(Magazine.class)).isFalse();
assertThat(converter.canConvertTo(Magazine.class)).isFalse();
}

// -------------------------------------------------------------------------
Expand All @@ -120,7 +120,7 @@ private static Method magazineMethod(String methodName) {
}

private static void assertConverts(String input, Class<?> targetType, Object expectedOutput) throws Exception {
assertThat(converter.canConvert(targetType)).isTrue();
assertThat(converter.canConvertTo(targetType)).isTrue();

var result = converter.convert(input, targetType);

Expand Down

0 comments on commit 14114f5

Please sign in to comment.