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
This commit is contained in:
parent
bd018fc9d7
commit
9821868707
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -37,6 +37,7 @@ import org.springframework.util.Assert;
|
|||
* @author Juergen Hoeller
|
||||
* @since 1.2.2
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class CompoundComparator<T> implements Comparator<T>, Serializable {
|
||||
|
||||
private final List<InvertibleComparator<T>> comparators;
|
||||
|
@ -58,10 +59,12 @@ public class CompoundComparator<T> implements Comparator<T>, Serializable {
|
|||
* @param comparators the comparators to build into a compound comparator
|
||||
* @see InvertibleComparator
|
||||
*/
|
||||
public CompoundComparator(Comparator[] comparators) {
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public CompoundComparator(Comparator... comparators) {
|
||||
Assert.notNull(comparators, "Comparators must not be null");
|
||||
this.comparators = new ArrayList<InvertibleComparator<T>>(comparators.length);
|
||||
for (Comparator<T> comparator : comparators) {
|
||||
addComparator(comparator);
|
||||
for (Comparator comparator : comparators) {
|
||||
this.addComparator(comparator);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -123,7 +126,7 @@ public class CompoundComparator<T> implements Comparator<T>, Serializable {
|
|||
* comparator.
|
||||
*/
|
||||
public void invertOrder() {
|
||||
for (InvertibleComparator comparator : this.comparators) {
|
||||
for (InvertibleComparator<T> comparator : this.comparators) {
|
||||
comparator.invertOrder();
|
||||
}
|
||||
}
|
||||
|
@ -159,7 +162,6 @@ public class CompoundComparator<T> implements Comparator<T>, Serializable {
|
|||
return this.comparators.size();
|
||||
}
|
||||
|
||||
|
||||
public int compare(T o1, T o2) {
|
||||
Assert.state(this.comparators.size() > 0,
|
||||
"No sort definitions have been added to this CompoundComparator to compare");
|
||||
|
@ -173,6 +175,7 @@ public class CompoundComparator<T> implements Comparator<T>, Serializable {
|
|||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
|
@ -180,7 +183,7 @@ public class CompoundComparator<T> implements Comparator<T>, Serializable {
|
|||
if (!(obj instanceof CompoundComparator)) {
|
||||
return false;
|
||||
}
|
||||
CompoundComparator other = (CompoundComparator) obj;
|
||||
CompoundComparator<T> other = (CompoundComparator<T>) obj;
|
||||
return this.comparators.equals(other.comparators);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -19,15 +19,18 @@ package org.springframework.util.comparator;
|
|||
import java.io.Serializable;
|
||||
import java.util.Comparator;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A decorator for a comparator, with an "ascending" flag denoting
|
||||
* whether comparison results should be treated in forward (standard
|
||||
* ascending) order or flipped for reverse (descending) order.
|
||||
*
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.2.2
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class InvertibleComparator<T> implements Comparator<T>, Serializable {
|
||||
|
||||
private final Comparator<T> comparator;
|
||||
|
@ -41,6 +44,7 @@ public class InvertibleComparator<T> implements Comparator<T>, Serializable {
|
|||
* @param comparator the comparator to decorate
|
||||
*/
|
||||
public InvertibleComparator(Comparator<T> comparator) {
|
||||
Assert.notNull(comparator, "Comparator must not be null");
|
||||
this.comparator = comparator;
|
||||
}
|
||||
|
||||
|
@ -51,6 +55,7 @@ public class InvertibleComparator<T> implements Comparator<T>, Serializable {
|
|||
* @param ascending the sort order: ascending (true) or descending (false)
|
||||
*/
|
||||
public InvertibleComparator(Comparator<T> comparator, boolean ascending) {
|
||||
Assert.notNull(comparator, "Comparator must not be null");
|
||||
this.comparator = comparator;
|
||||
setAscending(ascending);
|
||||
}
|
||||
|
@ -97,6 +102,7 @@ public class InvertibleComparator<T> implements Comparator<T>, Serializable {
|
|||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
|
@ -104,7 +110,7 @@ public class InvertibleComparator<T> implements Comparator<T>, Serializable {
|
|||
if (!(obj instanceof InvertibleComparator)) {
|
||||
return false;
|
||||
}
|
||||
InvertibleComparator other = (InvertibleComparator) obj;
|
||||
InvertibleComparator<T> other = (InvertibleComparator<T>) obj;
|
||||
return (this.comparator.equals(other.comparator) && this.ascending == other.ascending);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -23,7 +23,7 @@ import org.springframework.util.Assert;
|
|||
/**
|
||||
* A Comparator that will safely compare nulls to be lower or higher than
|
||||
* other objects. Can decorate a given Comparator or work on Comparables.
|
||||
*
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @author Juergen Hoeller
|
||||
* @since 1.2.2
|
||||
|
@ -35,14 +35,15 @@ public class NullSafeComparator<T> implements Comparator<T> {
|
|||
* A shared default instance of this comparator, treating nulls lower
|
||||
* than non-null objects.
|
||||
*/
|
||||
public static final NullSafeComparator NULLS_LOW = new NullSafeComparator(true);
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static final NullSafeComparator NULLS_LOW = new NullSafeComparator<Object>(true);
|
||||
|
||||
/**
|
||||
* A shared default instance of this comparator, treating nulls higher
|
||||
* than non-null objects.
|
||||
*/
|
||||
public static final NullSafeComparator NULLS_HIGH = new NullSafeComparator(false);
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static final NullSafeComparator NULLS_HIGH = new NullSafeComparator<Object>(false);
|
||||
|
||||
private final Comparator<T> nonNullComparator;
|
||||
|
||||
|
@ -63,7 +64,7 @@ public class NullSafeComparator<T> implements Comparator<T> {
|
|||
* @see #NULLS_LOW
|
||||
* @see #NULLS_HIGH
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private NullSafeComparator(boolean nullsLow) {
|
||||
this.nonNullComparator = new ComparableComparator();
|
||||
this.nullsLow = nullsLow;
|
||||
|
@ -99,6 +100,7 @@ public class NullSafeComparator<T> implements Comparator<T> {
|
|||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
|
@ -106,7 +108,7 @@ public class NullSafeComparator<T> implements Comparator<T> {
|
|||
if (!(obj instanceof NullSafeComparator)) {
|
||||
return false;
|
||||
}
|
||||
NullSafeComparator other = (NullSafeComparator) obj;
|
||||
NullSafeComparator<T> other = (NullSafeComparator<T>) obj;
|
||||
return (this.nonNullComparator.equals(other.nonNullComparator) && this.nullsLow == other.nullsLow);
|
||||
}
|
||||
|
||||
|
|
|
@ -814,10 +814,8 @@ public class MediaType implements Comparable<MediaType> {
|
|||
public static void sortBySpecificityAndQuality(List<MediaType> mediaTypes) {
|
||||
Assert.notNull(mediaTypes, "'mediaTypes' must not be null");
|
||||
if (mediaTypes.size() > 1) {
|
||||
Comparator<?>[] comparators = new Comparator[2];
|
||||
comparators[0] = MediaType.SPECIFICITY_COMPARATOR;
|
||||
comparators[1] = MediaType.QUALITY_VALUE_COMPARATOR;
|
||||
Collections.sort(mediaTypes, new CompoundComparator<MediaType>(comparators));
|
||||
Collections.sort(mediaTypes, new CompoundComparator<MediaType>(
|
||||
MediaType.SPECIFICITY_COMPARATOR, MediaType.QUALITY_VALUE_COMPARATOR));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue