Skip to content

Commit 1a01161

Browse files
committed
Polishing
Issue: SPR-11369
1 parent 3af9d1f commit 1a01161

File tree

4 files changed

+108
-57
lines changed

4 files changed

+108
-57
lines changed

spring-core/src/main/java/org/springframework/core/convert/converter/ConditionalConverter.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,11 +26,10 @@
2626
* <p>Often used to selectively match custom conversion logic based on the presence of a
2727
* field or class-level characteristic, such as an annotation or method. For example, when
2828
* converting from a String field to a Date field, an implementation might return
29-
*
3029
* {@code true} if the target field has also been annotated with {@code @DateTimeFormat}.
3130
*
32-
* <p>As another example, when converting from a String field to an {@code Account} field, an
33-
* implementation might return {@code true} if the target Account class defines a
31+
* <p>As another example, when converting from a String field to an {@code Account} field,
32+
* an implementation might return {@code true} if the target Account class defines a
3433
* {@code public static findAccount(String)} method.
3534
*
3635
* @author Phillip Webb
@@ -46,10 +45,10 @@ public interface ConditionalConverter {
4645
/**
4746
* Should the conversion from {@code sourceType} to {@code targetType} currently under
4847
* consideration be selected?
49-
*
5048
* @param sourceType the type descriptor of the field we are converting from
5149
* @param targetType the type descriptor of the field we are converting to
5250
* @return true if conversion should be performed, false otherwise
5351
*/
5452
boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType);
53+
5554
}

spring-core/src/main/java/org/springframework/core/convert/converter/GenericConverter.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,11 @@
1616

1717
package org.springframework.core.convert.converter;
1818

19+
import java.util.Set;
20+
1921
import org.springframework.core.convert.TypeDescriptor;
2022
import org.springframework.util.Assert;
2123

