Skip to content

Commit

Permalink
Address more ConstantCaseForConstants warnings.
Browse files Browse the repository at this point in the history
There are still so many more, but I ran out of steam.

Also, in Truth's `Platform` class, migrate off the deprecated `Splitter.onPattern`, and start using the now-non-`@Beta` `splitToList`.

PiperOrigin-RevId: 710166260
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Dec 28, 2024
1 parent 470f16f commit 9b05674
Show file tree
Hide file tree
Showing 43 changed files with 374 additions and 352 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
public class UnsignedLongsBenchmark {
private static final int ARRAY_SIZE = 0x10000;
private static final int ARRAY_MASK = 0x0ffff;
private static final Random RANDOM_SOURCE = new Random(314159265358979L);
private static final Random randomSource = new Random(314159265358979L);
private static final long[] longs = new long[ARRAY_SIZE];
private static final long[] divisors = new long[ARRAY_SIZE];
private static final String[] decimalStrings = new String[ARRAY_SIZE];
Expand Down Expand Up @@ -122,7 +122,7 @@ int toString(int reps) {
}

private static long random() {
return RANDOM_SOURCE.nextLong();
return randomSource.nextLong();
}

// A random value that cannot be 0 and that is unsigned-less-than or equal
Expand All @@ -131,7 +131,7 @@ private static long random() {
// Using remainder here does not give us a uniform distribution but it should
// not have a big impact on the measurement.
private static long randomDivisor(long dividend) {
long r = RANDOM_SOURCE.nextLong();
long r = randomSource.nextLong();
if (dividend == -1) {
return r;
} else {
Expand Down
84 changes: 42 additions & 42 deletions android/guava-tests/test/com/google/common/base/JoinerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ public class JoinerTest extends TestCase {
private static final Joiner J = Joiner.on("-");

// <Integer> needed to prevent warning :(
private static final Iterable<Integer> ITERABLE_ = Arrays.<Integer>asList();
private static final Iterable<Integer> ITERABLE_1 = Arrays.asList(1);
private static final Iterable<Integer> ITERABLE_12 = Arrays.asList(1, 2);
private static final Iterable<Integer> ITERABLE_123 = Arrays.asList(1, 2, 3);
private static final Iterable<@Nullable Integer> ITERABLE_NULL = Arrays.asList((Integer) null);
private static final Iterable<@Nullable Integer> ITERABLE_NULL_NULL =
private static final Iterable<Integer> iterable = Arrays.<Integer>asList();
private static final Iterable<Integer> iterable1 = Arrays.asList(1);
private static final Iterable<Integer> iterable12 = Arrays.asList(1, 2);
private static final Iterable<Integer> iterable123 = Arrays.asList(1, 2, 3);
private static final Iterable<@Nullable Integer> iterableNull = Arrays.asList((Integer) null);
private static final Iterable<@Nullable Integer> iterableNullNull =
Arrays.asList((Integer) null, null);
private static final Iterable<@Nullable Integer> ITERABLE_NULL_1 = Arrays.asList(null, 1);
private static final Iterable<@Nullable Integer> ITERABLE_1_NULL = Arrays.asList(1, null);
private static final Iterable<@Nullable Integer> ITERABLE_1_NULL_2 = Arrays.asList(1, null, 2);
private static final Iterable<@Nullable Integer> ITERABLE_FOUR_NULLS =
private static final Iterable<@Nullable Integer> iterableNull1 = Arrays.asList(null, 1);
private static final Iterable<@Nullable Integer> iterable1Null = Arrays.asList(1, null);
private static final Iterable<@Nullable Integer> iterable1Null2 = Arrays.asList(1, null, 2);
private static final Iterable<@Nullable Integer> iterableFourNulls =
Arrays.asList((Integer) null, null, null, null);

/*
Expand Down Expand Up @@ -107,60 +107,60 @@ public int size() {

@SuppressWarnings("JoinIterableIterator") // explicitly testing iterator overload, too
public void testNoSpecialNullBehavior() {
checkNoOutput(J, ITERABLE_);
checkResult(J, ITERABLE_1, "1");
checkResult(J, ITERABLE_12, "1-2");
checkResult(J, ITERABLE_123, "1-2-3");
checkNoOutput(J, iterable);
checkResult(J, iterable1, "1");
checkResult(J, iterable12, "1-2");
checkResult(J, iterable123, "1-2-3");
checkResult(J, UNDERREPORTING_SIZE_LIST, "1-2-3");
checkResult(J, OVERREPORTING_SIZE_LIST, "1-2-3");

assertThrows(NullPointerException.class, () -> J.join(ITERABLE_NULL));
assertThrows(NullPointerException.class, () -> J.join(ITERABLE_1_NULL_2));
assertThrows(NullPointerException.class, () -> J.join(iterableNull));
assertThrows(NullPointerException.class, () -> J.join(iterable1Null2));

assertThrows(NullPointerException.class, () -> J.join(ITERABLE_NULL.iterator()));
assertThrows(NullPointerException.class, () -> J.join(ITERABLE_1_NULL_2.iterator()));
assertThrows(NullPointerException.class, () -> J.join(iterableNull.iterator()));
assertThrows(NullPointerException.class, () -> J.join(iterable1Null2.iterator()));
}

public void testOnCharOverride() {
Joiner onChar = Joiner.on('-');
checkNoOutput(onChar, ITERABLE_);
checkResult(onChar, ITERABLE_1, "1");
checkResult(onChar, ITERABLE_12, "1-2");
checkResult(onChar, ITERABLE_123, "1-2-3");
checkNoOutput(onChar, iterable);
checkResult(onChar, iterable1, "1");
checkResult(onChar, iterable12, "1-2");
checkResult(onChar, iterable123, "1-2-3");
checkResult(J, UNDERREPORTING_SIZE_LIST, "1-2-3");
checkResult(J, OVERREPORTING_SIZE_LIST, "1-2-3");
}

public void testSkipNulls() {
Joiner skipNulls = J.skipNulls();
checkNoOutput(skipNulls, ITERABLE_);
checkNoOutput(skipNulls, ITERABLE_NULL);
checkNoOutput(skipNulls, ITERABLE_NULL_NULL);
checkNoOutput(skipNulls, ITERABLE_FOUR_NULLS);
checkResult(skipNulls, ITERABLE_1, "1");
checkResult(skipNulls, ITERABLE_12, "1-2");
checkResult(skipNulls, ITERABLE_123, "1-2-3");
checkNoOutput(skipNulls, iterable);
checkNoOutput(skipNulls, iterableNull);
checkNoOutput(skipNulls, iterableNullNull);
checkNoOutput(skipNulls, iterableFourNulls);
checkResult(skipNulls, iterable1, "1");
checkResult(skipNulls, iterable12, "1-2");
checkResult(skipNulls, iterable123, "1-2-3");
checkResult(J, UNDERREPORTING_SIZE_LIST, "1-2-3");
checkResult(J, OVERREPORTING_SIZE_LIST, "1-2-3");
checkResult(skipNulls, ITERABLE_NULL_1, "1");
checkResult(skipNulls, ITERABLE_1_NULL, "1");
checkResult(skipNulls, ITERABLE_1_NULL_2, "1-2");
checkResult(skipNulls, iterableNull1, "1");
checkResult(skipNulls, iterable1Null, "1");
checkResult(skipNulls, iterable1Null2, "1-2");
}

public void testUseForNull() {
Joiner zeroForNull = J.useForNull("0");
checkNoOutput(zeroForNull, ITERABLE_);
checkResult(zeroForNull, ITERABLE_1, "1");
checkResult(zeroForNull, ITERABLE_12, "1-2");
checkResult(zeroForNull, ITERABLE_123, "1-2-3");
checkNoOutput(zeroForNull, iterable);
checkResult(zeroForNull, iterable1, "1");
checkResult(zeroForNull, iterable12, "1-2");
checkResult(zeroForNull, iterable123, "1-2-3");
checkResult(J, UNDERREPORTING_SIZE_LIST, "1-2-3");
checkResult(J, OVERREPORTING_SIZE_LIST, "1-2-3");
checkResult(zeroForNull, ITERABLE_NULL, "0");
checkResult(zeroForNull, ITERABLE_NULL_NULL, "0-0");
checkResult(zeroForNull, ITERABLE_NULL_1, "0-1");
checkResult(zeroForNull, ITERABLE_1_NULL, "1-0");
checkResult(zeroForNull, ITERABLE_1_NULL_2, "1-0-2");
checkResult(zeroForNull, ITERABLE_FOUR_NULLS, "0-0-0-0");
checkResult(zeroForNull, iterableNull, "0");
checkResult(zeroForNull, iterableNullNull, "0-0");
checkResult(zeroForNull, iterableNull1, "0-1");
checkResult(zeroForNull, iterable1Null, "1-0");
checkResult(zeroForNull, iterable1Null2, "1-0-2");
checkResult(zeroForNull, iterableFourNulls, "0-0-0-0");
}

private static void checkNoOutput(Joiner joiner, Iterable<Integer> set) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ private Object[] getParametersForSignature(
return params;
}

private static final ImmutableList<Class<?>> possibleParamTypes =
private static final ImmutableList<Class<?>> POSSIBLE_PARAM_TYPES =
ImmutableList.of(char.class, int.class, long.class, Object.class);

/**
Expand All @@ -410,7 +410,7 @@ private static ImmutableList<ImmutableList<Class<?>>> allSignatures(Class<?> pre

List<List<Class<?>>> typesLists = new ArrayList<>();
for (int i = 0; i < 2; i++) {
typesLists.add(possibleParamTypes);
typesLists.add(POSSIBLE_PARAM_TYPES);
for (List<Class<?>> curr : Lists.cartesianProduct(typesLists)) {
allOverloads.add(
ImmutableList.<Class<?>>builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected SortedMap<K, V> makeEmptyMap() {
throw new UnsupportedOperationException();
}

private static final Joiner joiner = Joiner.on(", ");
private static final Joiner JOINER = Joiner.on(", ");

@Override
protected void assertMoreInvariants(Map<K, V> map) {
Expand All @@ -48,10 +48,10 @@ protected void assertMoreInvariants(Map<K, V> map) {
assertEquals(entry.getKey() + "=" + entry.getValue(), entry.toString());
}

assertEquals("{" + joiner.join(map.entrySet()) + "}", map.toString());
assertEquals("[" + joiner.join(map.entrySet()) + "]", map.entrySet().toString());
assertEquals("[" + joiner.join(map.keySet()) + "]", map.keySet().toString());
assertEquals("[" + joiner.join(map.values()) + "]", map.values().toString());
assertEquals("{" + JOINER.join(map.entrySet()) + "}", map.toString());
assertEquals("[" + JOINER.join(map.entrySet()) + "]", map.entrySet().toString());
assertEquals("[" + JOINER.join(map.keySet()) + "]", map.keySet().toString());
assertEquals("[" + JOINER.join(map.values()) + "]", map.values().toString());

assertEquals(newHashSet(map.entrySet()), map.entrySet());
assertEquals(newHashSet(map.keySet()), map.keySet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
import static com.google.common.collect.BoundType.CLOSED;
import static com.google.common.collect.BoundType.OPEN;
import static com.google.common.collect.ReflectionFreeAssertThrows.assertThrows;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.base.Objects;
import com.google.common.testing.NullPointerTester;
import java.util.Arrays;
import java.util.List;
import junit.framework.TestCase;
import org.jspecify.annotations.NullMarked;
Expand All @@ -40,7 +41,8 @@ public class GeneralRangeTest extends TestCase {
private static final Ordering<@Nullable Integer> ORDERING =
Ordering.<Integer>natural().<Integer>nullsFirst();

private static final List<@Nullable Integer> IN_ORDER_VALUES = asList(null, 1, 2, 3, 4, 5);
private static final List<@Nullable Integer> IN_ORDER_VALUES =
unmodifiableList(Arrays.<@Nullable Integer>asList(null, 1, 2, 3, 4, 5));

public void testCreateEmptyRangeFails() {
for (BoundType lboundType : BoundType.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@
@NullUnmarked
public class PackageSanityTests extends AbstractPackageSanityTests {

private static final AbstractGraphBuilder<?> GRAPH_BUILDER_A =
private static final AbstractGraphBuilder<?> graphBuilderA =
GraphBuilder.directed().expectedNodeCount(10);
private static final AbstractGraphBuilder<?> GRAPH_BUILDER_B =
private static final AbstractGraphBuilder<?> graphBuilderB =
ValueGraphBuilder.directed().allowsSelfLoops(true).expectedNodeCount(16);

private static final ImmutableGraph<String> IMMUTABLE_GRAPH_A =
GraphBuilder.directed().<String>immutable().addNode("A").build();
private static final ImmutableGraph<String> IMMUTABLE_GRAPH_B =
GraphBuilder.directed().<String>immutable().addNode("B").build();

private static final NetworkBuilder<?, ?> NETWORK_BUILDER_A =
private static final NetworkBuilder<?, ?> networkBuilderA =
NetworkBuilder.directed().allowsParallelEdges(true).expectedNodeCount(10);
private static final NetworkBuilder<?, ?> NETWORK_BUILDER_B =
private static final NetworkBuilder<?, ?> networkBuilderB =
NetworkBuilder.directed().allowsSelfLoops(true).expectedNodeCount(16);

private static final ImmutableNetwork<String, String> IMMUTABLE_NETWORK_A =
Expand All @@ -57,10 +57,10 @@ public PackageSanityTests() {
MutableNetwork<String, String> mutableNetworkB = NetworkBuilder.directed().build();
mutableNetworkB.addNode("b");

setDistinctValues(AbstractGraphBuilder.class, GRAPH_BUILDER_A, GRAPH_BUILDER_B);
setDistinctValues(AbstractGraphBuilder.class, graphBuilderA, graphBuilderB);
setDistinctValues(Graph.class, IMMUTABLE_GRAPH_A, IMMUTABLE_GRAPH_B);
setDistinctValues(MutableNetwork.class, mutableNetworkA, mutableNetworkB);
setDistinctValues(NetworkBuilder.class, NETWORK_BUILDER_A, NETWORK_BUILDER_B);
setDistinctValues(NetworkBuilder.class, networkBuilderA, networkBuilderB);
setDistinctValues(Network.class, IMMUTABLE_NETWORK_A, IMMUTABLE_NETWORK_B);
setDefault(EndpointPair.class, EndpointPair.ordered("A", "B"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void testAgainstSimplerImplementation() {
private static int referenceCrc(byte[] bytes) {
int crc = ~0;
for (byte b : bytes) {
crc = (crc >>> 8) ^ Crc32cHashFunction.Crc32cHasher.BYTE_TABLE[(crc ^ b) & 0xFF];
crc = (crc >>> 8) ^ Crc32cHashFunction.Crc32cHasher.byteTable[(crc ^ b) & 0xFF];
}
return ~crc;
}
Expand Down Expand Up @@ -169,7 +169,7 @@ public void testCrc32cByteTable() {
expected[i] = crc;
}

int[] actual = Crc32cHashFunction.Crc32cHasher.BYTE_TABLE;
int[] actual = Crc32cHashFunction.Crc32cHasher.byteTable;
assertTrue(
"Expected: \n" + Arrays.toString(expected) + "\nActual:\n" + Arrays.toString(actual),
Arrays.equals(expected, actual));
Expand All @@ -186,7 +186,7 @@ static int advanceOneBit(int next) {
public void testCrc32cStrideTable() {
int next = CRC32C_GENERATOR_FLIPPED;
for (int i = 0; i < 12; i++) { // for 3 ints = 12 bytes in between each stride window
next = (next >>> 8) ^ Crc32cHashFunction.Crc32cHasher.BYTE_TABLE[next & 0xFF];
next = (next >>> 8) ^ Crc32cHashFunction.Crc32cHasher.byteTable[next & 0xFF];
}
int[][] expected = new int[4][256];
for (int b = 0; b < 4; ++b) {
Expand All @@ -204,7 +204,7 @@ public void testCrc32cStrideTable() {
}
}

int[][] actual = Crc32cHashFunction.Crc32cHasher.STRIDE_TABLE;
int[][] actual = Crc32cHashFunction.Crc32cHasher.strideTable;
assertTrue(
"Expected: \n"
+ Arrays.deepToString(expected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static java.lang.Math.min;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import java.io.BufferedReader;
import java.io.FilterReader;
Expand Down Expand Up @@ -58,7 +59,8 @@ public void testProcess() throws IOException {
bufferHelper("mixed\nline\rendings\r\n", "mixed\n", "line\r", "endings\r\n");
}

private static final int[] CHUNK_SIZES = {1, 2, 3, Integer.MAX_VALUE};
private static final ImmutableSet<Integer> CHUNK_SIZES =
ImmutableSet.of(1, 2, 3, Integer.MAX_VALUE);

private static void bufferHelper(String input, String... expect) throws IOException {

Expand Down
26 changes: 14 additions & 12 deletions android/guava-tests/test/com/google/common/math/DoubleMathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;
import junit.framework.TestCase;
import org.jspecify.annotations.NullUnmarked;

Expand Down Expand Up @@ -512,17 +511,20 @@ public void testFactorialNegative() {
ImmutableList.of(-0.0, 0.0, 1.0, 100.0, 10000.0, Double.MAX_VALUE);

private static final Iterable<Double> TOLERANCE_CANDIDATES =
Iterables.concat(FINITE_TOLERANCE_CANDIDATES, ImmutableList.of(Double.POSITIVE_INFINITY));

private static final List<Double> BAD_TOLERANCE_CANDIDATES =
Doubles.asList(
-Double.MIN_VALUE,
-Double.MIN_NORMAL,
-1,
-20,
Double.NaN,
Double.NEGATIVE_INFINITY,
-0.001);
ImmutableList.copyOf(
Iterables.concat(
FINITE_TOLERANCE_CANDIDATES, ImmutableList.of(Double.POSITIVE_INFINITY)));

private static final ImmutableList<Double> BAD_TOLERANCE_CANDIDATES =
ImmutableList.copyOf(
Doubles.asList(
-Double.MIN_VALUE,
-Double.MIN_NORMAL,
-1,
-20,
Double.NaN,
Double.NEGATIVE_INFINITY,
-0.001));

public void testFuzzyEqualsFinite() {
for (double a : FINITE_DOUBLE_CANDIDATES) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.google.common.collect.Sets;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import junit.framework.TestCase;
import org.jspecify.annotations.NullUnmarked;

Expand All @@ -34,21 +33,23 @@
@NullUnmarked
public class QuantilesAlgorithmTest extends TestCase {

private static final Random RNG = new Random(82674067L);
private static final Random rng = new Random(82674067L);
private static final int DATASET_SIZE = 1000;
private static final double ALLOWED_ERROR = 1.0e-10;
private static final QuantilesAlgorithm REFERENCE_ALGORITHM = QuantilesAlgorithm.SORTING;
private static final Set<QuantilesAlgorithm> NON_REFERENCE_ALGORITHMS =
private static final ImmutableSet<QuantilesAlgorithm> NON_REFERENCE_ALGORITHMS =
Sets.difference(
ImmutableSet.copyOf(QuantilesAlgorithm.values()), ImmutableSet.of(REFERENCE_ALGORITHM));
ImmutableSet.copyOf(QuantilesAlgorithm.values()),
ImmutableSet.of(REFERENCE_ALGORITHM))
.immutableCopy();

private double[] dataset;

@Override
protected void setUp() {
dataset = new double[DATASET_SIZE];
for (int i = 0; i < DATASET_SIZE; i++) {
dataset[i] = RNG.nextDouble();
dataset[i] = rng.nextDouble();
}
}

Expand Down
Loading

0 comments on commit 9b05674

Please sign in to comment.