Skip to content

Commit 7903c2b

Browse files
committed
Rename TypedConverter to SimpleConverter, enforce non-null source
1 parent a57f5c9 commit 7903c2b

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/Converter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* instances may potentially be cached and called from different threads, they
2626
* should be thread-safe.
2727
*
28-
* <p>Extend {@link TypedConverter} if your implementation always converts
28+
* <p>Extend {@link SimpleConverter} if your implementation always converts
2929
* from a given source type into a given target type and does not need access to
3030
* the {@link ClassLoader} to perform the conversion.
3131
*
@@ -34,7 +34,7 @@
3434
*
3535
* @since 6.0
3636
* @see ConversionSupport
37-
* @see TypedConverter
37+
* @see SimpleConverter
3838
*/
3939
@API(status = EXPERIMENTAL, since = "6.0")
4040
public interface Converter<S, T extends @Nullable Object> {
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,53 +17,55 @@
1717
import org.junit.platform.commons.util.Preconditions;
1818

1919
/**
20-
* {@code TypedConversionService} is an abstract base class for
21-
* {@link Converter} implementations that always convert objects of a
22-
* given source type into a given target type.
20+
* {@code SimpleConverter} is an abstract base class for {@link Converter}
21+
* implementations that always convert objects of a given source type into a
22+
* given target type.
2323
*
2424
* @param <S> the type of the source argument to convert
2525
* @param <T> the type of the target object to create from the source
2626
* @since 6.0
2727
*/
2828
@API(status = EXPERIMENTAL, since = "6.0")
29-
public abstract class TypedConverter<S, T extends @Nullable Object> implements Converter<S, T> {
29+
public abstract class SimpleConverter<S, T extends @Nullable Object> implements Converter<S, T> {
3030

3131
private final Class<S> sourceType;
3232
private final Class<T> targetType;
3333

3434
/**
35-
* Create a new {@code TypedConversionService}.
35+
* Create a new {@code SimpleConverter}.
3636
*
3737
* @param sourceType the type of the argument to convert; never {@code null}
3838
* @param targetType the type of the target object to create from the source;
3939
* never {@code null}
4040
*/
41-
protected TypedConverter(Class<S> sourceType, Class<T> targetType) {
41+
protected SimpleConverter(Class<S> sourceType, Class<T> targetType) {
4242
this.sourceType = Preconditions.notNull(sourceType, "sourceType must not be null");
4343
this.targetType = Preconditions.notNull(targetType, "targetType must not be null");
4444
}
4545

4646
@Override
4747
public final boolean canConvert(ConversionContext context) {
48-
// FIXME TypeDescriptor.NONE handling?
49-
// FIXME add test cases with subtypes
50-
return this.sourceType == context.sourceType().getType() && this.targetType == context.targetType().getType();
48+
// FIXME adjust for subtypes
49+
return !context.sourceType().equals(TypeDescriptor.NONE) //
50+
&& this.sourceType == context.sourceType().getType() //
51+
&& this.targetType == context.targetType().getType();
5152
}
5253

5354
@Override
5455
public final T convert(@Nullable S source, ConversionContext context) {
56+
Preconditions.notNull(source, "source cannot be null");
5557
return convert(source);
5658
}
5759

5860
/**
5961
* Convert the supplied {@code source} object of type {@code S} into an object
6062
* of type {@code T}.
6163
*
62-
* @param source the source object to convert; may be {@code null}
64+
* @param source the source object to convert; never {@code null}
6365
* @return the converted object; may be {@code null} but only if the target
6466
* type is a reference type
6567
* @throws ConversionException if an error occurs during the conversion
6668
*/
67-
protected abstract T convert(@Nullable S source) throws ConversionException;
69+
protected abstract T convert(S source) throws ConversionException;
6870

6971
}

jupiter-tests/src/test/java/org/junit/jupiter/params/converter/LocaleConverter.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,18 @@
1212

1313
import java.util.Locale;
1414

15-
import org.jspecify.annotations.Nullable;
16-
import org.junit.platform.commons.support.conversion.TypedConverter;
15+
import org.junit.platform.commons.support.conversion.SimpleConverter;
1716

1817
// FIXME move to ConversionSupportIntegrationTests
19-
public class LocaleConverter extends TypedConverter<String, @Nullable Locale> {
18+
public class LocaleConverter extends SimpleConverter<String, Locale> {
2019

2120
public LocaleConverter() {
2221
super(String.class, Locale.class);
2322
}
2423

2524
@Override
26-
protected @Nullable Locale convert(@Nullable String source) {
27-
return source != null ? Locale.forLanguageTag(source) : null;
25+
protected Locale convert(String source) {
26+
return Locale.forLanguageTag(source);
2827
}
2928

3029
}

0 commit comments

Comments
 (0)