22-
import java.util.Set;
23-
2424
/**
2525
* Generic converter interface for converting between two or more types.
2626
*
@@ -104,13 +104,17 @@ public boolean equals(Object obj) {
104104
}
105105
ConvertiblePair other = (ConvertiblePair) obj;
106106
return this.sourceType.equals(other.sourceType) && this.targetType.equals(other.targetType);
107-
108107
}
109108

110109
@Override
111110
public int hashCode() {
112111
return this.sourceType.hashCode() * 31 + this.targetType.hashCode();
113112
}
113+
114+
@Override
115+
public String toString() {
116+
return this.sourceType.getName() + " -> " + this.targetType.getName();
117+
}
114118
}
115119

116120
}

spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -83,12 +83,12 @@ public void addConverter(Converter<?, ?> converter) {
8383
GenericConverter.ConvertiblePair typeInfo = getRequiredTypeInfo(converter, Converter.class);
8484
Assert.notNull(typeInfo, "Unable to the determine sourceType <S> and targetType " +
8585
"<T> which your Converter<S, T> converts between; declare these generic types.");
86-
addConverter(new ConverterAdapter(typeInfo, converter));
86+
addConverter(new ConverterAdapter(converter, typeInfo));
8787
}
8888

8989
public void addConverter(Class<?> sourceType, Class<?> targetType, Converter<?, ?> converter) {
9090
GenericConverter.ConvertiblePair typeInfo = new GenericConverter.ConvertiblePair(sourceType, targetType);
91-
addConverter(new ConverterAdapter(typeInfo, converter));
91+
addConverter(new ConverterAdapter(converter, typeInfo));
9292
}
9393

9494
public void addConverter(GenericConverter converter) {
@@ -103,7 +103,7 @@ public void addConverterFactory(ConverterFactory<?, ?> converterFactory) {
103103
"targetRangeType R which your ConverterFactory<S, R> converts between; " +
104104
"declare these generic types.");
105105
}
106-
addConverter(new ConverterFactoryAdapter(typeInfo, converterFactory));
106+
addConverter(new ConverterFactoryAdapter(converterFactory, typeInfo));
107107
}
108108

109109
public void removeConvertible(Class<?> sourceType, Class<?> targetType) {
@@ -114,14 +114,13 @@ public void removeConvertible(Class<?> sourceType, Class<?> targetType) {
114114
// implementing ConversionService
115115

116116
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
117-
Assert.notNull(targetType, "The targetType to convert to cannot be null");
118-
return canConvert(sourceType != null ?
119-
TypeDescriptor.valueOf(sourceType) : null,
117+
Assert.notNull(targetType, "targetType to convert to cannot be null");
118+
return canConvert((sourceType != null ? TypeDescriptor.valueOf(sourceType) : null),
120119
TypeDescriptor.valueOf(targetType));
121120
}
122121

123122
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
124-
Assert.notNull(targetType,"The targetType to convert to cannot be null");
123+
Assert.notNull(targetType, "targetType to convert to cannot be null");
125124
if (sourceType == null) {
126125
return true;
127126
}
@@ -189,6 +188,7 @@ public Object convert(Object source, TypeDescriptor targetType) {
189188
return convert(source, TypeDescriptor.forObject(source), targetType);
190189
}
191190

191+
@Override
192192
public String toString() {
193193
return this.converters.toString();
194194
}
@@ -297,17 +297,15 @@ private void assertNotPrimitiveTargetType(TypeDescriptor sourceType, TypeDescrip
297297
@SuppressWarnings("unchecked")
298298
private final class ConverterAdapter implements ConditionalGenericConverter {
299299

300-
private final ConvertiblePair typeInfo;
301-
302300
private final Converter<Object, Object> converter;
303301

302+
private final ConvertiblePair typeInfo;
304303

305-
public ConverterAdapter(ConvertiblePair typeInfo, Converter<?, ?> converter) {
304+
public ConverterAdapter(Converter<?, ?> converter, ConvertiblePair typeInfo) {
306305
this.converter = (Converter<Object, Object>) converter;
307306
this.typeInfo = typeInfo;
308307
}
309308

310-
311309
public Set<ConvertiblePair> getConvertibleTypes() {
312310
return Collections.singleton(this.typeInfo);
313311
}
@@ -329,10 +327,9 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
329327
return this.converter.convert(source);
330328
}
331329

330+
@Override
332331
public String toString() {
333-
return this.typeInfo.getSourceType().getName() + " -> " +
334-
this.typeInfo.getTargetType().getName() + " : " +
335-
this.converter.toString();
332+
return this.typeInfo + " : " + this.converter;
336333
}
337334
}
338335

@@ -343,17 +340,15 @@ public String toString() {
343340
@SuppressWarnings("unchecked")
344341
private final class ConverterFactoryAdapter implements ConditionalGenericConverter {
345342

346-
private final ConvertiblePair typeInfo;
347-
348343
private final ConverterFactory<Object, Object> converterFactory;
349344

345+
private final ConvertiblePair typeInfo;
350346

351-
public ConverterFactoryAdapter(ConvertiblePair typeInfo, ConverterFactory<?, ?> converterFactory) {
347+
public ConverterFactoryAdapter(ConverterFactory<?, ?> converterFactory, ConvertiblePair typeInfo) {
352348
this.converterFactory = (ConverterFactory<Object, Object>) converterFactory;
353349
this.typeInfo = typeInfo;
354350
}
355351

356-
357352
public Set<ConvertiblePair> getConvertibleTypes() {
358353
return Collections.singleton(this.typeInfo);
359354
}
@@ -379,10 +374,9 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
379374
return this.converterFactory.getConverter(targetType.getObjectType()).convert(source);
380375
}
381376

377+
@Override
382378
public String toString() {
383-
return this.typeInfo.getSourceType().getName() + " -> " +
384-
this.typeInfo.getTargetType().getName() + " : " +
385-
this.converterFactory.toString();
379+
return this.typeInfo + " : " + this.converterFactory;
386380
}
387381
}
388382

@@ -396,13 +390,11 @@ private static final class ConverterCacheKey {
396390

397391
private final TypeDescriptor targetType;
398392

399-
400393
public ConverterCacheKey(TypeDescriptor sourceType, TypeDescriptor targetType) {
401394
this.sourceType = sourceType;
402395
this.targetType = targetType;
403396
}
404397

405-
406398
public boolean equals(Object other) {
407399
if (this == other) {
408400
return true;
@@ -411,18 +403,20 @@ public boolean equals(Object other) {
411403
return false;
412404
}
413405
ConverterCacheKey otherKey = (ConverterCacheKey) other;
414-
return ObjectUtils.nullSafeEquals(this.sourceType, otherKey.sourceType)
415-
&& ObjectUtils.nullSafeEquals(this.targetType, otherKey.targetType);
406+
return ObjectUtils.nullSafeEquals(this.sourceType, otherKey.sourceType) &&
407+
ObjectUtils.nullSafeEquals(this.targetType, otherKey.targetType);
416408
}
417409

410+
@Override
418411
public int hashCode() {
419-
return ObjectUtils.nullSafeHashCode(this.sourceType) * 29
420-
+ ObjectUtils.nullSafeHashCode(this.targetType);
412+
return ObjectUtils.nullSafeHashCode(this.sourceType) * 29 +
413+
ObjectUtils.nullSafeHashCode(this.targetType);
421414
}
422415

416+
@Override
423417
public String toString() {
424-
return "ConverterCacheKey [sourceType = " + this.sourceType
425-
+ ", targetType = " + this.targetType + "]";
418+
return "ConverterCacheKey [sourceType = " + this.sourceType +
419+
", targetType = " + this.targetType + "]";
426420
}
427421
}
428422

@@ -442,7 +436,7 @@ public void add(GenericConverter converter) {
442436
if (convertibleTypes == null) {
443437
Assert.state(converter instanceof ConditionalConverter,
444438
"Only conditional converters may return null convertible types");
445-
globalConverters.add(converter);
439+
this.globalConverters.add(converter);
446440
}
447441
else {
448442
for (ConvertiblePair convertiblePair : convertibleTypes) {
@@ -466,12 +460,12 @@ public void remove(Class<?> sourceType, Class<?> targetType) {
466460
}
467461

468462
/**
469-
* Find a {@link GenericConverter} given a source and target type. This method will
470-
* attempt to match all possible converters by working though the class and interface
471-
* hierarchy of the types.
463+
* Find a {@link GenericConverter} given a source and target type.
464+
* <p>This method will attempt to match all possible converters by working
465+
* through the class and interface hierarchy of the types.
472466
* @param sourceType the source type
473467
* @param targetType the target type
474-
* @return a {@link GenericConverter} or <tt>null</tt>
468+
* @return a matching {@link GenericConverter}, or {@code null} if none found
475469
*/
476470
public GenericConverter find(TypeDescriptor sourceType, TypeDescriptor targetType) {
477471
// Search the full type hierarchy
@@ -500,22 +494,19 @@ private GenericConverter getRegisteredConverter(TypeDescriptor sourceType,
500494
return converter;
501495
}
502496
}
503-
504497
// Check ConditionalGenericConverter that match all types
505498
for (GenericConverter globalConverter : this.globalConverters) {
506499
if (((ConditionalConverter)globalConverter).matches(sourceType, targetType)) {
507500
return globalConverter;
508501
}
509502
}
510-
511503
return null;
512504
}
513505

