From 0c6b99e489689521e7661fb6ee53acdca444e827 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Wed, 3 Feb 2010 11:38:31 +0000 Subject: [PATCH] SPR-6788 - The class MediaType has a natural ordering that is inconsistent with equals, which is generally recommended or should otherwise at least be indicated in the javadoc --- .../org/springframework/http/MediaType.java | 16 +++++++-- .../springframework/http/MediaTypeTests.java | 36 ++++++++++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/org.springframework.web/src/main/java/org/springframework/http/MediaType.java b/org.springframework.web/src/main/java/org/springframework/http/MediaType.java index e86624a3e2e..0411c3b546c 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/MediaType.java +++ b/org.springframework.web/src/main/java/org/springframework/http/MediaType.java @@ -20,12 +20,12 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Comparator; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; @@ -44,7 +44,7 @@ import org.springframework.util.StringUtils; * @see HTTP 1.1, section 3.7 * @since 3.0 */ -public class MediaType { +public class MediaType implements Comparable { public static final MediaType ALL = new MediaType("*", "*"); @@ -213,6 +213,18 @@ public class MediaType { return isWildcardType(); } + /** + * Compares this {@link MediaType} to another alphabetically. + * + * @param other media type to compare to + * @see #sortBySpecificity(List) + */ + public int compareTo(MediaType other) { + String s1 = this.toString(); + String s2 = other.toString(); + return s1.compareToIgnoreCase(s2); + } + @Override public boolean equals(Object other) { if (this == other) { diff --git a/org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java b/org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java index d1180c5b407..4a315d3e6c7 100644 --- a/org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java +++ b/org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java @@ -19,8 +19,8 @@ package org.springframework.http; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collections; -import java.util.List; import java.util.Comparator; +import java.util.List; import java.util.Random; import static org.junit.Assert.*; @@ -111,6 +111,40 @@ public class MediaTypeTests { assertEquals("Invalid amount of media types", 0, mediaTypes.size()); } + @Test + public void compareTo() { + MediaType audioBasic = new MediaType("audio", "basic"); + MediaType audio = new MediaType("audio"); + MediaType audioWave = new MediaType("audio", "wave"); + MediaType audioBasicLevel = new MediaType("audio", "basic", Collections.singletonMap("level", "1")); + + // equal + assertEquals("Invalid comparison result", 0, audioBasic.compareTo(audioBasic)); + assertEquals("Invalid comparison result", 0, audio.compareTo(audio)); + assertEquals("Invalid comparison result", 0, audioBasicLevel.compareTo(audioBasicLevel)); + + assertTrue("Invalid comparison result", audioBasicLevel.compareTo(audio) > 0); + + List expected = new ArrayList(); + expected.add(audio); + expected.add(audioBasic); + expected.add(audioBasicLevel); + expected.add(audioWave); + + List result = new ArrayList(expected); + Random rnd = new Random(); + // shuffle & sort 10 times + for (int i = 0; i < 10; i++) { + Collections.shuffle(result, rnd); + Collections.sort(result); + + for (int j = 0; j < result.size(); j++) { + assertSame("Invalid media type at " + j, expected.get(j), result.get(j)); + } + } + + } + @Test public void specificityComparator() throws Exception { MediaType audioBasic = new MediaType("audio", "basic");