1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -83,12 +83,12 @@ public void addConverter(Converter<?, ?> converter) {
83
83
GenericConverter .ConvertiblePair typeInfo = getRequiredTypeInfo (converter , Converter .class );
84
84
Assert .notNull (typeInfo , "Unable to the determine sourceType <S> and targetType " +
85
85
"<T> which your Converter<S, T> converts between; declare these generic types." );
86
- addConverter (new ConverterAdapter (typeInfo , converter ));
86
+ addConverter (new ConverterAdapter (converter , typeInfo ));
87
87
}
88
88
89
89
public void addConverter (Class <?> sourceType , Class <?> targetType , Converter <?, ?> converter ) {
90
90
GenericConverter .ConvertiblePair typeInfo = new GenericConverter .ConvertiblePair (sourceType , targetType );
91
- addConverter (new ConverterAdapter (typeInfo , converter ));
91
+ addConverter (new ConverterAdapter (converter , typeInfo ));
92
92
}
93
93
94
94
public void addConverter (GenericConverter converter ) {
@@ -103,7 +103,7 @@ public void addConverterFactory(ConverterFactory<?, ?> converterFactory) {
103
103
"targetRangeType R which your ConverterFactory<S, R> converts between; " +
104
104
"declare these generic types." );
105
105
}
106
- addConverter (new ConverterFactoryAdapter (typeInfo , converterFactory ));
106
+ addConverter (new ConverterFactoryAdapter (converterFactory , typeInfo ));
107
107
}
108
108
109
109
public void removeConvertible (Class <?> sourceType , Class <?> targetType ) {
@@ -114,14 +114,13 @@ public void removeConvertible(Class<?> sourceType, Class<?> targetType) {
114
114
// implementing ConversionService
115
115
116
116
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 ),
120
119
TypeDescriptor .valueOf (targetType ));
121
120
}
122
121
123
122
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" );
125
124
if (sourceType == null ) {
126
125
return true ;
127
126
}
@@ -189,6 +188,7 @@ public Object convert(Object source, TypeDescriptor targetType) {
189
188
return convert (source , TypeDescriptor .forObject (source ), targetType );
190
189
}
191
190
191
+ @ Override
192
192
public String toString () {
193
193
return this .converters .toString ();
194
194
}
@@ -297,17 +297,15 @@ private void assertNotPrimitiveTargetType(TypeDescriptor sourceType, TypeDescrip
297
297
@ SuppressWarnings ("unchecked" )
298
298
private final class ConverterAdapter implements ConditionalGenericConverter {
299
299
300
- private final ConvertiblePair typeInfo ;
301
-
302
300
private final Converter <Object , Object > converter ;
303
301
302
+ private final ConvertiblePair typeInfo ;
304
303
305
- public ConverterAdapter (ConvertiblePair typeInfo , Converter <?, ?> converter ) {
304
+ public ConverterAdapter (Converter <?, ?> converter , ConvertiblePair typeInfo ) {
306
305
this .converter = (Converter <Object , Object >) converter ;
307
306
this .typeInfo = typeInfo ;
308
307
}
309
308
310
-
311
309
public Set <ConvertiblePair > getConvertibleTypes () {
312
310
return Collections .singleton (this .typeInfo );
313
311
}
@@ -329,10 +327,9 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
329
327
return this .converter .convert (source );
330
328
}
331
329
330
+ @ Override
332
331
public String toString () {
333
- return this .typeInfo .getSourceType ().getName () + " -> " +
334
- this .typeInfo .getTargetType ().getName () + " : " +
335
- this .converter .toString ();
332
+ return this .typeInfo + " : " + this .converter ;
336
333
}
337
334
}
338
335
@@ -343,17 +340,15 @@ public String toString() {
343
340
@ SuppressWarnings ("unchecked" )
344
341
private final class ConverterFactoryAdapter implements ConditionalGenericConverter {
345
342
346
- private final ConvertiblePair typeInfo ;
347
-
348
343
private final ConverterFactory <Object , Object > converterFactory ;
349
344
345
+ private final ConvertiblePair typeInfo ;
350
346
351
- public ConverterFactoryAdapter (ConvertiblePair typeInfo , ConverterFactory <?, ?> converterFactory ) {
347
+ public ConverterFactoryAdapter (ConverterFactory <?, ?> converterFactory , ConvertiblePair typeInfo ) {
352
348
this .converterFactory = (ConverterFactory <Object , Object >) converterFactory ;
353
349
this .typeInfo = typeInfo ;
354
350
}
355
351
356
-
357
352
public Set <ConvertiblePair > getConvertibleTypes () {
358
353
return Collections .singleton (this .typeInfo );
359
354
}
@@ -379,10 +374,9 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
379
374
return this .converterFactory .getConverter (targetType .getObjectType ()).convert (source );
380
375
}
381
376
377
+ @ Override
382
378
public String toString () {
383
- return this .typeInfo .getSourceType ().getName () + " -> " +
384
- this .typeInfo .getTargetType ().getName () + " : " +
385
- this .converterFactory .toString ();
379
+ return this .typeInfo + " : " + this .converterFactory ;
386
380
}
387
381
}
388
382
@@ -396,13 +390,11 @@ private static final class ConverterCacheKey {
396
390
397
391
private final TypeDescriptor targetType ;
398
392
399
-
400
393
public ConverterCacheKey (TypeDescriptor sourceType , TypeDescriptor targetType ) {
401
394
this .sourceType = sourceType ;
402
395
this .targetType = targetType ;
403
396
}
404
397
405
-
406
398
public boolean equals (Object other ) {
407
399
if (this == other ) {
408
400
return true ;
@@ -411,18 +403,20 @@ public boolean equals(Object other) {
411
403
return false ;
412
404
}
413
405
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 );
416
408
}
417
409
410
+ @ Override
418
411
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 );
421
414
}
422
415
416
+ @ Override
423
417
public String toString () {
424
- return "ConverterCacheKey [sourceType = " + this .sourceType
425
- + ", targetType = " + this .targetType + "]" ;
418
+ return "ConverterCacheKey [sourceType = " + this .sourceType +
419
+ ", targetType = " + this .targetType + "]" ;
426
420
}
427
421
}
428
422
@@ -442,7 +436,7 @@ public void add(GenericConverter converter) {
442
436
if (convertibleTypes == null ) {
443
437
Assert .state (converter instanceof ConditionalConverter ,
444
438
"Only conditional converters may return null convertible types" );
445
- globalConverters .add (converter );
439
+ this . globalConverters .add (converter );
446
440
}
447
441
else {
448
442
for (ConvertiblePair convertiblePair : convertibleTypes ) {
@@ -466,12 +460,12 @@ public void remove(Class<?> sourceType, Class<?> targetType) {
466
460
}
467
461
468
462
/**
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.
472
466
* @param sourceType the source type
473
467
* @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
475
469
*/
476
470
public GenericConverter find (TypeDescriptor sourceType , TypeDescriptor targetType ) {
477
471
// Search the full type hierarchy
@@ -500,22 +494,19 @@ private GenericConverter getRegisteredConverter(TypeDescriptor sourceType,
500
494
return converter ;
501
495
}
502
496
}
503
-
504
497
// Check ConditionalGenericConverter that match all types
505
498
for (GenericConverter globalConverter : this .globalConverters ) {
506
499
if (((ConditionalConverter )globalConverter ).matches (sourceType , targetType )) {
507
500
return globalConverter ;
508
501
}
509
502
}
510
-
511
503
return null ;
512
504
}
513
505
514
506
/**
515
507
* Returns an ordered class hierarchy for the given type.
516
508
* @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
519
510
*/
520
511
private List <Class <?>> getClassHierarchy (Class <?> type ) {
521
512
List <Class <?>> hierarchy = new ArrayList <Class <?>>(20 );
@@ -525,8 +516,7 @@ private List<Class<?>> getClassHierarchy(Class<?> type) {
525
516
int i = 0 ;
526
517
while (i < hierarchy .size ()) {
527
518
Class <?> candidate = hierarchy .get (i );
528
- candidate = (array ? candidate .getComponentType ()
529
- : ClassUtils .resolvePrimitiveIfNecessary (candidate ));
519
+ candidate = (array ? candidate .getComponentType () : ClassUtils .resolvePrimitiveIfNecessary (candidate ));
530
520
Class <?> superclass = candidate .getSuperclass ();
531
521
if (candidate .getSuperclass () != null && superclass != Object .class ) {
532
522
addToClassHierarchy (i + 1 , candidate .getSuperclass (), array , hierarchy , visited );
@@ -554,11 +544,9 @@ private void addToClassHierarchy(int index, Class<?> type, boolean asArray,
554
544
@ Override
555
545
public String toString () {
556
546
StringBuilder builder = new StringBuilder ();
557
- builder .append ("ConversionService converters = " ). append ( " \n " );
547
+ builder .append ("ConversionService converters =\n " );
558
548
for (String converterString : getConverterStrings ()) {
559
- builder .append ("\t " );
560
- builder .append (converterString );
561
- builder .append ("\n " );
549
+ builder .append ('\t' ).append (converterString ).append ('\n' );
562
550
}
563
551
return builder .toString ();
564
552
}
@@ -595,6 +583,7 @@ public GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor t
595
583
return null ;
596
584
}
597
585
586
+ @ Override
598
587
public String toString () {
599
588
return StringUtils .collectionToCommaDelimitedString (this .converters );
600
589
}
@@ -612,7 +601,6 @@ public NoOpConverter(String name) {
612
601
this .name = name ;
613
602
}
614
603
615
-
616
604
public Set <ConvertiblePair > getConvertibleTypes () {
617
605
return null ;
618
606
}
0 commit comments