diff --git a/android/guava-tests/test/com/google/common/net/MediaTypeTest.java b/android/guava-tests/test/com/google/common/net/MediaTypeTest.java index d91db30c6422..7dfa9b82d842 100755 --- a/android/guava-tests/test/com/google/common/net/MediaTypeTest.java +++ b/android/guava-tests/test/com/google/common/net/MediaTypeTest.java @@ -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; /** @@ -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.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"), diff --git a/android/guava/src/com/google/common/net/MediaType.java b/android/guava/src/com/google/common/net/MediaType.java index 02c6d2e52154..2eda32ff4a15 100755 --- a/android/guava/src/com/google/common/net/MediaType.java +++ b/android/guava/src/com/google/common/net/MediaType.java @@ -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; @@ -622,16 +623,16 @@ public MediaType withParameters(Multimap parameters) { } /** - * Replaces 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. + * Replaces 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 values) { checkNotNull(attribute); - checkNotNull(value); + checkNotNull(values); String normalizedAttribute = normalizeToken(attribute); ImmutableListMultimap.Builder builder = ImmutableListMultimap.builder(); for (Entry entry : parameters.entries()) { @@ -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)) { @@ -650,6 +653,18 @@ public MediaType withParameter(String attribute, String value) { return MoreObjects.firstNonNull(KNOWN_TYPES.get(mediaType), mediaType); } + /** + * Replaces 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 diff --git a/guava-gwt/test/com/google/common/net/MediaTypeTest_gwt.java b/guava-gwt/test/com/google/common/net/MediaTypeTest_gwt.java index c2dc40d0a7b9..2333ec62a0ee 100644 --- a/guava-gwt/test/com/google/common/net/MediaTypeTest_gwt.java +++ b/guava-gwt/test/com/google/common/net/MediaTypeTest_gwt.java @@ -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(); diff --git a/guava-tests/test/com/google/common/net/MediaTypeTest.java b/guava-tests/test/com/google/common/net/MediaTypeTest.java index d91db30c6422..7dfa9b82d842 100644 --- a/guava-tests/test/com/google/common/net/MediaTypeTest.java +++ b/guava-tests/test/com/google/common/net/MediaTypeTest.java @@ -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; /** @@ -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.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"), diff --git a/guava/src/com/google/common/net/MediaType.java b/guava/src/com/google/common/net/MediaType.java index 02c6d2e52154..2eda32ff4a15 100644 --- a/guava/src/com/google/common/net/MediaType.java +++ b/guava/src/com/google/common/net/MediaType.java @@ -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; @@ -622,16 +623,16 @@ public MediaType withParameters(Multimap parameters) { } /** - * Replaces 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. + * Replaces 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 values) { checkNotNull(attribute); - checkNotNull(value); + checkNotNull(values); String normalizedAttribute = normalizeToken(attribute); ImmutableListMultimap.Builder builder = ImmutableListMultimap.builder(); for (Entry entry : parameters.entries()) { @@ -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)) { @@ -650,6 +653,18 @@ public MediaType withParameter(String attribute, String value) { return MoreObjects.firstNonNull(KNOWN_TYPES.get(mediaType), mediaType); } + /** + * Replaces 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