Skip to content

Commit 16bf501

Browse files
committed
Polishing
1 parent fd13c99 commit 16bf501

File tree

7 files changed

+447
-504
lines changed

7 files changed

+447
-504
lines changed
Lines changed: 13 additions & 11 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-2013 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.
@@ -17,30 +17,32 @@
1717
package org.springframework.expression;
1818

1919
/**
20-
* Instances of a type comparator should be able to compare pairs of objects for equality, the specification of the
21-
* return value is the same as for {@link Comparable}.
20+
* Instances of a type comparator should be able to compare pairs of objects for equality.
21+
* The specification of the return value is the same as for {@link java.lang.Comparable}.
2222
*
2323
* @author Andy Clement
2424
* @since 3.0
25+
* @see java.lang.Comparable
2526
*/
2627
public interface TypeComparator {
2728

2829
/**
29-
* Compare two objects.
30+
* Return {@code true} if the comparator can compare these two objects.
3031
* @param firstObject the first object
3132
* @param secondObject the second object
32-
* @return 0 if they are equal, <0 if the first is smaller than the second, or >0 if the first is larger than the
33-
* second
34-
* @throws EvaluationException if a problem occurs during comparison (or they are not comparable)
33+
* @return {@code true} if the comparator can compare these objects
3534
*/
36-
int compare(Object firstObject, Object secondObject) throws EvaluationException;
35+
boolean canCompare(Object firstObject, Object secondObject);
3736

3837
/**
39-
* Return true if the comparator can compare these two objects
38+
* Compare two given objects.
4039
* @param firstObject the first object
4140
* @param secondObject the second object
42-
* @return true if the comparator can compare these objects
41+
* @return 0 if they are equal, <0 if the first is smaller than the second,
42+
* or >0 if the first is larger than the second
43+
* @throws EvaluationException if a problem occurs during comparison
44+
* (or if they are not comparable in the first place)
4345
*/
44-
boolean canCompare(Object firstObject, Object secondObject);
46+
int compare(Object firstObject, Object secondObject) throws EvaluationException;
4547

4648
}

spring-expression/src/main/java/org/springframework/expression/TypeConverter.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,23 @@ public interface TypeConverter {
3535
* to the desired target type.
3636
* @param sourceType a type descriptor that describes the source type
3737
* @param targetType a type descriptor that describes the requested result type
38-
* @return true if that conversion can be performed
38+
* @return {@code true} if that conversion can be performed
3939
*/
4040
boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType);
4141

4242
/**
43-
* Convert (may coerce) a value from one type to another, for example from a boolean
44-
* to a string. The typeDescriptor parameter enables support for typed collections -
45-
* if the caller really wishes they can have a List&lt;Integer&gt; for example, rather
46-
* than simply a List.
43+
* Convert (or coerce) a value from one type to another, for example from a
44+
* {@code boolean} to a {@code String}.
45+
* <p>The {@link TypeDescriptor} parameters enable support for typed collections:
46+
* A caller may prefer a {@code List&lt;Integer&gt;}, for example, rather than
47+
* simply any {@code List}.
4748
* @param value the value to be converted
4849
* @param sourceType a type descriptor that supplies extra information about the
4950
* source object
5051
* @param targetType a type descriptor that supplies extra information about the
5152
* requested result type
5253
* @return the converted value
53-
* @throws EvaluationException if conversion is not possible
54+
* @throws EvaluationException if conversion failed or is not possible to begin with
5455
*/
5556
Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType);
5657

spring-expression/src/main/java/org/springframework/expression/TypeLocator.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2013 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.
@@ -17,21 +17,25 @@
1717
package org.springframework.expression;
1818

1919
/**
20-
* Implementors of this interface are expected to be able to locate types. They may use custom classloaders
21-
* or the and deal with common package prefixes (java.lang, etc) however they wish. See
22-
* {@link org.springframework.expression.spel.support.StandardTypeLocator} for an example implementation.
20+
* Implementers of this interface are expected to be able to locate types.
21+
* They may use a custom {@link ClassLoader} and/or deal with common
22+
* package prefixes (e.g. {@code java.lang}) however they wish.
23+
*
24+
* <p>See {@link org.springframework.expression.spel.support.StandardTypeLocator}
25+
* for an example implementation.
2326
*
2427
* @author Andy Clement
2528
* @since 3.0
2629
*/
2730
public interface TypeLocator {
2831

2932
/**
30-
* Find a type by name. The name may or may not be fully qualified (eg. String or java.lang.String)
31-
* @param typename the type to be located
32-
* @return the class object representing that type
33-
* @throws EvaluationException if there is a problem finding it
33+
* Find a type by name. The name may or may not be fully qualified
34+
* (e.g. {@code String} or {@code java.lang.String}).
35+
* @param typeName the type to be located
36+
* @return the {@code Class} object representing that type
37+
* @throws EvaluationException if there is a problem finding the type
3438
*/
35-
Class<?> findType(String typename) throws EvaluationException;
39+
Class<?> findType(String typeName) throws EvaluationException;
3640

3741
}
Lines changed: 43 additions & 33 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-2013 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.
@@ -21,68 +21,78 @@
2121
import org.springframework.expression.spel.SpelMessage;
2222

