@@ -281,7 +281,7 @@ public abstract class Conventions {
|
||||
|
||||
/**
|
||||
* Retrieves the {@code Class} of an element in the {@code Collection}.
|
||||
* The exact element for which the {@code Class} is retreived will depend
|
||||
* The exact element for which the {@code Class} is retrieved will depend
|
||||
* on the concrete {@code Collection} implementation.
|
||||
*/
|
||||
private static <E> E peekAhead(Collection<E> collection) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -36,10 +36,12 @@ final class CollectionToStringConverter implements ConditionalGenericConverter {
|
||||
|
||||
private final ConversionService conversionService;
|
||||
|
||||
|
||||
public CollectionToStringConverter(ConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Set<ConvertiblePair> getConvertibleTypes() {
|
||||
return Collections.singleton(new ConvertiblePair(Collection.class, String.class));
|
||||
@@ -47,7 +49,8 @@ final class CollectionToStringConverter implements ConditionalGenericConverter {
|
||||
|
||||
@Override
|
||||
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
return ConversionUtils.canConvertElements(sourceType.getElementTypeDescriptor(), targetType, this.conversionService);
|
||||
return ConversionUtils.canConvertElements(
|
||||
sourceType.getElementTypeDescriptor(), targetType, this.conversionService);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -56,7 +59,7 @@ final class CollectionToStringConverter implements ConditionalGenericConverter {
|
||||
return null;
|
||||
}
|
||||
Collection<?> sourceCollection = (Collection<?>) source;
|
||||
if (sourceCollection.size() == 0) {
|
||||
if (sourceCollection.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
@@ -65,7 +68,8 @@ final class CollectionToStringConverter implements ConditionalGenericConverter {
|
||||
if (i > 0) {
|
||||
sb.append(DELIMITER);
|
||||
}
|
||||
Object targetElement = this.conversionService.convert(sourceElement, sourceType.elementTypeDescriptor(sourceElement), targetType);
|
||||
Object targetElement = this.conversionService.convert(
|
||||
sourceElement, sourceType.elementTypeDescriptor(sourceElement), targetType);
|
||||
sb.append(targetElement);
|
||||
i++;
|
||||
}
|
||||
|
||||
@@ -138,14 +138,14 @@ public class GenericConversionService implements ConfigurableConversionService {
|
||||
|
||||
@Override
|
||||
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
|
||||
Assert.notNull(targetType, "targetType to convert to cannot be null");
|
||||
Assert.notNull(targetType, "Target type to convert to cannot be null");
|
||||
return canConvert((sourceType != null ? TypeDescriptor.valueOf(sourceType) : null),
|
||||
TypeDescriptor.valueOf(targetType));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
Assert.notNull(targetType, "targetType to convert to cannot be null");
|
||||
Assert.notNull(targetType, "Target type to convert to cannot be null");
|
||||
if (sourceType == null) {
|
||||
return true;
|
||||
}
|
||||
@@ -154,9 +154,9 @@ public class GenericConversionService implements ConfigurableConversionService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether conversion between the sourceType and targetType can be bypassed.
|
||||
* Return whether conversion between the source type and the target type can be bypassed.
|
||||
* <p>More precisely, this method will return true if objects of sourceType can be
|
||||
* converted to the targetType by returning the source object unchanged.
|
||||
* converted to the target type by returning the source object unchanged.
|
||||
* @param sourceType context about the source type to convert from
|
||||
* (may be {@code null} if source is {@code null})
|
||||
* @param targetType context about the target type to convert to (required)
|
||||
@@ -165,7 +165,7 @@ public class GenericConversionService implements ConfigurableConversionService {
|
||||
* @since 3.2
|
||||
*/
|
||||
public boolean canBypassConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
Assert.notNull(targetType, "targetType to convert to cannot be null");
|
||||
Assert.notNull(targetType, "Target type to convert to cannot be null");
|
||||
if (sourceType == null) {
|
||||
return true;
|
||||
}
|
||||
@@ -176,20 +176,20 @@ public class GenericConversionService implements ConfigurableConversionService {
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T convert(Object source, Class<T> targetType) {
|
||||
Assert.notNull(targetType, "targetType to convert to cannot be null");
|
||||
Assert.notNull(targetType, "Target type to convert to cannot be null");
|
||||
return (T) convert(source, TypeDescriptor.forObject(source), TypeDescriptor.valueOf(targetType));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
Assert.notNull(targetType, "targetType to convert to cannot be null");
|
||||
Assert.notNull(targetType, "Target type to convert to cannot be null");
|
||||
if (sourceType == null) {
|
||||
Assert.isTrue(source == null, "source must be [null] if sourceType == [null]");
|
||||
Assert.isTrue(source == null, "Source must be [null] if source type == [null]");
|
||||
return handleResult(null, targetType, convertNullSource(null, targetType));
|
||||
}
|
||||
if (source != null && !sourceType.getObjectType().isInstance(source)) {
|
||||
throw new IllegalArgumentException("source to convert from must be an instance of " +
|
||||
sourceType + "; instead it was a " + source.getClass().getName());
|
||||
throw new IllegalArgumentException("Source to convert from must be an instance of [" +
|
||||
sourceType + "]; instead it was a [" + source.getClass().getName() + "]");
|
||||
}
|
||||
GenericConverter converter = getConverter(sourceType, targetType);
|
||||
if (converter != null) {
|
||||
@@ -201,9 +201,9 @@ public class GenericConversionService implements ConfigurableConversionService {
|
||||
|
||||
/**
|
||||
* Convenience operation for converting a source object to the specified targetType,
|
||||
* where the targetType is a descriptor that provides additional conversion context.
|
||||
* where the target type is a descriptor that provides additional conversion context.
|
||||
* Simply delegates to {@link #convert(Object, TypeDescriptor, TypeDescriptor)} and
|
||||
* encapsulates the construction of the sourceType descriptor using
|
||||
* encapsulates the construction of the source type descriptor using
|
||||
* {@link TypeDescriptor#forObject(Object)}.
|
||||
* @param source the source object
|
||||
* @param targetType the target type
|
||||
@@ -230,8 +230,8 @@ public class GenericConversionService implements ConfigurableConversionService {
|
||||
* {@link java.util.Optional#empty()} instance if the target type is
|
||||
* {@code java.util.Optional}. Subclasses may override this to return
|
||||
* custom {@code null} objects for specific target types.
|
||||
* @param sourceType the sourceType to convert from
|
||||
* @param targetType the targetType to convert to
|
||||
* @param sourceType the source type to convert from
|
||||
* @param targetType the target type to convert to
|
||||
* @return the converted null object
|
||||
*/
|
||||
protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
@@ -275,7 +275,7 @@ public class GenericConversionService implements ConfigurableConversionService {
|
||||
|
||||
/**
|
||||
* Return the default converter if no converter is found for the given sourceType/targetType pair.
|
||||
* <p>Returns a NO_OP Converter if the sourceType is assignable to the targetType.
|
||||
* <p>Returns a NO_OP Converter if the source type is assignable to the target type.
|
||||
* Returns {@code null} otherwise, indicating no suitable converter could be found.
|
||||
* @param sourceType the source type to convert from
|
||||
* @param targetType the target type to convert to
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -68,13 +68,13 @@ final class ObjectToOptionalConverter implements ConditionalGenericConverter {
|
||||
else if (source instanceof Optional) {
|
||||
return source;
|
||||
}
|
||||
else if (targetType.getResolvableType() == null) {
|
||||
return Optional.of(source);
|
||||
}
|
||||
else {
|
||||
else if (targetType.getResolvableType() != null) {
|
||||
Object target = this.conversionService.convert(source, sourceType, new GenericTypeDescriptor(targetType));
|
||||
return Optional.ofNullable(target);
|
||||
}
|
||||
else {
|
||||
return Optional.of(source);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user