1
1
/*
2
- * Copyright 2002-2016 the original author or authors.
2
+ * Copyright 2002-2017 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.
16
16
17
17
package org .springframework .beans .support ;
18
18
19
- import org . junit . Test ;
19
+ import java . util . Comparator ;
20
20
21
- import org .springframework . util . comparator . CompoundComparator ;
21
+ import org .junit . Test ;
22
22
23
23
import static org .junit .Assert .*;
24
24
25
25
/**
26
- * Unit tests for {@link PropertyComparator}
27
- *
28
- * @see org.springframework.util.comparator.ComparatorTests
26
+ * Unit tests for {@link PropertyComparator}.
29
27
*
30
28
* @author Keith Donald
31
29
* @author Chris Beams
@@ -40,7 +38,7 @@ public void testPropertyComparator() {
40
38
Dog dog2 = new Dog ();
41
39
dog2 .setNickName ("biscy" );
42
40
43
- PropertyComparator c = new PropertyComparator ("nickName" , false , true );
41
+ PropertyComparator < Dog > c = new PropertyComparator <> ("nickName" , false , true );
44
42
assertTrue (c .compare (dog , dog2 ) > 0 );
45
43
assertTrue (c .compare (dog , dog ) == 0 );
46
44
assertTrue (c .compare (dog2 , dog ) < 0 );
@@ -50,15 +48,13 @@ public void testPropertyComparator() {
50
48
public void testPropertyComparatorNulls () {
51
49
Dog dog = new Dog ();
52
50
Dog dog2 = new Dog ();
53
- PropertyComparator c = new PropertyComparator ("nickName" , false , true );
51
+ PropertyComparator < Dog > c = new PropertyComparator <> ("nickName" , false , true );
54
52
assertTrue (c .compare (dog , dog2 ) == 0 );
55
53
}
56
54
57
- @ SuppressWarnings ("unchecked" )
58
55
@ Test
59
56
public void testCompoundComparator () {
60
- CompoundComparator <Dog > c = new CompoundComparator <>();
61
- c .addComparator (new PropertyComparator ("lastName" , false , true ));
57
+ Comparator <Dog > c = new PropertyComparator <>("lastName" , false , true );
62
58
63
59
Dog dog1 = new Dog ();
64
60
dog1 .setFirstName ("macy" );
@@ -70,19 +66,17 @@ public void testCompoundComparator() {
70
66
71
67
assertTrue (c .compare (dog1 , dog2 ) == 0 );
72
68
73
- c . addComparator (new PropertyComparator ("firstName" , false , true ));
69
+ c = c . thenComparing (new PropertyComparator <> ("firstName" , false , true ));
74
70
assertTrue (c .compare (dog1 , dog2 ) > 0 );
75
71
76
72
dog2 .setLastName ("konikk dog" );
77
73
assertTrue (c .compare (dog2 , dog1 ) > 0 );
78
74
}
79
75
80
- @ SuppressWarnings ("unchecked" )
81
76
@ Test
82
77
public void testCompoundComparatorInvert () {
83
- CompoundComparator <Dog > c = new CompoundComparator <>();
84
- c .addComparator (new PropertyComparator ("lastName" , false , true ));
85
- c .addComparator (new PropertyComparator ("firstName" , false , true ));
78
+ Comparator <Dog > c = (new PropertyComparator <Dog >("lastName" , false , true )).
79
+ thenComparing (new PropertyComparator <>("firstName" , false , true ));
86
80
Dog dog1 = new Dog ();
87
81
dog1 .setFirstName ("macy" );
88
82
dog1 .setLastName ("grayspots" );
@@ -92,7 +86,7 @@ public void testCompoundComparatorInvert() {
92
86
dog2 .setLastName ("grayspots" );
93
87
94
88
assertTrue (c .compare (dog1 , dog2 ) > 0 );
95
- c . invertOrder ();
89
+ c = c . reversed ();
96
90
assertTrue (c .compare (dog1 , dog2 ) < 0 );
97
91
}
98
92
@@ -106,11 +100,6 @@ private static class Dog implements Comparable<Object> {
106
100
107
101
private String lastName ;
108
102
109
- @ Override
110
- public int compareTo (Object o ) {
111
- return nickName .compareTo (((Dog )o ).nickName );
112
- }
113
-
114
103
public String getNickName () {
115
104
return nickName ;
116
105
}
@@ -134,6 +123,11 @@ public String getLastName() {
134
123
public void setLastName (String lastName ) {
135
124
this .lastName = lastName ;
136
125
}
126
+
127
+ @ Override
128
+ public int compareTo (Object o ) {
129
+ return this .nickName .compareTo (((Dog ) o ).nickName );
130
+ }
137
131
}
138
132
139
133
}
0 commit comments