Rename MimeType's getCharSet() to getCharset()

Issue: SPR-14172
This commit is contained in:
Juergen Hoeller
2016-04-14 16:38:59 +02:00
parent 977734cc59
commit f1cb793ccb
25 changed files with 144 additions and 129 deletions

View File

@@ -51,19 +51,12 @@ public class MimeType implements Comparable<MimeType>, Serializable {
private static final long serialVersionUID = 4085923477777865903L;
protected static final String WILDCARD_TYPE = "*";
private static final BitSet TOKEN;
protected static final String WILDCARD_TYPE = "*";
private static final String PARAM_CHARSET = "charset";
private final String type;
private final String subtype;
private final Map<String, String> parameters;
private static final BitSet TOKEN;
static {
// variable names refer to RFC 2616, section 2.2
@@ -101,6 +94,13 @@ public class MimeType implements Comparable<MimeType>, Serializable {
}
private final String type;
private final String subtype;
private final Map<String, String> parameters;
/**
* Create a new {@code MimeType} for the given primary type.
* <p>The {@linkplain #getSubtype() subtype} is set to <code>"&#42;"</code>,
@@ -140,17 +140,12 @@ public class MimeType implements Comparable<MimeType>, Serializable {
* @param other the other media type
* @param charset the character set
* @throws IllegalArgumentException if any of the parameters contains illegal characters
* @since 4.3
*/
public MimeType(MimeType other, Charset charset) {
this(other.getType(), other.getSubtype(), addCharsetParameter(charset, other.getParameters()));
}
private static Map<String, String> addCharsetParameter(Charset charset, Map<String, String> parameters) {
Map<String, String> map = new LinkedHashMap<String, String>(parameters);
map.put(PARAM_CHARSET, charset.name());
return map;
}
/**
* Copy-constructor that copies the type and subtype of the given {@code MimeType},
* and allows for different parameter.
@@ -279,12 +274,24 @@ public class MimeType implements Comparable<MimeType>, Serializable {
/**
* Return the character set, as indicated by a {@code charset} parameter, if any.
* @return the character set, or {@code null} if not available
* @since 4.3
*/
public Charset getCharSet() {
public Charset getCharset() {
String charSet = getParameter(PARAM_CHARSET);
return (charSet != null ? Charset.forName(unquote(charSet)) : null);
}
/**
* Return the character set, as indicated by a {@code charset} parameter, if any.
* @return the character set, or {@code null} if not available
* @deprecated as of Spring 4.3, in favor of {@link #getCharset()} with its name
* aligned with the Java return type name
*/
@Deprecated
public Charset getCharSet() {
return getCharset();
}
/**
* Return a generic parameter value, given a parameter name.
* @param name the parameter name
@@ -392,6 +399,81 @@ public class MimeType implements Comparable<MimeType>, Serializable {
return false;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof MimeType)) {
return false;
}
MimeType otherType = (MimeType) other;
return (this.type.equalsIgnoreCase(otherType.type) &&
this.subtype.equalsIgnoreCase(otherType.subtype) &&
parametersAreEqual(otherType));
}
/**
* Determine if the parameters in this {@code MimeType} and the supplied
* {@code MimeType} are equal, performing case-insensitive comparisons
* for {@link Charset}s.
* @since 4.2
*/
private boolean parametersAreEqual(MimeType other) {
if (this.parameters.size() != other.parameters.size()) {
return false;
}
for (String key : this.parameters.keySet()) {
if (!other.parameters.containsKey(key)) {
return false;
}
if (PARAM_CHARSET.equals(key)) {
if (!ObjectUtils.nullSafeEquals(getCharset(), other.getCharset())) {
return false;
}
}
else if (!ObjectUtils.nullSafeEquals(this.parameters.get(key), other.parameters.get(key))) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
int result = this.type.hashCode();
result = 31 * result + this.subtype.hashCode();
result = 31 * result + this.parameters.hashCode();
return result;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
appendTo(builder);
return builder.toString();
}
protected void appendTo(StringBuilder builder) {
builder.append(this.type);
builder.append('/');
builder.append(this.subtype);
appendTo(this.parameters, builder);
}
private void appendTo(Map<String, String> map, StringBuilder builder) {
for (Map.Entry<String, String> entry : map.entrySet()) {
builder.append(';');
builder.append(entry.getKey());
builder.append('=');
builder.append(entry.getValue());
}
}
/**
* Compares this {@code MediaType} to another alphabetically.
* @param other media type to compare to
@@ -437,79 +519,6 @@ public class MimeType implements Comparable<MimeType>, Serializable {
return 0;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof MimeType)) {
return false;
}
MimeType otherType = (MimeType) other;
return (this.type.equalsIgnoreCase(otherType.type) &&
this.subtype.equalsIgnoreCase(otherType.subtype) &&
parametersAreEqual(otherType));
}
/**
* Determine if the parameters in this {@code MimeType} and the supplied
* {@code MimeType} are equal, performing case-insensitive comparisons
* for {@link Charset}s.
* @since 4.2
*/
private boolean parametersAreEqual(MimeType that) {
if (this.parameters.size() != that.parameters.size()) {
return false;
}
for (String key : this.parameters.keySet()) {
if (!that.parameters.containsKey(key)) {
return false;
}
if (PARAM_CHARSET.equals(key)) {
if (!ObjectUtils.nullSafeEquals(this.getCharSet(), that.getCharSet())) {
return false;
}
}
else if (!ObjectUtils.nullSafeEquals(this.parameters.get(key), that.parameters.get(key))) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
int result = this.type.hashCode();
result = 31 * result + this.subtype.hashCode();
result = 31 * result + this.parameters.hashCode();
return result;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
appendTo(builder);
return builder.toString();
}
protected void appendTo(StringBuilder builder) {
builder.append(this.type);
builder.append('/');
builder.append(this.subtype);
appendTo(this.parameters, builder);
}
private void appendTo(Map<String, String> map, StringBuilder builder) {
for (Map.Entry<String, String> entry : map.entrySet()) {
builder.append(';');
builder.append(entry.getKey());
builder.append('=');
builder.append(entry.getValue());
}
}
/**
* Parse the given String value into a {@code MimeType} object,
@@ -521,6 +530,12 @@ public class MimeType implements Comparable<MimeType>, Serializable {
return MimeTypeUtils.parseMimeType(value);
}
private static Map<String, String> addCharsetParameter(Charset charset, Map<String, String> parameters) {
Map<String, String> map = new LinkedHashMap<String, String>(parameters);
map.put(PARAM_CHARSET, charset.name());
return map;
}
public static class SpecificityComparator<T extends MimeType> implements Comparator<T> {