Skip to content

Commit

Permalink
Add MediaType.withParameters(String attribute, Iterable<String> values)
Browse files Browse the repository at this point in the history
RELNOTES=`net`: Added `withParameters` method that can replace a specific attribute with zero or more values

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=181805050
  • Loading branch information
herbyderby authored and cpovirk committed Jan 16, 2018
1 parent d788bc1 commit 7da42d2
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 16 deletions.
38 changes: 38 additions & 0 deletions android/guava-tests/test/com/google/common/net/MediaTypeTest.java
Expand Up @@ -41,12 +41,14 @@
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.testing.EqualsTester;
import com.google.common.testing.NullPointerTester;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Arrays;
import junit.framework.TestCase;

/**
Expand Down Expand Up @@ -246,6 +248,42 @@ public void testWithParameter_invalidAttribute() {
}
}

public void testWithParametersIterable() {
assertEquals(
MediaType.parse("text/plain"),
MediaType.parse("text/plain; a=1; a=2").withParameters("a", ImmutableSet.<String>of()));
assertEquals(
MediaType.parse("text/plain; a=1"),
MediaType.parse("text/plain").withParameters("a", ImmutableSet.of("1")));
assertEquals(
MediaType.parse("text/plain; a=1"),
MediaType.parse("text/plain; a=1; a=2").withParameters("a", ImmutableSet.of("1")));
assertEquals(
MediaType.parse("text/plain; a=1; a=3"),
MediaType.parse("text/plain; a=1; a=2").withParameters("a", ImmutableSet.of("1", "3")));
assertEquals(
MediaType.parse("text/plain; a=1; a=2; b=3; b=4"),
MediaType.parse("text/plain; a=1; a=2").withParameters("b", ImmutableSet.of("3", "4")));
}

public void testWithParametersIterable_invalidAttribute() {
MediaType mediaType = MediaType.parse("text/plain");
try {
mediaType.withParameters("@", ImmutableSet.of("2"));
fail();
} catch (IllegalArgumentException expected) {
}
}

public void testWithParametersIterable_nullValue() {
MediaType mediaType = MediaType.parse("text/plain");
try {
mediaType.withParameters("a", Arrays.asList((String) null));
fail();
} catch (NullPointerException expected) {
}
}

public void testWithCharset() {
assertEquals(
MediaType.parse("text/plain; charset=utf-8"),
Expand Down
31 changes: 23 additions & 8 deletions android/guava/src/com/google/common/net/MediaType.java
Expand Up @@ -33,6 +33,7 @@
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
Expand Down Expand Up @@ -622,16 +623,16 @@ public MediaType withParameters(Multimap<String, String> parameters) {
}

/**
* <em>Replaces</em> all parameters with the given attribute with a single parameter with the
* given value. If multiple parameters with the same attributes are necessary use {@link
* #withParameters}. Prefer {@link #withCharset} for setting the {@code charset} parameter when
* using a {@link Charset} object.
* <em>Replaces</em> all parameters with the given attribute with parameters using the given
* values. If there are no values, any existing parameters with the given attribute are
* removed.
*
* @throws IllegalArgumentException if either {@code attribute} or {@code value} is invalid
* @throws IllegalArgumentException if either {@code attribute} or {@code values} is invalid
* @since NEXT
*/
public MediaType withParameter(String attribute, String value) {
public MediaType withParameters(String attribute, Iterable<String> values) {
checkNotNull(attribute);
checkNotNull(value);
checkNotNull(values);
String normalizedAttribute = normalizeToken(attribute);
ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder();
for (Entry<String, String> entry : parameters.entries()) {
Expand All @@ -640,7 +641,9 @@ public MediaType withParameter(String attribute, String value) {
builder.put(key, entry.getValue());
}
}
builder.put(normalizedAttribute, normalizeParameterValue(normalizedAttribute, value));
for (String value : values) {
builder.put(normalizedAttribute, normalizeParameterValue(normalizedAttribute, value));
}
MediaType mediaType = new MediaType(type, subtype, builder.build());
// if the attribute isn't charset, we can just inherit the current parsedCharset
if (!normalizedAttribute.equals(CHARSET_ATTRIBUTE)) {
Expand All @@ -650,6 +653,18 @@ public MediaType withParameter(String attribute, String value) {
return MoreObjects.firstNonNull(KNOWN_TYPES.get(mediaType), mediaType);
}

/**
* <em>Replaces</em> all parameters with the given attribute with a single parameter with the
* given value. If multiple parameters with the same attributes are necessary use {@link
* #withParameters(String, Iterable)}. Prefer {@link #withCharset} for setting the {@code charset}
* parameter when using a {@link Charset} object.
*
* @throws IllegalArgumentException if either {@code attribute} or {@code value} is invalid
*/
public MediaType withParameter(String attribute, String value) {
return withParameters(attribute, ImmutableSet.of(value));
}

/**
* Returns a new instance with the same type and subtype as this instance, with the {@code
* charset} parameter set to the {@link Charset#name name} of the given charset. Only one {@code
Expand Down
15 changes: 15 additions & 0 deletions guava-gwt/test/com/google/common/net/MediaTypeTest_gwt.java
Expand Up @@ -143,6 +143,21 @@ public void testWithParameters() throws Exception {
testCase.testWithParameters();
}

public void testWithParametersIterable() throws Exception {
com.google.common.net.MediaTypeTest testCase = new com.google.common.net.MediaTypeTest();
testCase.testWithParametersIterable();
}

public void testWithParametersIterable_invalidAttribute() throws Exception {
com.google.common.net.MediaTypeTest testCase = new com.google.common.net.MediaTypeTest();
testCase.testWithParametersIterable_invalidAttribute();
}

public void testWithParametersIterable_nullValue() throws Exception {
com.google.common.net.MediaTypeTest testCase = new com.google.common.net.MediaTypeTest();
testCase.testWithParametersIterable_nullValue();
}

public void testWithParameters_invalidAttribute() throws Exception {
com.google.common.net.MediaTypeTest testCase = new com.google.common.net.MediaTypeTest();
testCase.testWithParameters_invalidAttribute();
Expand Down
38 changes: 38 additions & 0 deletions guava-tests/test/com/google/common/net/MediaTypeTest.java
Expand Up @@ -41,12 +41,14 @@
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.testing.EqualsTester;
import com.google.common.testing.NullPointerTester;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Arrays;
import junit.framework.TestCase;

/**
Expand Down Expand Up @@ -246,6 +248,42 @@ public void testWithParameter_invalidAttribute() {
}
}

public void testWithParametersIterable() {
assertEquals(
MediaType.parse("text/plain"),
MediaType.parse("text/plain; a=1; a=2").withParameters("a", ImmutableSet.<String>of()));
assertEquals(
MediaType.parse("text/plain; a=1"),
MediaType.parse("text/plain").withParameters("a", ImmutableSet.of("1")));
assertEquals(
MediaType.parse("text/plain; a=1"),
MediaType.parse("text/plain; a=1; a=2").withParameters("a", ImmutableSet.of("1")));
assertEquals(
MediaType.parse("text/plain; a=1; a=3"),
MediaType.parse("text/plain; a=1; a=2").withParameters("a", ImmutableSet.of("1", "3")));
assertEquals(
MediaType.parse("text/plain; a=1; a=2; b=3; b=4"),
MediaType.parse("text/plain; a=1; a=2").withParameters("b", ImmutableSet.of("3", "4")));
}

public void testWithParametersIterable_invalidAttribute() {
MediaType mediaType = MediaType.parse("text/plain");
try {
mediaType.withParameters("@", ImmutableSet.of("2"));
fail();
} catch (IllegalArgumentException expected) {
}
}

public void testWithParametersIterable_nullValue() {
MediaType mediaType = MediaType.parse("text/plain");
try {
mediaType.withParameters("a", Arrays.asList((String) null));
fail();
} catch (NullPointerException expected) {
}
}

public void testWithCharset() {
assertEquals(
MediaType.parse("text/plain; charset=utf-8"),
Expand Down
31 changes: 23 additions & 8 deletions guava/src/com/google/common/net/MediaType.java
Expand Up @@ -33,6 +33,7 @@
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
Expand Down Expand Up @@ -622,16 +623,16 @@ public MediaType withParameters(Multimap<String, String> parameters) {
}

/**
* <em>Replaces</em> all parameters with the given attribute with a single parameter with the
* given value. If multiple parameters with the same attributes are necessary use {@link
* #withParameters}. Prefer {@link #withCharset} for setting the {@code charset} parameter when
* using a {@link Charset} object.
* <em>Replaces</em> all parameters with the given attribute with parameters using the given
* values. If there are no values, any existing parameters with the given attribute are
* removed.
*
* @throws IllegalArgumentException if either {@code attribute} or {@code value} is invalid
* @throws IllegalArgumentException if either {@code attribute} or {@code values} is invalid
* @since NEXT
*/
public MediaType withParameter(String attribute, String value) {
public MediaType withParameters(String attribute, Iterable<String> values) {
checkNotNull(attribute);
checkNotNull(value);
checkNotNull(values);
String normalizedAttribute = normalizeToken(attribute);
ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder();
for (Entry<String, String> entry : parameters.entries()) {
Expand All @@ -640,7 +641,9 @@ public MediaType withParameter(String attribute, String value) {
builder.put(key, entry.getValue());
}
}
builder.put(normalizedAttribute, normalizeParameterValue(normalizedAttribute, value));
for (String value : values) {
builder.put(normalizedAttribute, normalizeParameterValue(normalizedAttribute, value));
}
MediaType mediaType = new MediaType(type, subtype, builder.build());
// if the attribute isn't charset, we can just inherit the current parsedCharset
if (!normalizedAttribute.equals(CHARSET_ATTRIBUTE)) {
Expand All @@ -650,6 +653,18 @@ public MediaType withParameter(String attribute, String value) {
return MoreObjects.firstNonNull(KNOWN_TYPES.get(mediaType), mediaType);
}

/**
* <em>Replaces</em> all parameters with the given attribute with a single parameter with the
* given value. If multiple parameters with the same attributes are necessary use {@link
* #withParameters(String, Iterable)}. Prefer {@link #withCharset} for setting the {@code charset}
* parameter when using a {@link Charset} object.
*
* @throws IllegalArgumentException if either {@code attribute} or {@code value} is invalid
*/
public MediaType withParameter(String attribute, String value) {
return withParameters(attribute, ImmutableSet.of(value));
}

/**
* Returns a new instance with the same type and subtype as this instance, with the {@code
* charset} parameter set to the {@link Charset#name name} of the given charset. Only one {@code
Expand Down

0 comments on commit 7da42d2

Please sign in to comment.