Polish contribution

Issue: SPR-14039 - Closes gh-998
This commit is contained in:
Stephane Nicoll
2016-03-16 16:59:30 +01:00
parent 1a302e1fac
commit f422e490b0
7 changed files with 76 additions and 66 deletions

View File

@@ -0,0 +1,48 @@
/*
* 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.
* 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.springframework.core.convert.support;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalConverter;
import org.springframework.util.ClassUtils;
/**
* A {@link ConditionalConverter} base implementation for enum-based converters.
*
* @author Stephane Nicoll
* @since 4.3
*/
abstract class AbstractConditionalEnumConverter implements ConditionalConverter {
private final ConversionService conversionService;
protected AbstractConditionalEnumConverter(ConversionService conversionService) {
this.conversionService = conversionService;
}
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
for (Class<?> interfaceType : ClassUtils.getAllInterfacesForClass(sourceType.getType())) {
if (this.conversionService.canConvert(TypeDescriptor.valueOf(interfaceType), targetType)) {
return false;
}
}
return true;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 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.
@@ -25,6 +25,7 @@ import org.springframework.core.convert.converter.GenericConverter;
* Internal utilities for the conversion package.
*
* @author Keith Donald
* @author Stephane Nicoll
* @since 3.0
*/
abstract class ConversionUtils {
@@ -65,4 +66,16 @@ abstract class ConversionUtils {
}
}
public static Class<?> getEnumType(Class<?> targetType) {
Class<?> enumType = targetType;
while (enumType != null && !enumType.isEnum()) {
enumType = enumType.getSuperclass();
}
if (enumType == null) {
throw new IllegalArgumentException(
"The target type " + targetType.getName() + " does not refer to an enum");
}
return enumType;
}
}

View File

@@ -17,36 +17,19 @@
package org.springframework.core.convert.support;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalConverter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.ClassUtils;
/**
* Calls {@link Enum#ordinal()} to convert a source Enum to a Integer.
* This converter will not match enums with interfaces that can be converted.
*
* @author Yanming Zhou (zhouyanming@gmail.com)
* @author Yanming Zhou
* @since 4.3
*/
final class EnumToIntegerConverter implements Converter<Enum<?>, Integer>, ConditionalConverter {
private final ConversionService conversionService;
final class EnumToIntegerConverter extends AbstractConditionalEnumConverter implements Converter<Enum<?>, Integer> {
public EnumToIntegerConverter(ConversionService conversionService) {
this.conversionService = conversionService;
}
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
for (Class<?> interfaceType : ClassUtils.getAllInterfacesForClass(sourceType.getType())) {
if (this.conversionService.canConvert(TypeDescriptor.valueOf(interfaceType), targetType)) {
return false;
}
}
return true;
super(conversionService);
}
@Override

View File

@@ -17,10 +17,7 @@
package org.springframework.core.convert.support;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalConverter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.ClassUtils;
/**
* Calls {@link Enum#name()} to convert a source Enum to a String.
@@ -30,24 +27,10 @@ import org.springframework.util.ClassUtils;
* @author Phillip Webb
* @since 3.0
*/
final class EnumToStringConverter implements Converter<Enum<?>, String>, ConditionalConverter {
private final ConversionService conversionService;
final class EnumToStringConverter extends AbstractConditionalEnumConverter implements Converter<Enum<?>, String> {
public EnumToStringConverter(ConversionService conversionService) {
this.conversionService = conversionService;
}
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
for (Class<?> interfaceType : ClassUtils.getAllInterfacesForClass(sourceType.getType())) {
if (this.conversionService.canConvert(TypeDescriptor.valueOf(interfaceType), targetType)) {
return false;
}
}
return true;
super(conversionService);
}
@Override

View File

@@ -22,7 +22,8 @@ import org.springframework.core.convert.converter.ConverterFactory;
/**
* Converts from a Integer to a {@link java.lang.Enum} by calling {@link Class#getEnumConstants()}.
*
* @author Yanming Zhou (zhouyanming@gmail.com)
* @author Yanming Zhou
* @author Stephane Nicoll
* @since 4.3
*/
@SuppressWarnings({"unchecked", "rawtypes"})
@@ -30,15 +31,7 @@ final class IntegerToEnumConverterFactory implements ConverterFactory<Integer, E
@Override
public <T extends Enum> Converter<Integer, T> getConverter(Class<T> targetType) {
Class<?> enumType = targetType;
while (enumType != null && !enumType.isEnum()) {
enumType = enumType.getSuperclass();
}
if (enumType == null) {
throw new IllegalArgumentException(
"The target type " + targetType.getName() + " does not refer to an enum");
}
return new IntegerToEnum(enumType);
return new IntegerToEnum(ConversionUtils.getEnumType(targetType));
}
@@ -52,10 +45,7 @@ final class IntegerToEnumConverterFactory implements ConverterFactory<Integer, E
@Override
public T convert(Integer source) {
if (source == null) {
return null;
}
return enumType.getEnumConstants()[source];
return this.enumType.getEnumConstants()[source];
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 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.
@@ -23,6 +23,7 @@ import org.springframework.core.convert.converter.ConverterFactory;
* Converts from a String to a {@link java.lang.Enum} by calling {@link Enum#valueOf(Class, String)}.
*
* @author Keith Donald
* @author Stephane Nicoll
* @since 3.0
*/
@SuppressWarnings({"unchecked", "rawtypes"})
@@ -30,15 +31,7 @@ final class StringToEnumConverterFactory implements ConverterFactory<String, Enu
@Override
public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {
Class<?> enumType = targetType;
while (enumType != null && !enumType.isEnum()) {
enumType = enumType.getSuperclass();
}
if (enumType == null) {
throw new IllegalArgumentException(
"The target type " + targetType.getName() + " does not refer to an enum");
}
return new StringToEnum(enumType);
return new StringToEnum(ConversionUtils.getEnumType(targetType));
}