Skip to content

Commit 5041fbe

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
Migrate off our Charsets constants, and further discourage usage.
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
1 parent 04c1b7a commit 5041fbe

File tree

82 files changed

+406
-409
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+406
-409
lines changed

android/guava-testlib/src/com/google/common/testing/ArbitraryInstances.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
package com.google.common.testing;
1818

1919
import static com.google.common.base.Preconditions.checkArgument;
20+
import static java.nio.charset.StandardCharsets.UTF_8;
2021
import static java.util.Objects.requireNonNull;
2122

2223
import com.google.common.annotations.GwtIncompatible;
2324
import com.google.common.annotations.J2ktIncompatible;
2425
import com.google.common.base.CharMatcher;
25-
import com.google.common.base.Charsets;
2626
import com.google.common.base.Defaults;
2727
import com.google.common.base.Equivalence;
2828
import com.google.common.base.Joiner;
@@ -206,7 +206,7 @@ private static MatchResult createMatchResult() {
206206
.put(Pattern.class, Pattern.compile(""))
207207
.put(MatchResult.class, createMatchResult())
208208
.put(TimeUnit.class, TimeUnit.SECONDS)
209-
.put(Charset.class, Charsets.UTF_8)
209+
.put(Charset.class, UTF_8)
210210
.put(Currency.class, Currency.getInstance(Locale.US))
211211
.put(Locale.class, Locale.US)
212212
.put(UUID.class, UUID.randomUUID())
@@ -237,7 +237,7 @@ private static MatchResult createMatchResult() {
237237
.put(ByteSource.class, ByteSource.empty())
238238
.put(CharSource.class, CharSource.empty())
239239
.put(ByteSink.class, NullByteSink.INSTANCE)
240-
.put(CharSink.class, NullByteSink.INSTANCE.asCharSink(Charsets.UTF_8))
240+
.put(CharSink.class, NullByteSink.INSTANCE.asCharSink(UTF_8))
241241
// All collections are immutable empty. So safe for any type parameter.
242242
.put(Iterator.class, ImmutableSet.of().iterator())
243243
.put(PeekingIterator.class, Iterators.peekingIterator(ImmutableSet.of().iterator()))

android/guava-testlib/src/com/google/common/testing/FreshValueGenerator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020
import static com.google.common.base.Throwables.throwIfUnchecked;
21+
import static java.nio.charset.StandardCharsets.UTF_8;
2122
import static java.util.Objects.requireNonNull;
2223

2324
import com.google.common.annotations.GwtIncompatible;
2425
import com.google.common.annotations.J2ktIncompatible;
2526
import com.google.common.base.CharMatcher;
26-
import com.google.common.base.Charsets;
2727
import com.google.common.base.Equivalence;
2828
import com.google.common.base.Joiner;
2929
import com.google.common.base.Splitter;
@@ -505,7 +505,7 @@ Pattern generatePattern() {
505505

506506
@Generates
507507
Charset generateCharset() {
508-
return pickInstance(Charset.availableCharsets().values(), Charsets.UTF_8);
508+
return pickInstance(Charset.availableCharsets().values(), UTF_8);
509509
}
510510

511511
@Generates

android/guava-testlib/test/com/google/common/testing/ArbitraryInstancesTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
package com.google.common.testing;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static java.nio.charset.StandardCharsets.UTF_8;
2021
import static org.junit.Assert.assertThrows;
2122

2223
import com.google.common.base.CharMatcher;
23-
import com.google.common.base.Charsets;
2424
import com.google.common.base.Equivalence;
2525
import com.google.common.base.Joiner;
2626
import com.google.common.base.Predicate;
@@ -165,7 +165,7 @@ public void testGet_primitives() {
165165
assertEquals(TimeUnit.SECONDS, ArbitraryInstances.get(TimeUnit.class));
166166
assertNotNull(ArbitraryInstances.get(Object.class));
167167
assertEquals(0, ArbitraryInstances.get(Number.class));
168-
assertEquals(Charsets.UTF_8, ArbitraryInstances.get(Charset.class));
168+
assertEquals(UTF_8, ArbitraryInstances.get(Charset.class));
169169
assertNotNull(ArbitraryInstances.get(UUID.class));
170170
}
171171

android/guava-tests/test/com/google/common/base/Utf8Test.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static java.lang.Character.MIN_HIGH_SURROGATE;
2424
import static java.lang.Character.MIN_LOW_SURROGATE;
2525
import static java.lang.Character.MIN_SUPPLEMENTARY_CODE_POINT;
26+
import static java.nio.charset.StandardCharsets.UTF_8;
2627

2728
import com.google.common.annotations.GwtCompatible;
2829
import com.google.common.annotations.GwtIncompatible;
@@ -332,8 +333,8 @@ private static void testBytes(int numBytes, long expectedCount, long start, long
332333
}
333334
boolean isRoundTrippable = Utf8.isWellFormed(bytes);
334335
assertEquals(isRoundTrippable, Utf8.isWellFormed(bytes, 0, numBytes));
335-
String s = new String(bytes, Charsets.UTF_8);
336-
byte[] bytesReencoded = s.getBytes(Charsets.UTF_8);
336+
String s = new String(bytes, UTF_8);
337+
byte[] bytesReencoded = s.getBytes(UTF_8);
337338
boolean bytesEqual = Arrays.equals(bytes, bytesReencoded);
338339

339340
if (bytesEqual != isRoundTrippable) {

android/guava-tests/test/com/google/common/collect/MapsCollectionTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import static com.google.common.base.Preconditions.checkArgument;
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121
import static com.google.common.collect.testing.Helpers.mapEntry;
22+
import static java.nio.charset.StandardCharsets.UTF_8;
2223

23-
import com.google.common.base.Charsets;
2424
import com.google.common.base.Function;
2525
import com.google.common.base.Predicate;
2626
import com.google.common.collect.Maps.EntryTransformer;
@@ -631,14 +631,14 @@ protected SortedMap<String, String> delegate() {
631631
}
632632

633633
private static String encode(String str) {
634-
return BaseEncoding.base64().encode(str.getBytes(Charsets.UTF_8));
634+
return BaseEncoding.base64().encode(str.getBytes(UTF_8));
635635
}
636636

637637
private static final Function<String, String> DECODE_FUNCTION =
638638
new Function<String, String>() {
639639
@Override
640640
public String apply(String input) {
641-
return new String(BaseEncoding.base64().decode(input), Charsets.UTF_8);
641+
return new String(BaseEncoding.base64().decode(input), UTF_8);
642642
}
643643
};
644644

android/guava-tests/test/com/google/common/hash/AbstractByteHasherTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package com.google.common.hash;
1616

17-
import static com.google.common.base.Charsets.UTF_16LE;
17+
import static java.nio.charset.StandardCharsets.UTF_16LE;
1818
import static org.junit.Assert.assertArrayEquals;
1919
import static org.junit.Assert.assertThrows;
2020

android/guava-tests/test/com/google/common/hash/AbstractStreamingHasherTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.google.common.hash;
1818

19-
import static com.google.common.base.Charsets.UTF_16LE;
19+
import static java.nio.charset.StandardCharsets.UTF_16LE;
2020
import static org.junit.Assert.assertThrows;
2121

2222
import com.google.common.collect.Iterables;

android/guava-tests/test/com/google/common/hash/BloomFilterTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package com.google.common.hash;
1818

19-
import static com.google.common.base.Charsets.UTF_8;
2019
import static com.google.common.truth.Truth.assertThat;
20+
import static java.nio.charset.StandardCharsets.UTF_8;
2121
import static org.junit.Assert.assertThrows;
2222

2323
import com.google.common.base.Stopwatch;

android/guava-tests/test/com/google/common/hash/Crc32cHashFunctionTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package com.google.common.hash;
1616

17-
import static com.google.common.base.Charsets.UTF_8;
17+
import static java.nio.charset.StandardCharsets.UTF_8;
1818

1919
import java.util.Arrays;
2020
import java.util.Random;

android/guava-tests/test/com/google/common/hash/FarmHashFingerprint64Test.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
package com.google.common.hash;
1818

19-
import static com.google.common.base.Charsets.ISO_8859_1;
20-
import static com.google.common.base.Charsets.UTF_8;
2119
import static com.google.common.truth.Truth.assertThat;
20+
import static java.nio.charset.StandardCharsets.ISO_8859_1;
21+
import static java.nio.charset.StandardCharsets.UTF_8;
2222

2323
import com.google.common.base.Strings;
2424
import java.util.Arrays;

android/guava-tests/test/com/google/common/hash/Fingerprint2011Test.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
package com.google.common.hash;
44

5-
import static com.google.common.base.Charsets.ISO_8859_1;
6-
import static com.google.common.base.Charsets.UTF_8;
75
import static com.google.common.truth.Truth.assertThat;
6+
import static java.nio.charset.StandardCharsets.ISO_8859_1;
7+
import static java.nio.charset.StandardCharsets.UTF_8;
88

99
import com.google.common.base.Strings;
1010
import com.google.common.collect.ImmutableSortedMap;

android/guava-tests/test/com/google/common/hash/FunnelsTest.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616

1717
package com.google.common.hash;
1818

19+
import static java.nio.charset.StandardCharsets.US_ASCII;
20+
import static java.nio.charset.StandardCharsets.UTF_8;
1921
import static org.mockito.Mockito.inOrder;
2022
import static org.mockito.Mockito.mock;
2123
import static org.mockito.Mockito.verify;
2224

23-
import com.google.common.base.Charsets;
2425
import com.google.common.testing.EqualsTester;
2526
import com.google.common.testing.SerializableTester;
2627
import java.io.OutputStream;
@@ -151,8 +152,8 @@ public void testSerialization() {
151152
Funnels.sequentialFunnel(Funnels.integerFunnel()),
152153
SerializableTester.reserialize(Funnels.sequentialFunnel(Funnels.integerFunnel())));
153154
assertEquals(
154-
Funnels.stringFunnel(Charsets.US_ASCII),
155-
SerializableTester.reserialize(Funnels.stringFunnel(Charsets.US_ASCII)));
155+
Funnels.stringFunnel(US_ASCII),
156+
SerializableTester.reserialize(Funnels.stringFunnel(US_ASCII)));
156157
}
157158

158159
public void testEquals() {
@@ -161,8 +162,8 @@ public void testEquals() {
161162
.addEqualityGroup(Funnels.integerFunnel())
162163
.addEqualityGroup(Funnels.longFunnel())
163164
.addEqualityGroup(Funnels.unencodedCharsFunnel())
164-
.addEqualityGroup(Funnels.stringFunnel(Charsets.UTF_8))
165-
.addEqualityGroup(Funnels.stringFunnel(Charsets.US_ASCII))
165+
.addEqualityGroup(Funnels.stringFunnel(UTF_8))
166+
.addEqualityGroup(Funnels.stringFunnel(US_ASCII))
166167
.addEqualityGroup(
167168
Funnels.sequentialFunnel(Funnels.integerFunnel()),
168169
SerializableTester.reserialize(Funnels.sequentialFunnel(Funnels.integerFunnel())))

android/guava-tests/test/com/google/common/hash/HashCodeTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
package com.google.common.hash;
1818

1919
import static com.google.common.io.BaseEncoding.base16;
20+
import static java.nio.charset.StandardCharsets.US_ASCII;
2021
import static org.junit.Assert.assertThrows;
2122

22-
import com.google.common.base.Charsets;
2323
import com.google.common.collect.ImmutableList;
2424
import com.google.common.io.BaseEncoding;
2525
import com.google.common.testing.ClassSanityTester;
@@ -183,7 +183,7 @@ public void testHashCode_equalsAndSerializable() throws Exception {
183183
}
184184

185185
public void testRoundTripHashCodeUsingBaseEncoding() {
186-
HashCode hash1 = Hashing.sha1().hashString("foo", Charsets.US_ASCII);
186+
HashCode hash1 = Hashing.sha1().hashString("foo", US_ASCII);
187187
HashCode hash2 = HashCode.fromBytes(BaseEncoding.base16().lowerCase().decode(hash1.toString()));
188188
assertEquals(hash1, hash2);
189189
}
@@ -215,7 +215,7 @@ public void testObjectHashCodeWithSameLowOrderBytes() {
215215
}
216216

217217
public void testRoundTripHashCodeUsingFromString() {
218-
HashCode hash1 = Hashing.sha1().hashString("foo", Charsets.US_ASCII);
218+
HashCode hash1 = Hashing.sha1().hashString("foo", US_ASCII);
219219
HashCode hash2 = HashCode.fromString(hash1.toString());
220220
assertEquals(hash1, hash2);
221221
}
@@ -235,7 +235,7 @@ public void testFromStringFailsWithInvalidHexChar() {
235235
}
236236

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

android/guava-tests/test/com/google/common/hash/HashTestUtils.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
1717
package com.google.common.hash;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static java.nio.charset.StandardCharsets.ISO_8859_1;
21+
import static java.nio.charset.StandardCharsets.US_ASCII;
22+
import static java.nio.charset.StandardCharsets.UTF_16;
23+
import static java.nio.charset.StandardCharsets.UTF_16BE;
24+
import static java.nio.charset.StandardCharsets.UTF_16LE;
25+
import static java.nio.charset.StandardCharsets.UTF_8;
2026
import static org.junit.Assert.assertEquals;
2127
import static org.junit.Assert.assertFalse;
2228

23-
import com.google.common.base.Charsets;
2429
import com.google.common.collect.ImmutableSet;
2530
import com.google.common.collect.Sets;
2631
import com.google.common.primitives.Ints;
@@ -628,13 +633,7 @@ private static void assertHashLongEquivalence(HashFunction hashFunction, Random
628633
}
629634

630635
private static final ImmutableSet<Charset> CHARSETS =
631-
ImmutableSet.of(
632-
Charsets.ISO_8859_1,
633-
Charsets.US_ASCII,
634-
Charsets.UTF_16,
635-
Charsets.UTF_16BE,
636-
Charsets.UTF_16LE,
637-
Charsets.UTF_8);
636+
ImmutableSet.of(ISO_8859_1, US_ASCII, UTF_16, UTF_16BE, UTF_16LE, UTF_8);
638637

639638
private static void assertHashStringEquivalence(HashFunction hashFunction, Random random) {
640639
// Test that only data and data-order is important, not the individual operations.
@@ -658,7 +657,7 @@ private static void assertHashStringEquivalence(HashFunction hashFunction, Rando
658657
int size = random.nextInt(2048);
659658
byte[] bytes = new byte[size];
660659
random.nextBytes(bytes);
661-
String string = new String(bytes, Charsets.US_ASCII);
660+
String string = new String(bytes, US_ASCII);
662661
assertEquals(
663662
hashFunction.hashUnencodedChars(string),
664663
hashFunction.newHasher().putUnencodedChars(string).hash());

android/guava-tests/test/com/google/common/hash/MacHashFunctionTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package com.google.common.hash;
1818

19-
import static com.google.common.base.Charsets.UTF_8;
2019
import static com.google.common.io.BaseEncoding.base16;
20+
import static java.nio.charset.StandardCharsets.UTF_8;
2121
import static org.junit.Assert.assertThrows;
2222

2323
import com.google.common.collect.ImmutableSet;

android/guava-tests/test/com/google/common/hash/MessageDigestHashFunctionTest.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
package com.google.common.hash;
1818

19+
import static java.nio.charset.StandardCharsets.UTF_8;
1920
import static org.junit.Assert.assertThrows;
2021

21-
import com.google.common.base.Charsets;
2222
import com.google.common.collect.ImmutableMap;
2323
import com.google.common.collect.ImmutableSet;
2424
import java.security.MessageDigest;
@@ -64,9 +64,7 @@ public void testPutAfterHash() {
6464

6565
assertEquals(
6666
"2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
67-
sha1.putString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8)
68-
.hash()
69-
.toString());
67+
sha1.putString("The quick brown fox jumps over the lazy dog", UTF_8).hash().toString());
7068
assertThrows(IllegalStateException.class, () -> sha1.putInt(42));
7169
}
7270

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

7674
assertEquals(
7775
"2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
78-
sha1.putString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8)
79-
.hash()
80-
.toString());
76+
sha1.putString("The quick brown fox jumps over the lazy dog", UTF_8).hash().toString());
8177
assertThrows(IllegalStateException.class, () -> sha1.hash());
8278
}
8379

android/guava-tests/test/com/google/common/hash/Murmur3Hash128Test.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
package com.google.common.hash;
1818

1919
import static com.google.common.hash.Hashing.murmur3_128;
20+
import static java.nio.charset.StandardCharsets.UTF_8;
2021

21-
import com.google.common.base.Charsets;
2222
import com.google.common.hash.HashTestUtils.HashFn;
2323
import java.nio.ByteBuffer;
2424
import java.nio.ByteOrder;
@@ -40,7 +40,7 @@ public void testKnownValues() {
4040

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

0 commit comments

Comments
 (0)