Skip to content

Commit

Permalink
Touch-ups
Browse files Browse the repository at this point in the history
  • Loading branch information
mizosoft committed May 21, 2024
1 parent 982a46b commit 5a772a1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
48 changes: 24 additions & 24 deletions methanol/src/main/java/com/github/mizosoft/methanol/MediaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import static com.github.mizosoft.methanol.internal.Utils.requireValidToken;
import static com.github.mizosoft.methanol.internal.Validate.requireArgument;
import static com.github.mizosoft.methanol.internal.text.HttpCharMatchers.QUOTED_PAIR_MATCHER;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

import com.github.mizosoft.methanol.internal.text.HeaderValueTokenizer;
Expand Down Expand Up @@ -135,7 +134,7 @@ public final class MediaType {
private @MonotonicNonNull Charset lazyCharset;
private boolean isCharsetParsed;

private @MonotonicNonNull String cachedToString;
private @MonotonicNonNull String lazyToString;

private MediaType(String type, String subtype) {
this.type = type;
Expand Down Expand Up @@ -269,8 +268,7 @@ public MediaType withParameter(String name, String value) {
* @throws IllegalArgumentException if any of the given parameters is invalid
*/
public MediaType withParameters(Map<String, String> parameters) {
requireNonNull(parameters);
return create(type, subtype, parameters, new LinkedHashMap<>(this.parameters));
return of(type, subtype, parameters, new LinkedHashMap<>(this.parameters));
}

/**
Expand All @@ -287,7 +285,8 @@ public boolean equals(@Nullable Object obj) {
if (!(obj instanceof MediaType)) {
return false;
}
MediaType other = (MediaType) obj;

var other = (MediaType) obj;
return type.equals(other.type)
&& subtype.equals(other.subtype)
&& parameters.equals(other.parameters);
Expand All @@ -305,17 +304,16 @@ public int hashCode() {
*/
@Override
public String toString() {
var result = cachedToString;
if (result == null) {
result = computeToString();
cachedToString = result;
var toString = lazyToString;
if (toString == null) {
toString = computeToString();
lazyToString = toString;
}
return result;
return toString;
}

private String computeToString() {
var sb = new StringBuilder();
sb.append(type).append("/").append(subtype);
var sb = new StringBuilder().append(type).append("/").append(subtype);
parameters.forEach(
(name, value) ->
sb.append("; ").append(name).append("=").append(escapeAndQuoteValueIfNeeded(value)));
Expand Down Expand Up @@ -343,23 +341,25 @@ public static MediaType of(String type, String subtype) {
* invalid
*/
public static MediaType of(String type, String subtype, Map<String, String> parameters) {
return create(type, subtype, parameters, new LinkedHashMap<>());
return of(type, subtype, parameters, new LinkedHashMap<>());
}

private static MediaType create(
private static MediaType of(
String type,
String subtype,
Map<String, String> parameters,
Map<String, String> newParameters) {
requireNonNull(type, "type");
requireNonNull(subtype, "subtype");
requireNonNull(parameters, "parameters");
Map<String, String> untrustedParameters,
Map<String, String> trustedParameters) {
requireNonNull(type);
requireNonNull(subtype);
requireNonNull(untrustedParameters);
requireNonNull(trustedParameters);
requireArgument(
!type.equals(WILDCARD) || subtype.equals(WILDCARD),
"cannot have a wildcard type with a concrete subtype");
"Cannot have a wildcard type with a concrete subtype");
var normalizedType = validateAndNormalizeToken(type);
var normalizedSubtype = validateAndNormalizeToken(subtype);
for (var entry : parameters.entrySet()) {
var parameters = new LinkedHashMap<>(trustedParameters);
for (var entry : untrustedParameters.entrySet()) {
var normalizedAttribute = validateAndNormalizeToken(entry.getKey());
String normalizedValue;
if (normalizedAttribute.equals(CHARSET_ATTRIBUTE)) {
Expand All @@ -369,10 +369,10 @@ private static MediaType create(
requireArgument(
QUOTED_PAIR_MATCHER.allMatch(normalizedValue), "illegal value: '%s'", normalizedValue);
}
newParameters.put(normalizedAttribute, normalizedValue);
parameters.put(normalizedAttribute, normalizedValue);
}
return new MediaType(
normalizedType, normalizedSubtype, Collections.unmodifiableMap(newParameters));
normalizedType, normalizedSubtype, Collections.unmodifiableMap(parameters));
}

private static String validateAndNormalizeToken(String token) {
Expand Down Expand Up @@ -408,7 +408,7 @@ public static MediaType parse(String value) {
}
return parameters != null ? of(type, subtype, parameters) : of(type, subtype);
} catch (IllegalArgumentException | IllegalStateException e) {
throw new IllegalArgumentException(format("couldn't parse: '%s'", value), e);
throw new IllegalArgumentException("Couldn't parse: '" + value + "'", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020 Moataz Abdelnasser
* Copyright (c) 2024 Moataz Abdelnasser
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -110,7 +110,6 @@ public boolean consumeDelimiter(char delimiter) {

public boolean consumeDelimiter(char delimiter, boolean requireDelimiter) {
// 1*( OWS <delimiter> OWS ) | <empty-string> | OWS ; Last OWS if requireDelimiter is false
// TODO first consume OWS
if (hasRemaining()) {
consumeCharsMatching(OWS_MATCHER);
if (requireDelimiter) {
Expand Down

0 comments on commit 5a772a1

Please sign in to comment.