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:
Phillip Webb 2012-08-27 13:20:24 -07:00 committed by Chris Beams
parent bd018fc9d7
commit 9821868707
4 changed files with 30 additions and 21 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 * @author Juergen Hoeller
* @since 1.2.2 * @since 1.2.2
*/ */
@SuppressWarnings("serial")
public class CompoundComparator<T> implements Comparator<T>, Serializable { public class CompoundComparator<T> implements Comparator<T>, Serializable {
private final List<InvertibleComparator<T>> comparators; 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 * @param comparators the comparators to build into a compound comparator
* @see InvertibleComparator * @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); this.comparators = new ArrayList<InvertibleComparator<T>>(comparators.length);
for (Comparator<T> comparator : comparators) { for (Comparator comparator : comparators) {
addComparator(comparator); this.addComparator(comparator);
} }
} }
@ -123,7 +126,7 @@ public class CompoundComparator<T> implements Comparator<T>, Serializable {
* comparator. * comparator.
*/ */
public void invertOrder() { public void invertOrder() {
for (InvertibleComparator comparator : this.comparators) { for (InvertibleComparator<T> comparator : this.comparators) {
comparator.invertOrder(); comparator.invertOrder();
} }
} }
@ -159,7 +162,6 @@ public class CompoundComparator<T> implements Comparator<T>, Serializable {
return this.comparators.size(); return this.comparators.size();
} }
public int compare(T o1, T o2) { public int compare(T o1, T o2) {
Assert.state(this.comparators.size() > 0, Assert.state(this.comparators.size() > 0,
"No sort definitions have been added to this CompoundComparator to compare"); "No sort definitions have been added to this CompoundComparator to compare");
@ -173,6 +175,7 @@ public class CompoundComparator<T> implements Comparator<T>, Serializable {
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) { if (this == obj) {
return true; return true;
@ -180,7 +183,7 @@ public class CompoundComparator<T> implements Comparator<T>, Serializable {
if (!(obj instanceof CompoundComparator)) { if (!(obj instanceof CompoundComparator)) {
return false; return false;
} }
CompoundComparator other = (CompoundComparator) obj; CompoundComparator<T> other = (CompoundComparator<T>) obj;
return this.comparators.equals(other.comparators); return this.comparators.equals(other.comparators);
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.io.Serializable;
import java.util.Comparator; import java.util.Comparator;
import org.springframework.util.Assert;
/** /**
* A decorator for a comparator, with an "ascending" flag denoting * A decorator for a comparator, with an "ascending" flag denoting
* whether comparison results should be treated in forward (standard * whether comparison results should be treated in forward (standard
* ascending) order or flipped for reverse (descending) order. * ascending) order or flipped for reverse (descending) order.
* *
* @author Keith Donald * @author Keith Donald
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 1.2.2 * @since 1.2.2
*/ */
@SuppressWarnings("serial")
public class InvertibleComparator<T> implements Comparator<T>, Serializable { public class InvertibleComparator<T> implements Comparator<T>, Serializable {
private final Comparator<T> comparator; private final Comparator<T> comparator;
@ -41,6 +44,7 @@ public class InvertibleComparator<T> implements Comparator<T>, Serializable {
* @param comparator the comparator to decorate * @param comparator the comparator to decorate
*/ */
public InvertibleComparator(Comparator<T> comparator) { public InvertibleComparator(Comparator<T> comparator) {
Assert.notNull(comparator, "Comparator must not be null");
this.comparator = comparator; 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) * @param ascending the sort order: ascending (true) or descending (false)
*/ */
public InvertibleComparator(Comparator<T> comparator, boolean ascending) { public InvertibleComparator(Comparator<T> comparator, boolean ascending) {
Assert.notNull(comparator, "Comparator must not be null");
this.comparator = comparator; this.comparator = comparator;
setAscending(ascending); setAscending(ascending);
} }
@ -97,6 +102,7 @@ public class InvertibleComparator<T> implements Comparator<T>, Serializable {
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) { if (this == obj) {
return true; return true;
@ -104,7 +110,7 @@ public class InvertibleComparator<T> implements Comparator<T>, Serializable {
if (!(obj instanceof InvertibleComparator)) { if (!(obj instanceof InvertibleComparator)) {
return false; return false;
} }
InvertibleComparator other = (InvertibleComparator) obj; InvertibleComparator<T> other = (InvertibleComparator<T>) obj;
return (this.comparator.equals(other.comparator) && this.ascending == other.ascending); return (this.comparator.equals(other.comparator) && this.ascending == other.ascending);
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 * A Comparator that will safely compare nulls to be lower or higher than
* other objects. Can decorate a given Comparator or work on Comparables. * other objects. Can decorate a given Comparator or work on Comparables.
* *
* @author Keith Donald * @author Keith Donald
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 1.2.2 * @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 * A shared default instance of this comparator, treating nulls lower
* than non-null objects. * 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 * A shared default instance of this comparator, treating nulls higher
* than non-null objects. * 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; private final Comparator<T> nonNullComparator;
@ -63,7 +64,7 @@ public class NullSafeComparator<T> implements Comparator<T> {
* @see #NULLS_LOW * @see #NULLS_LOW
* @see #NULLS_HIGH * @see #NULLS_HIGH
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings({ "unchecked", "rawtypes" })
private NullSafeComparator(boolean nullsLow) { private NullSafeComparator(boolean nullsLow) {
this.nonNullComparator = new ComparableComparator(); this.nonNullComparator = new ComparableComparator();
this.nullsLow = nullsLow; this.nullsLow = nullsLow;
@ -99,6 +100,7 @@ public class NullSafeComparator<T> implements Comparator<T> {
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) { if (this == obj) {
return true; return true;
@ -106,7 +108,7 @@ public class NullSafeComparator<T> implements Comparator<T> {
if (!(obj instanceof NullSafeComparator)) { if (!(obj instanceof NullSafeComparator)) {
return false; return false;
} }
NullSafeComparator other = (NullSafeComparator) obj; NullSafeComparator<T> other = (NullSafeComparator<T>) obj;
return (this.nonNullComparator.equals(other.nonNullComparator) && this.nullsLow == other.nullsLow); return (this.nonNullComparator.equals(other.nonNullComparator) && this.nullsLow == other.nullsLow);
} }

View File

@ -814,10 +814,8 @@ public class MediaType implements Comparable<MediaType> {
public static void sortBySpecificityAndQuality(List<MediaType> mediaTypes) { public static void sortBySpecificityAndQuality(List<MediaType> mediaTypes) {
Assert.notNull(mediaTypes, "'mediaTypes' must not be null"); Assert.notNull(mediaTypes, "'mediaTypes' must not be null");
if (mediaTypes.size() > 1) { if (mediaTypes.size() > 1) {
Comparator<?>[] comparators = new Comparator[2]; Collections.sort(mediaTypes, new CompoundComparator<MediaType>(
comparators[0] = MediaType.SPECIFICITY_COMPARATOR; MediaType.SPECIFICITY_COMPARATOR, MediaType.QUALITY_VALUE_COMPARATOR));
comparators[1] = MediaType.QUALITY_VALUE_COMPARATOR;
Collections.sort(mediaTypes, new CompoundComparator<MediaType>(comparators));
} }
} }