2323
/**
24-
* A simple basic TypeComparator implementation. It supports comparison of numbers and types implementing Comparable.
24+
* A simple basic {@link TypeComparator} implementation.
25+
* It supports comparison of Numbers and types implementing Comparable.
2526
*
2627
* @author Andy Clement
2728
* @author Juergen Hoeller
2829
* @since 3.0
2930
*/
3031
public class StandardTypeComparator implements TypeComparator {
3132

33+
public boolean canCompare(Object left, Object right) {
34+
if (left == null || right == null) {
35+
return true;
36+
}
37+
if (left instanceof Number && right instanceof Number) {
38+
return true;
39+
}
40+
if (left instanceof Comparable) {
41+
return true;
42+
}
43+
return false;
44+
}
45+
3246
@SuppressWarnings("unchecked")
3347
public int compare(Object left, Object right) throws SpelEvaluationException {
3448
// If one is null, check if the other is
3549
if (left == null) {
36-
return right == null ? 0 : -1;
37-
} else if (right == null) {
38-
return 1; // left cannot be null
50+
return (right == null ? 0 : -1);
51+
}
52+
else if (right == null) {
53+
return 1; // left cannot be null at this point
3954
}
4055

4156
// Basic number comparisons
4257
if (left instanceof Number && right instanceof Number) {
4358
Number leftNumber = (Number) left;
4459
Number rightNumber = (Number) right;
60+
4561
if (leftNumber instanceof Double || rightNumber instanceof Double) {
46-
double d1 = leftNumber.doubleValue();
47-
double d2 = rightNumber.doubleValue();
48-
return Double.compare(d1,d2);
49-
} else if (leftNumber instanceof Float || rightNumber instanceof Float) {
50-
float f1 = leftNumber.floatValue();
51-
float f2 = rightNumber.floatValue();
52-
return Float.compare(f1,f2);
53-
} else if (leftNumber instanceof Long || rightNumber instanceof Long) {
54-
Long l1 = leftNumber.longValue();
55-
Long l2 = rightNumber.longValue();
56-
return l1.compareTo(l2);
57-
} else {
58-
Integer i1 = leftNumber.intValue();
59-
Integer i2 = rightNumber.intValue();
60-
return i1.compareTo(i2);
62+
return Double.compare(leftNumber.doubleValue(), rightNumber.doubleValue());
63+
}
64+
else if (leftNumber instanceof Float || rightNumber instanceof Float) {
65+
return Float.compare(leftNumber.floatValue(), rightNumber.floatValue());
66+
}
67+
else if (leftNumber instanceof Long || rightNumber instanceof Long) {
68+
// Don't call Long.compare here - only available on JDK 1.7+
69+
return compare(leftNumber.longValue(), rightNumber.longValue());
70+
}
71+
else {
72+
// Don't call Integer.compare here - only available on JDK 1.7+
73+
return compare(leftNumber.intValue(), rightNumber.intValue());
6174
}
6275
}
6376

6477
try {
6578
if (left instanceof Comparable) {
6679
return ((Comparable) left).compareTo(right);
6780
}
68-
} catch (ClassCastException cce) {
69-
throw new SpelEvaluationException(cce, SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
81+
}
82+
catch (ClassCastException ex) {
83+
throw new SpelEvaluationException(ex, SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
7084
}
7185

7286
throw new SpelEvaluationException(SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
7387
}
7488

75-
public boolean canCompare(Object left, Object right) {
76-
if (left == null || right == null) {
77-
return true;
78-
}
79-
if (left instanceof Number && right instanceof Number) {
80-
return true;
81-
}
82-
if (left instanceof Comparable) {
83-
return true;
84-
}
85-
return false;
89+
90+
private static int compare(int x, int y) {
91+
return (x < y ? -1 : (x > y ? 1 : 0));
92+
}
93+
94+
private static int compare(long x, long y) {
95+
return (x < y ? -1 : (x > y ? 1 : 0));
8696
}
8797

8898
}

spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeConverter.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import org.springframework.core.convert.ConversionException;
2020
import org.springframework.core.convert.ConversionService;
21-
import org.springframework.core.convert.ConverterNotFoundException;
2221
import org.springframework.core.convert.TypeDescriptor;
2322
import org.springframework.core.convert.support.DefaultConversionService;
2423
import org.springframework.expression.TypeConverter;
@@ -27,8 +26,8 @@
2726
import org.springframework.util.Assert;
2827

2928
/**
30-
* Default implementation of the {@link TypeConverter} interface, delegating to a core
31-
* Spring {@link ConversionService}.
29+
* Default implementation of the {@link TypeConverter} interface,
30+
* delegating to a core Spring {@link ConversionService}.
3231
*
3332
* @author Juergen Hoeller
3433
* @author Andy Clement
@@ -42,6 +41,9 @@ public class StandardTypeConverter implements TypeConverter {
4241
private final ConversionService conversionService;
4342

4443

44+
/**
45+
* Create a StandardTypeConverter for the default ConversionService.
46+
*/
4547
public StandardTypeConverter() {
4648
synchronized (this) {
4749
if (defaultConversionService == null) {
@@ -51,6 +53,10 @@ public StandardTypeConverter() {
5153
this.conversionService = defaultConversionService;
5254
}
5355

56+
/**
57+
* Create a StandardTypeConverter for the given ConversionService.
58+
* @param conversionService the ConversionService to delegate to
59+
*/
5460
public StandardTypeConverter(ConversionService conversionService) {
5561
Assert.notNull(conversionService, "ConversionService must not be null");
5662
this.conversionService = conversionService;
@@ -65,10 +71,6 @@ public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescript
6571
try {
6672
return this.conversionService.convert(value, sourceType, targetType);
6773
}
68-
catch (ConverterNotFoundException ex) {
69-
throw new SpelEvaluationException(
70-
ex, SpelMessage.TYPE_CONVERSION_ERROR, sourceType.toString(), targetType.toString());
71-
}
7274
catch (ConversionException ex) {
7375
throw new SpelEvaluationException(
7476
ex, SpelMessage.TYPE_CONVERSION_ERROR, sourceType.toString(), targetType.toString());

0 commit comments

Comments
 (0)