514506
/**
515507
* Returns an ordered class hierarchy for the given type.
516508
* @param type the type
517-
* @return an ordered list of all classes that the given type extends or
518-
* implements.
509+
* @return an ordered list of all classes that the given type extends or implements
519510
*/
520511
private List<Class<?>> getClassHierarchy(Class<?> type) {
521512
List<Class<?>> hierarchy = new ArrayList<Class<?>>(20);
@@ -525,8 +516,7 @@ private List<Class<?>> getClassHierarchy(Class<?> type) {
525516
int i = 0;
526517
while (i < hierarchy.size()) {
527518
Class<?> candidate = hierarchy.get(i);
528-
candidate = (array ? candidate.getComponentType()
529-
: ClassUtils.resolvePrimitiveIfNecessary(candidate));
519+
candidate = (array ? candidate.getComponentType() : ClassUtils.resolvePrimitiveIfNecessary(candidate));
530520
Class<?> superclass = candidate.getSuperclass();
531521
if (candidate.getSuperclass() != null && superclass != Object.class) {
532522
addToClassHierarchy(i + 1, candidate.getSuperclass(), array, hierarchy, visited);
@@ -554,11 +544,9 @@ private void addToClassHierarchy(int index, Class<?> type, boolean asArray,
554544
@Override
555545
public String toString() {
556546
StringBuilder builder = new StringBuilder();
557-
builder.append("ConversionService converters = ").append("\n");
547+
builder.append("ConversionService converters =\n");
558548
for (String converterString : getConverterStrings()) {
559-
builder.append("\t");
560-
builder.append(converterString);
561-
builder.append("\n");
549+
builder.append('\t').append(converterString).append('\n');
562550
}
563551
return builder.toString();
564552
}
@@ -595,6 +583,7 @@ public GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor t
595583
return null;
596584
}
597585

586+
@Override
598587
public String toString() {
599588
return StringUtils.collectionToCommaDelimitedString(this.converters);
600589
}
@@ -612,7 +601,6 @@ public NoOpConverter(String name) {
612601
this.name = name;
613602
}
614603

615-
616604
public Set<ConvertiblePair> getConvertibleTypes() {
617605
return null;
618606
}

0 commit comments

Comments
 (0)