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
This commit is contained in:
parent
a44ec9f4f7
commit
0c6b99e489
|
|
@ -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 <a href="http://tools.ietf.org/html/rfc2616#section-3.7">HTTP 1.1, section 3.7</a>
|
||||
* @since 3.0
|
||||
*/
|
||||
public class MediaType {
|
||||
public class MediaType implements Comparable<MediaType> {
|
||||
|
||||
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) {
|
||||
|
|
|
|||
|
|
@ -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<MediaType> expected = new ArrayList<MediaType>();
|
||||
expected.add(audio);
|
||||
expected.add(audioBasic);
|
||||
expected.add(audioBasicLevel);
|
||||
expected.add(audioWave);
|
||||
|
||||
List<MediaType> result = new ArrayList<MediaType>(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");
|
||||
|
|
|
|||
Loading…
Reference in New Issue