More accurate checks for presence of MediaType.ALL
Typically a straight up equals as well as Collections#contains checks for MediaType.ALL is susceptible to the presence of media type parameters. This commits adds equalsTypeAndSubtype as well as an isPresentIn(Collection<MimeType>) methods to MimeType to faciliate with checks for MediaType.ALL. Issue: SPR-17550
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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,6 +19,7 @@ package org.springframework.util;
|
||||
import java.io.Serializable;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.BitSet;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
@@ -383,6 +384,37 @@ public class MimeType implements Comparable<MimeType>, Serializable {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Similar to {@link #equals(Object)} but based on the type and subtype
|
||||
* only, i.e. ignoring parameters.
|
||||
* @param other the other mime type to compare to
|
||||
* @return whether the two mime types have the same type and subtype
|
||||
* @since 5.1.4
|
||||
*/
|
||||
public boolean equalsTypeAndSubtype(@Nullable MimeType other) {
|
||||
if (other == null) {
|
||||
return false;
|
||||
}
|
||||
return this.type.equalsIgnoreCase(other.type) && this.subtype.equalsIgnoreCase(other.subtype);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlike {@link Collection#contains(Object)} which relies on
|
||||
* {@link MimeType#equals(Object)}, this method only checks the type and the
|
||||
* subtype, but otherwise ignores parameters.
|
||||
* @param mimeTypes the list of mime types to perform the check against
|
||||
* @return whether the list contains the given mime type
|
||||
* @since 5.1.4
|
||||
*/
|
||||
public boolean isPresentIn(Collection<? extends MimeType> mimeTypes) {
|
||||
for (MimeType mimeType : mimeTypes) {
|
||||
if (mimeType.equalsTypeAndSubtype(this)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
|
||||
Reference in New Issue
Block a user