Polishing

Issue: SPR-11259
(cherry picked from commit f0d2151)
This commit is contained in:
Juergen Hoeller
2014-01-01 19:24:37 +01:00
parent 57eedf33d6
commit 2e123b01e5
4 changed files with 38 additions and 48 deletions

View File

@@ -59,7 +59,7 @@ public class DateFormatterRegistrar implements FormatterRegistrar {
// In order to retain back compatibility we only register Date/Calendar
// types when a user defined formatter is specified (see SPR-10105)
if(this.dateFormatter != null) {
if (this.dateFormatter != null) {
registry.addFormatter(this.dateFormatter);
registry.addFormatterForFieldType(Calendar.class, this.dateFormatter);
}
@@ -108,7 +108,7 @@ public class DateFormatterRegistrar implements FormatterRegistrar {
private static class CalendarToLongConverter implements Converter<Calendar, Long> {
public Long convert(Calendar source) {
return source.getTime().getTime();
return source.getTimeInMillis();
}
}
@@ -123,10 +123,10 @@ public class DateFormatterRegistrar implements FormatterRegistrar {
private static class LongToCalendarConverter implements Converter<Long, Calendar> {
private final DateToCalendarConverter dateToCalendarConverter = new DateToCalendarConverter();
public Calendar convert(Long source) {
return this.dateToCalendarConverter.convert(new Date(source));
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(source);
return calendar;
}
}

View File

@@ -184,7 +184,7 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar {
// In order to retain backwards compatibility we only register Date/Calendar
// types when a user defined formatter is specified (see SPR-10105)
if( this.formatters.containsKey(Type.DATE_TIME)) {
if (this.formatters.containsKey(Type.DATE_TIME)) {
addFormatterForFields(registry,
new ReadableInstantPrinter(dateTimeFormatter),
new DateTimeParser(dateTimeFormatter),

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
@@ -313,7 +313,7 @@ public class GenericConversionService implements ConfigurableConversionService {
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
if(!this.typeInfo.getTargetType().equals(targetType.getObjectType())) {
if (!this.typeInfo.getTargetType().equals(targetType.getObjectType())) {
return false;
}
if (this.converter instanceof ConditionalConverter) {
@@ -363,9 +363,9 @@ public class GenericConversionService implements ConfigurableConversionService {
if (this.converterFactory instanceof ConditionalConverter) {
matches = ((ConditionalConverter) this.converterFactory).matches(sourceType, targetType);
}
if(matches) {
if (matches) {
Converter<?, ?> converter = this.converterFactory.getConverter(targetType.getType());
if(converter instanceof ConditionalConverter) {
if (converter instanceof ConditionalConverter) {
matches = ((ConditionalConverter) converter).matches(sourceType, targetType);
}
}
@@ -426,16 +426,16 @@ public class GenericConversionService implements ConfigurableConversionService {
}
}
/**
* Manages all converters registered with the service.
*/
private static class Converters {
private final Set<GenericConverter> globalConverters =
new LinkedHashSet<GenericConverter>();
private final Set<GenericConverter> globalConverters = new LinkedHashSet<GenericConverter>();
private final Map<ConvertiblePair, ConvertersForPair> converters =
new LinkedHashMap<ConvertiblePair, ConvertersForPair>(36);
new LinkedHashMap<ConvertiblePair, ConvertersForPair>(36);
public void add(GenericConverter converter) {
Set<ConvertiblePair> convertibleTypes = converter.getConvertibleTypes();
@@ -462,7 +462,7 @@ public class GenericConversionService implements ConfigurableConversionService {
}
public void remove(Class<?> sourceType, Class<?> targetType) {
converters.remove(new ConvertiblePair(sourceType, targetType));
this.converters.remove(new ConvertiblePair(sourceType, targetType));
}
/**
@@ -480,9 +480,8 @@ public class GenericConversionService implements ConfigurableConversionService {
for (Class<?> sourceCandidate : sourceCandidates) {
for (Class<?> targetCandidate : targetCandidates) {
ConvertiblePair convertiblePair = new ConvertiblePair(sourceCandidate, targetCandidate);
GenericConverter converter = getRegisteredConverter(
sourceType, targetType, convertiblePair);
if(converter != null) {
GenericConverter converter = getRegisteredConverter(sourceType, targetType, convertiblePair);
if (converter != null) {
return converter;
}
}
@@ -494,17 +493,17 @@ public class GenericConversionService implements ConfigurableConversionService {
TypeDescriptor targetType, ConvertiblePair convertiblePair) {
// Check specifically registered converters
ConvertersForPair convertersForPair = converters.get(convertiblePair);
GenericConverter converter = convertersForPair == null ? null
: convertersForPair.getConverter(sourceType, targetType);
if (converter != null) {
return converter;
ConvertersForPair convertersForPair = this.converters.get(convertiblePair);
if (convertersForPair != null) {
GenericConverter converter = convertersForPair.getConverter(sourceType, targetType);
if (converter != null) {
return converter;
}
}
// Check ConditionalGenericConverter that match all types
for (GenericConverter globalConverter : this.globalConverters) {
if (((ConditionalConverter)globalConverter).matches(
sourceType, targetType)) {
if (((ConditionalConverter)globalConverter).matches(sourceType, targetType)) {
return globalConverter;
}
}
@@ -544,10 +543,10 @@ public class GenericConversionService implements ConfigurableConversionService {
private void addToClassHierarchy(int index, Class<?> type, boolean asArray,
List<Class<?>> hierarchy, Set<Class<?>> visited) {
if(asArray) {
if (asArray) {
type = Array.newInstance(type, 0).getClass();
}
if(visited.add(type)) {
if (visited.add(type)) {
hierarchy.add(index, type);
}
}
@@ -586,12 +585,10 @@ public class GenericConversionService implements ConfigurableConversionService {
this.converters.addFirst(converter);
}
public GenericConverter getConverter(TypeDescriptor sourceType,
TypeDescriptor targetType) {
public GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
for (GenericConverter converter : this.converters) {
if (!(converter instanceof ConditionalGenericConverter)
|| ((ConditionalGenericConverter) converter).matches(sourceType,
targetType)) {
if (!(converter instanceof ConditionalGenericConverter) ||
((ConditionalGenericConverter) converter).matches(sourceType, targetType)) {
return converter;
}
}
@@ -609,8 +606,7 @@ public class GenericConversionService implements ConfigurableConversionService {
*/
private static class NoOpConverter implements GenericConverter {
private String name;
private final String name;
public NoOpConverter(String name) {
this.name = name;
@@ -621,14 +617,14 @@ public class GenericConversionService implements ConfigurableConversionService {
return null;
}
public Object convert(Object source, TypeDescriptor sourceType,
TypeDescriptor targetType) {
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
return source;
}
@Override
public String toString() {
return name;
return this.name;
}
}
}

View File

@@ -16,16 +16,6 @@
package org.springframework.core.convert.support;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.*;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.awt.Color;
import java.math.BigDecimal;
import java.math.BigInteger;
@@ -45,6 +35,7 @@ import java.util.Properties;
import java.util.Set;
import org.junit.Test;
import org.springframework.core.MethodParameter;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConverterNotFoundException;
@@ -52,6 +43,9 @@ import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterRegistry;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* @author Keith Donald
* @author Juergen Hoeller
@@ -214,7 +208,7 @@ public class DefaultConversionTests {
}
@Test
public void testStringToEnumWithSubclss() throws Exception {
public void testStringToEnumWithSubclass() throws Exception {
assertEquals(SubFoo.BAZ, conversionService.convert("BAZ", SubFoo.BAR.getClass()));
}
@@ -229,7 +223,7 @@ public class DefaultConversionTests {
}
public static enum Foo {
BAR, BAZ;
BAR, BAZ
}
public static enum SubFoo {