Skip to content

Commit

Permalink
Migrate off our Charsets constants, and further discourage usage.
Browse files Browse the repository at this point in the history
Guava has required Java 8 for a while now.

[`StandardCharsets`](https://developer.android.com/reference/java/nio/charset/StandardCharsets) has been available since API Level 19, and we currently [test for compatibility with 21](https://guava.dev/#important-warnings).

RELNOTES=n/a
PiperOrigin-RevId: 655152755
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Jul 23, 2024
1 parent 04c1b7a commit 5041fbe
Show file tree
Hide file tree
Showing 82 changed files with 406 additions and 409 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
package com.google.common.testing;

import static com.google.common.base.Preconditions.checkArgument;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.base.CharMatcher;
import com.google.common.base.Charsets;
import com.google.common.base.Defaults;
import com.google.common.base.Equivalence;
import com.google.common.base.Joiner;
Expand Down Expand Up @@ -206,7 +206,7 @@ private static MatchResult createMatchResult() {
.put(Pattern.class, Pattern.compile(""))
.put(MatchResult.class, createMatchResult())
.put(TimeUnit.class, TimeUnit.SECONDS)
.put(Charset.class, Charsets.UTF_8)
.put(Charset.class, UTF_8)
.put(Currency.class, Currency.getInstance(Locale.US))
.put(Locale.class, Locale.US)
.put(UUID.class, UUID.randomUUID())
Expand Down Expand Up @@ -237,7 +237,7 @@ private static MatchResult createMatchResult() {
.put(ByteSource.class, ByteSource.empty())
.put(CharSource.class, CharSource.empty())
.put(ByteSink.class, NullByteSink.INSTANCE)
.put(CharSink.class, NullByteSink.INSTANCE.asCharSink(Charsets.UTF_8))
.put(CharSink.class, NullByteSink.INSTANCE.asCharSink(UTF_8))
// All collections are immutable empty. So safe for any type parameter.
.put(Iterator.class, ImmutableSet.of().iterator())
.put(PeekingIterator.class, Iterators.peekingIterator(ImmutableSet.of().iterator()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Throwables.throwIfUnchecked;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.base.CharMatcher;
import com.google.common.base.Charsets;
import com.google.common.base.Equivalence;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
Expand Down Expand Up @@ -505,7 +505,7 @@ Pattern generatePattern() {

@Generates
Charset generateCharset() {
return pickInstance(Charset.availableCharsets().values(), Charsets.UTF_8);
return pickInstance(Charset.availableCharsets().values(), UTF_8);
}

@Generates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
package com.google.common.testing;

import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThrows;

import com.google.common.base.CharMatcher;
import com.google.common.base.Charsets;
import com.google.common.base.Equivalence;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
Expand Down Expand Up @@ -165,7 +165,7 @@ public void testGet_primitives() {
assertEquals(TimeUnit.SECONDS, ArbitraryInstances.get(TimeUnit.class));
assertNotNull(ArbitraryInstances.get(Object.class));
assertEquals(0, ArbitraryInstances.get(Number.class));
assertEquals(Charsets.UTF_8, ArbitraryInstances.get(Charset.class));
assertEquals(UTF_8, ArbitraryInstances.get(Charset.class));
assertNotNull(ArbitraryInstances.get(UUID.class));
}

Expand Down
5 changes: 3 additions & 2 deletions android/guava-tests/test/com/google/common/base/Utf8Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static java.lang.Character.MIN_HIGH_SURROGATE;
import static java.lang.Character.MIN_LOW_SURROGATE;
import static java.lang.Character.MIN_SUPPLEMENTARY_CODE_POINT;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
Expand Down Expand Up @@ -332,8 +333,8 @@ private static void testBytes(int numBytes, long expectedCount, long start, long
}
boolean isRoundTrippable = Utf8.isWellFormed(bytes);
assertEquals(isRoundTrippable, Utf8.isWellFormed(bytes, 0, numBytes));
String s = new String(bytes, Charsets.UTF_8);
byte[] bytesReencoded = s.getBytes(Charsets.UTF_8);
String s = new String(bytes, UTF_8);
byte[] bytesReencoded = s.getBytes(UTF_8);
boolean bytesEqual = Arrays.equals(bytes, bytesReencoded);

if (bytesEqual != isRoundTrippable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.testing.Helpers.mapEntry;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Maps.EntryTransformer;
Expand Down Expand Up @@ -631,14 +631,14 @@ protected SortedMap<String, String> delegate() {
}

private static String encode(String str) {
return BaseEncoding.base64().encode(str.getBytes(Charsets.UTF_8));
return BaseEncoding.base64().encode(str.getBytes(UTF_8));
}

private static final Function<String, String> DECODE_FUNCTION =
new Function<String, String>() {
@Override
public String apply(String input) {
return new String(BaseEncoding.base64().decode(input), Charsets.UTF_8);
return new String(BaseEncoding.base64().decode(input), UTF_8);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package com.google.common.hash;

import static com.google.common.base.Charsets.UTF_16LE;
import static java.nio.charset.StandardCharsets.UTF_16LE;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertThrows;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.google.common.hash;

import static com.google.common.base.Charsets.UTF_16LE;
import static java.nio.charset.StandardCharsets.UTF_16LE;
import static org.junit.Assert.assertThrows;

import com.google.common.collect.Iterables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.google.common.hash;

import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThrows;

import com.google.common.base.Stopwatch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package com.google.common.hash;

import static com.google.common.base.Charsets.UTF_8;
import static java.nio.charset.StandardCharsets.UTF_8;

import java.util.Arrays;
import java.util.Random;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.google.common.hash;

import static com.google.common.base.Charsets.ISO_8859_1;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.base.Strings;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

package com.google.common.hash;

import static com.google.common.base.Charsets.ISO_8859_1;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSortedMap;
Expand Down
11 changes: 6 additions & 5 deletions android/guava-tests/test/com/google/common/hash/FunnelsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

package com.google.common.hash;

import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import com.google.common.base.Charsets;
import com.google.common.testing.EqualsTester;
import com.google.common.testing.SerializableTester;
import java.io.OutputStream;
Expand Down Expand Up @@ -151,8 +152,8 @@ public void testSerialization() {
Funnels.sequentialFunnel(Funnels.integerFunnel()),
SerializableTester.reserialize(Funnels.sequentialFunnel(Funnels.integerFunnel())));
assertEquals(
Funnels.stringFunnel(Charsets.US_ASCII),
SerializableTester.reserialize(Funnels.stringFunnel(Charsets.US_ASCII)));
Funnels.stringFunnel(US_ASCII),
SerializableTester.reserialize(Funnels.stringFunnel(US_ASCII)));
}

public void testEquals() {
Expand All @@ -161,8 +162,8 @@ public void testEquals() {
.addEqualityGroup(Funnels.integerFunnel())
.addEqualityGroup(Funnels.longFunnel())
.addEqualityGroup(Funnels.unencodedCharsFunnel())
.addEqualityGroup(Funnels.stringFunnel(Charsets.UTF_8))
.addEqualityGroup(Funnels.stringFunnel(Charsets.US_ASCII))
.addEqualityGroup(Funnels.stringFunnel(UTF_8))
.addEqualityGroup(Funnels.stringFunnel(US_ASCII))
.addEqualityGroup(
Funnels.sequentialFunnel(Funnels.integerFunnel()),
SerializableTester.reserialize(Funnels.sequentialFunnel(Funnels.integerFunnel())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package com.google.common.hash;

import static com.google.common.io.BaseEncoding.base16;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static org.junit.Assert.assertThrows;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.io.BaseEncoding;
import com.google.common.testing.ClassSanityTester;
Expand Down Expand Up @@ -183,7 +183,7 @@ public void testHashCode_equalsAndSerializable() throws Exception {
}

public void testRoundTripHashCodeUsingBaseEncoding() {
HashCode hash1 = Hashing.sha1().hashString("foo", Charsets.US_ASCII);
HashCode hash1 = Hashing.sha1().hashString("foo", US_ASCII);
HashCode hash2 = HashCode.fromBytes(BaseEncoding.base16().lowerCase().decode(hash1.toString()));
assertEquals(hash1, hash2);
}
Expand Down Expand Up @@ -215,7 +215,7 @@ public void testObjectHashCodeWithSameLowOrderBytes() {
}

public void testRoundTripHashCodeUsingFromString() {
HashCode hash1 = Hashing.sha1().hashString("foo", Charsets.US_ASCII);
HashCode hash1 = Hashing.sha1().hashString("foo", US_ASCII);
HashCode hash2 = HashCode.fromString(hash1.toString());
assertEquals(hash1, hash2);
}
Expand All @@ -235,7 +235,7 @@ public void testFromStringFailsWithInvalidHexChar() {
}

public void testFromStringFailsWithUpperCaseString() {
String string = Hashing.sha1().hashString("foo", Charsets.US_ASCII).toString().toUpperCase();
String string = Hashing.sha1().hashString("foo", US_ASCII).toString().toUpperCase();
assertThrows(IllegalArgumentException.class, () -> HashCode.fromString(string));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@
package com.google.common.hash;

import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_16;
import static java.nio.charset.StandardCharsets.UTF_16BE;
import static java.nio.charset.StandardCharsets.UTF_16LE;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.common.primitives.Ints;
Expand Down Expand Up @@ -628,13 +633,7 @@ private static void assertHashLongEquivalence(HashFunction hashFunction, Random
}

private static final ImmutableSet<Charset> CHARSETS =
ImmutableSet.of(
Charsets.ISO_8859_1,
Charsets.US_ASCII,
Charsets.UTF_16,
Charsets.UTF_16BE,
Charsets.UTF_16LE,
Charsets.UTF_8);
ImmutableSet.of(ISO_8859_1, US_ASCII, UTF_16, UTF_16BE, UTF_16LE, UTF_8);

private static void assertHashStringEquivalence(HashFunction hashFunction, Random random) {
// Test that only data and data-order is important, not the individual operations.
Expand All @@ -658,7 +657,7 @@ private static void assertHashStringEquivalence(HashFunction hashFunction, Rando
int size = random.nextInt(2048);
byte[] bytes = new byte[size];
random.nextBytes(bytes);
String string = new String(bytes, Charsets.US_ASCII);
String string = new String(bytes, US_ASCII);
assertEquals(
hashFunction.hashUnencodedChars(string),
hashFunction.newHasher().putUnencodedChars(string).hash());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.google.common.hash;

import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.io.BaseEncoding.base16;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThrows;

import com.google.common.collect.ImmutableSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.google.common.hash;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThrows;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.security.MessageDigest;
Expand Down Expand Up @@ -64,9 +64,7 @@ public void testPutAfterHash() {

assertEquals(
"2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
sha1.putString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8)
.hash()
.toString());
sha1.putString("The quick brown fox jumps over the lazy dog", UTF_8).hash().toString());
assertThrows(IllegalStateException.class, () -> sha1.putInt(42));
}

Expand All @@ -75,9 +73,7 @@ public void testHashTwice() {

assertEquals(
"2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
sha1.putString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8)
.hash()
.toString());
sha1.putString("The quick brown fox jumps over the lazy dog", UTF_8).hash().toString());
assertThrows(IllegalStateException.class, () -> sha1.hash());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package com.google.common.hash;

import static com.google.common.hash.Hashing.murmur3_128;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.base.Charsets;
import com.google.common.hash.HashTestUtils.HashFn;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
Expand All @@ -40,7 +40,7 @@ public void testKnownValues() {

// Known output from Python smhasher
HashCode foxHash =
murmur3_128(0).hashString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8);
murmur3_128(0).hashString("The quick brown fox jumps over the lazy dog", UTF_8);
assertEquals("6c1b07bc7bbc4be347939ac4a93c437a", foxHash.toString());
}

Expand Down
Loading

0 comments on commit 5041fbe

Please sign in to comment.