Skip to content

Commit 9821868

Browse files
philwebbcbeams
authored andcommitted
Refactor and polish various Comparator impls
- Refactor CompoundComparator constructor to use varargs - Refactor MediaType to consume new varargs constructor - Add notNull assertions where appropriate - Add generic typing where appropriate - Suppress generics warnings elsewhere - Fix whitespace errors
1 parent bd018fc commit 9821868

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

spring-core/src/main/java/org/springframework/util/comparator/CompoundComparator.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -37,6 +37,7 @@
3737
* @author Juergen Hoeller
3838
* @since 1.2.2
3939
*/
40+
@SuppressWarnings("serial")
4041
public class CompoundComparator<T> implements Comparator<T>, Serializable {
4142

4243
private final List<InvertibleComparator<T>> comparators;
@@ -58,10 +59,12 @@ public CompoundComparator() {
5859
* @param comparators the comparators to build into a compound comparator
5960
* @see InvertibleComparator
6061
*/
61-
public CompoundComparator(Comparator[] comparators) {
62+
@SuppressWarnings({ "unchecked", "rawtypes" })
63+
public CompoundComparator(Comparator... comparators) {
64+
Assert.notNull(comparators, "Comparators must not be null");
6265
this.comparators = new ArrayList<InvertibleComparator<T>>(comparators.length);
63-
for (Comparator<T> comparator : comparators) {
64-
addComparator(comparator);
66+
for (Comparator comparator : comparators) {
67+
this.addComparator(comparator);
6568
}
6669
}
6770

@@ -123,7 +126,7 @@ public void setComparator(int index, Comparator<T> comparator, boolean ascending
123126
* comparator.
124127
*/
125128
public void invertOrder() {
126-
for (InvertibleComparator comparator : this.comparators) {
129+
for (InvertibleComparator<T> comparator : this.comparators) {
127130
comparator.invertOrder();
128131
}
129132
}
@@ -159,7 +162,6 @@ public int getComparatorCount() {
159162
return this.comparators.size();
160163
}
161164

162-
163165
public int compare(T o1, T o2) {
164166
Assert.state(this.comparators.size() > 0,
165167
"No sort definitions have been added to this CompoundComparator to compare");
@@ -173,14 +175,15 @@ public int compare(T o1, T o2) {
173175
}
174176

175177
@Override
178+
@SuppressWarnings("unchecked")
176179
public boolean equals(Object obj) {
177180
if (this == obj) {
178181
return true;
179182
}
180183
if (!(obj instanceof CompoundComparator)) {
181184
return false;
182185
}
183-
CompoundComparator other = (CompoundComparator) obj;
186+
CompoundComparator<T> other = (CompoundComparator<T>) obj;
184187
return this.comparators.equals(other.comparators);
185188
}
186189

spring-core/src/main/java/org/springframework/util/comparator/InvertibleComparator.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2005 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -19,15 +19,18 @@
1919
import java.io.Serializable;
2020
import java.util.Comparator;
2121

22+
import org.springframework.util.Assert;
23+
2224
/**
2325
* A decorator for a comparator, with an "ascending" flag denoting
2426
* whether comparison results should be treated in forward (standard
2527
* ascending) order or flipped for reverse (descending) order.
26-
*
28+
*
2729
* @author Keith Donald
2830
* @author Juergen Hoeller
2931
* @since 1.2.2
3032
*/
33+
@SuppressWarnings("serial")
3134
public class InvertibleComparator<T> implements Comparator<T>, Serializable {
3235

3336
private final Comparator<T> comparator;
@@ -41,6 +44,7 @@ public class InvertibleComparator<T> implements Comparator<T>, Serializable {
4144
* @param comparator the comparator to decorate
4245
*/
4346
public InvertibleComparator(Comparator<T> comparator) {
47+
Assert.notNull(comparator, "Comparator must not be null");
4448
this.comparator = comparator;
4549
}
4650

@@ -51,6 +55,7 @@ public InvertibleComparator(Comparator<T> comparator) {
5155
* @param ascending the sort order: ascending (true) or descending (false)
5256
*/
5357
public InvertibleComparator(Comparator<T> comparator, boolean ascending) {
58+
Assert.notNull(comparator, "Comparator must not be null");
5459
this.comparator = comparator;
5560
setAscending(ascending);
5661
}
@@ -97,14 +102,15 @@ public int compare(T o1, T o2) {
97102
}
98103

99104
@Override
105+
@SuppressWarnings("unchecked")
100106
public boolean equals(Object obj) {
101107
if (this == obj) {
102108
return true;
103109
}
104110
if (!(obj instanceof InvertibleComparator)) {
105111
return false;
106112
}
107-
InvertibleComparator other = (InvertibleComparator) obj;
113+
InvertibleComparator<T> other = (InvertibleComparator<T>) obj;
108114
return (this.comparator.equals(other.comparator) && this.ascending == other.ascending);
109115
}
110116

spring-core/src/main/java/org/springframework/util/comparator/NullSafeComparator.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2005 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -23,7 +23,7 @@
2323
/**
2424
* A Comparator that will safely compare nulls to be lower or higher than
2525
* other objects. Can decorate a given Comparator or work on Comparables.
26-
*
26+
*
2727
* @author Keith Donald
2828
* @author Juergen Hoeller
2929
* @since 1.2.2
@@ -35,14 +35,15 @@ public class NullSafeComparator<T> implements Comparator<T> {
3535
* A shared default instance of this comparator, treating nulls lower
3636
* than non-null objects.
3737
*/
38-
public static final NullSafeComparator NULLS_LOW = new NullSafeComparator(true);
38+
@SuppressWarnings("rawtypes")
39+
public static final NullSafeComparator NULLS_LOW = new NullSafeComparator<Object>(true);
3940

4041
/**
4142
* A shared default instance of this comparator, treating nulls higher
4243
* than non-null objects.
4344
*/
44-
public static final NullSafeComparator NULLS_HIGH = new NullSafeComparator(false);
45-
45+
@SuppressWarnings("rawtypes")
46+
public static final NullSafeComparator NULLS_HIGH = new NullSafeComparator<Object>(false);
4647

4748
private final Comparator<T> nonNullComparator;
4849

@@ -63,7 +64,7 @@ public class NullSafeComparator<T> implements Comparator<T> {
6364
* @see #NULLS_LOW
6465
* @see #NULLS_HIGH
6566
*/
66-
@SuppressWarnings("unchecked")
67+
@SuppressWarnings({ "unchecked", "rawtypes" })
6768
private NullSafeComparator(boolean nullsLow) {
6869
this.nonNullComparator = new ComparableComparator();
6970
this.nullsLow = nullsLow;
@@ -99,14 +100,15 @@ public int compare(T o1, T o2) {
99100
}
100101

101102
@Override
103+
@SuppressWarnings("unchecked")
102104
public boolean equals(Object obj) {
103105
if (this == obj) {
104106
return true;
105107
}
106108
if (!(obj instanceof NullSafeComparator)) {
107109
return false;
108110
}
109-
NullSafeComparator other = (NullSafeComparator) obj;
111+
NullSafeComparator<T> other = (NullSafeComparator<T>) obj;
110112
return (this.nonNullComparator.equals(other.nonNullComparator) && this.nullsLow == other.nullsLow);
111113
}
112114

spring-web/src/main/java/org/springframework/http/MediaType.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -814,10 +814,8 @@ public static void sortByQualityValue(List<MediaType> mediaTypes) {
814814
public static void sortBySpecificityAndQuality(List<MediaType> mediaTypes) {
815815
Assert.notNull(mediaTypes, "'mediaTypes' must not be null");
816816
if (mediaTypes.size() > 1) {
817-
Comparator<?>[] comparators = new Comparator[2];
818-
comparators[0] = MediaType.SPECIFICITY_COMPARATOR;
819-
comparators[1] = MediaType.QUALITY_VALUE_COMPARATOR;
820-
Collections.sort(mediaTypes, new CompoundComparator<MediaType>(comparators));
817+
Collections.sort(mediaTypes, new CompoundComparator<MediaType>(
818+
MediaType.SPECIFICITY_COMPARATOR, MediaType.QUALITY_VALUE_COMPARATOR));
821819
}
822820
}
823821

0 commit comments

Comments
 (0)