From 2e123b01e5b8b4d0ac9b1829f53679f06868cd0f Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 1 Jan 2014 19:24:37 +0100 Subject: [PATCH] Polishing Issue: SPR-11259 (cherry picked from commit f0d2151) --- .../datetime/DateFormatterRegistrar.java | 10 ++-- .../joda/JodaTimeFormatterRegistrar.java | 2 +- .../support/GenericConversionService.java | 56 +++++++++---------- .../support/DefaultConversionTests.java | 18 ++---- 4 files changed, 38 insertions(+), 48 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/format/datetime/DateFormatterRegistrar.java b/spring-context/src/main/java/org/springframework/format/datetime/DateFormatterRegistrar.java index cc9bf6265fc..b94ddc9e7df 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/DateFormatterRegistrar.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/DateFormatterRegistrar.java @@ -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 { 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 { - 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; } } diff --git a/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaTimeFormatterRegistrar.java b/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaTimeFormatterRegistrar.java index f780f91ee8f..a0f385123e4 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaTimeFormatterRegistrar.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaTimeFormatterRegistrar.java @@ -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), diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java b/spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java index 148dbee97d6..2f4f4f1ba4b 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java @@ -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 globalConverters = - new LinkedHashSet(); + private final Set globalConverters = new LinkedHashSet(); private final Map converters = - new LinkedHashMap(36); + new LinkedHashMap(36); public void add(GenericConverter converter) { Set 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> hierarchy, Set> 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; } } + } diff --git a/spring-core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java b/spring-core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java index ba42cddf9da..139e9099c1e 100644 --- a/spring-core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java +++ b/spring-core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java @@ -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 {