diff --git a/spring-core/src/main/java/org/springframework/util/comparator/CompoundComparator.java b/spring-core/src/main/java/org/springframework/util/comparator/CompoundComparator.java index 8faf091ce8..4e1a6189af 100644 --- a/spring-core/src/main/java/org/springframework/util/comparator/CompoundComparator.java +++ b/spring-core/src/main/java/org/springframework/util/comparator/CompoundComparator.java @@ -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 implements Comparator, Serializable { private final List> comparators; @@ -58,10 +59,12 @@ public class CompoundComparator implements Comparator, 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>(comparators.length); - for (Comparator comparator : comparators) { - addComparator(comparator); + for (Comparator comparator : comparators) { + this.addComparator(comparator); } } @@ -123,7 +126,7 @@ public class CompoundComparator implements Comparator, Serializable { * comparator. */ public void invertOrder() { - for (InvertibleComparator comparator : this.comparators) { + for (InvertibleComparator comparator : this.comparators) { comparator.invertOrder(); } } @@ -159,7 +162,6 @@ public class CompoundComparator implements Comparator, 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 implements Comparator, Serializable { } @Override + @SuppressWarnings("unchecked") public boolean equals(Object obj) { if (this == obj) { return true; @@ -180,7 +183,7 @@ public class CompoundComparator implements Comparator, Serializable { if (!(obj instanceof CompoundComparator)) { return false; } - CompoundComparator other = (CompoundComparator) obj; + CompoundComparator other = (CompoundComparator) obj; return this.comparators.equals(other.comparators); } diff --git a/spring-core/src/main/java/org/springframework/util/comparator/InvertibleComparator.java b/spring-core/src/main/java/org/springframework/util/comparator/InvertibleComparator.java index 6fd14f453f..f78e56ff5d 100644 --- a/spring-core/src/main/java/org/springframework/util/comparator/InvertibleComparator.java +++ b/spring-core/src/main/java/org/springframework/util/comparator/InvertibleComparator.java @@ -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 implements Comparator, Serializable { private final Comparator comparator; @@ -41,6 +44,7 @@ public class InvertibleComparator implements Comparator, Serializable { * @param comparator the comparator to decorate */ public InvertibleComparator(Comparator comparator) { + Assert.notNull(comparator, "Comparator must not be null"); this.comparator = comparator; } @@ -51,6 +55,7 @@ public class InvertibleComparator implements Comparator, Serializable { * @param ascending the sort order: ascending (true) or descending (false) */ public InvertibleComparator(Comparator comparator, boolean ascending) { + Assert.notNull(comparator, "Comparator must not be null"); this.comparator = comparator; setAscending(ascending); } @@ -97,6 +102,7 @@ public class InvertibleComparator implements Comparator, Serializable { } @Override + @SuppressWarnings("unchecked") public boolean equals(Object obj) { if (this == obj) { return true; @@ -104,7 +110,7 @@ public class InvertibleComparator implements Comparator, Serializable { if (!(obj instanceof InvertibleComparator)) { return false; } - InvertibleComparator other = (InvertibleComparator) obj; + InvertibleComparator other = (InvertibleComparator) obj; return (this.comparator.equals(other.comparator) && this.ascending == other.ascending); } diff --git a/spring-core/src/main/java/org/springframework/util/comparator/NullSafeComparator.java b/spring-core/src/main/java/org/springframework/util/comparator/NullSafeComparator.java index b2e761ebb6..eeb11e1c41 100644 --- a/spring-core/src/main/java/org/springframework/util/comparator/NullSafeComparator.java +++ b/spring-core/src/main/java/org/springframework/util/comparator/NullSafeComparator.java @@ -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 implements Comparator { * 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(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(false); private final Comparator nonNullComparator; @@ -63,7 +64,7 @@ public class NullSafeComparator implements Comparator { * @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 implements Comparator { } @Override + @SuppressWarnings("unchecked") public boolean equals(Object obj) { if (this == obj) { return true; @@ -106,7 +108,7 @@ public class NullSafeComparator implements Comparator { if (!(obj instanceof NullSafeComparator)) { return false; } - NullSafeComparator other = (NullSafeComparator) obj; + NullSafeComparator other = (NullSafeComparator) obj; return (this.nonNullComparator.equals(other.nonNullComparator) && this.nullsLow == other.nullsLow); } diff --git a/spring-web/src/main/java/org/springframework/http/MediaType.java b/spring-web/src/main/java/org/springframework/http/MediaType.java index 4cc037b8c6..f2a2fb8a2d 100644 --- a/spring-web/src/main/java/org/springframework/http/MediaType.java +++ b/spring-web/src/main/java/org/springframework/http/MediaType.java @@ -814,10 +814,8 @@ public class MediaType implements Comparable { public static void sortBySpecificityAndQuality(List 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(comparators)); + Collections.sort(mediaTypes, new CompoundComparator( + MediaType.SPECIFICITY_COMPARATOR, MediaType.QUALITY_VALUE_COMPARATOR)); } }