Skip to content

Commit d650068

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
It's kind of amazing how much this improves readability in some cases. (Also, use `getOrDefault` in one place where we can, and suppress some suggestions to use it in places where we can't.) RELNOTES=n/a PiperOrigin-RevId: 725869779
1 parent 45f57f3 commit d650068

31 files changed

+85
-99
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
@NullUnmarked
3333
public class ObjectsTest extends TestCase {
3434

35+
@SuppressWarnings("YodaCondition") // test of reversed call
3536
public void testEqual() throws Exception {
3637
assertTrue(Objects.equal(1, 1));
3738
assertTrue(Objects.equal(null, null));

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.Iterator;
3838
import java.util.List;
3939
import java.util.NoSuchElementException;
40+
import java.util.Objects;
4041
import junit.framework.Test;
4142
import junit.framework.TestCase;
4243
import junit.framework.TestSuite;
@@ -68,7 +69,7 @@ public static Test suite() {
6869
}
6970

7071
static final Predicate<@Nullable String> NOT_YYY_ZZZ =
71-
input -> !"yyy".equals(input) && !"zzz".equals(input);
72+
input -> !Objects.equals(input, "yyy") && !Objects.equals(input, "zzz");
7273

7374
static final Predicate<String> LENGTH_1 = input -> input.length() == 1;
7475

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

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.common.annotations.GwtIncompatible;
2424
import com.google.common.base.Predicate;
2525
import java.util.Map.Entry;
26+
import java.util.Objects;
2627
import junit.framework.TestCase;
2728
import org.jspecify.annotations.NullUnmarked;
2829

@@ -36,12 +37,8 @@
3637
public class FilteredMultimapTest extends TestCase {
3738

3839
private static final Predicate<Entry<String, Integer>> ENTRY_PREDICATE =
39-
new Predicate<Entry<String, Integer>>() {
40-
@Override
41-
public boolean apply(Entry<String, Integer> entry) {
42-
return !"badkey".equals(entry.getKey()) && !((Integer) 55556).equals(entry.getValue());
43-
}
44-
};
40+
entry ->
41+
!Objects.equals(entry.getKey(), "badkey") && !Objects.equals(entry.getValue(), 55556);
4542

4643
protected Multimap<String, Integer> create() {
4744
Multimap<String, Integer> unfiltered = HashMultimap.create();
@@ -50,13 +47,7 @@ protected Multimap<String, Integer> create() {
5047
return Multimaps.filterEntries(unfiltered, ENTRY_PREDICATE);
5148
}
5249

53-
private static final Predicate<String> KEY_PREDICATE =
54-
new Predicate<String>() {
55-
@Override
56-
public boolean apply(String key) {
57-
return !"badkey".equals(key);
58-
}
59-
};
50+
private static final Predicate<String> KEY_PREDICATE = key -> !Objects.equals(key, "badkey");
6051

6152
public void testFilterKeys() {
6253
Multimap<String, Integer> unfiltered = HashMultimap.create();
@@ -67,13 +58,7 @@ public void testFilterKeys() {
6758
assertTrue(filtered.containsEntry("foo", 55556));
6859
}
6960

70-
private static final Predicate<Integer> VALUE_PREDICATE =
71-
new Predicate<Integer>() {
72-
@Override
73-
public boolean apply(Integer value) {
74-
return !((Integer) 55556).equals(value);
75-
}
76-
};
61+
private static final Predicate<Integer> VALUE_PREDICATE = value -> !Objects.equals(value, 55556);
7762

7863
public void testFilterValues() {
7964
Multimap<String, Integer> unfiltered = HashMultimap.create();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.util.Map.Entry;
4747
import java.util.NavigableMap;
4848
import java.util.NavigableSet;
49+
import java.util.Objects;
4950
import java.util.Set;
5051
import java.util.SortedMap;
5152
import java.util.SortedSet;
@@ -565,15 +566,15 @@ static void putEntries(Map<String, String> map, Entry<String, String>[] entries)
565566
new Predicate<String>() {
566567
@Override
567568
public boolean apply(@Nullable String string) {
568-
return !"banana".equals(string) && !"eggplant".equals(string);
569+
return !Objects.equals(string, "banana") && !Objects.equals(string, "eggplant");
569570
}
570571
};
571572

572573
static final Predicate<String> FILTER_VALUES =
573574
new Predicate<String>() {
574575
@Override
575576
public boolean apply(@Nullable String string) {
576-
return !"toast".equals(string) && !"spam".equals(string);
577+
return !Objects.equals(string, "toast") && !Objects.equals(string, "spam");
577578
}
578579
};
579580

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collection;
2222
import java.util.Map;
2323
import java.util.Map.Entry;
24+
import java.util.Objects;
2425
import org.jspecify.annotations.NullUnmarked;
2526

2627
/**
@@ -35,7 +36,7 @@ public class MultimapsFilterEntriesAsMapTest extends AbstractMultimapAsMapImplem
3536
new Predicate<Entry<String, Integer>>() {
3637
@Override
3738
public boolean apply(Entry<String, Integer> entry) {
38-
return !"badkey".equals(entry.getKey()) && 55556 != entry.getValue();
39+
return !Objects.equals(entry.getKey(), "badkey") && entry.getValue() != 55556;
3940
}
4041
};
4142

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,7 @@ public int count(@Nullable Object element) {
122122
@Override
123123
public int add(E element, int occurrences) {
124124
checkArgument(occurrences >= 0);
125-
Integer frequency = backingMap.get(element);
126-
if (frequency == null) {
127-
frequency = 0;
128-
}
125+
Integer frequency = backingMap.getOrDefault(element, 0);
129126
if (occurrences == 0) {
130127
return frequency;
131128
}

android/guava-tests/test/com/google/common/util/concurrent/UninterruptiblesTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.common.util.concurrent;
1818

19+
import static com.google.common.truth.Truth.assertThat;
1920
import static com.google.common.util.concurrent.InterruptionUtil.repeatedlyInterruptTestThread;
2021
import static com.google.common.util.concurrent.Uninterruptibles.awaitTerminationUninterruptibly;
2122
import static com.google.common.util.concurrent.Uninterruptibles.awaitUninterruptibly;
@@ -783,7 +784,7 @@ void joinSuccessfully(long timeoutMillis) {
783784
void joinUnsuccessfully(long timeoutMillis) {
784785
Uninterruptibles.joinUninterruptibly(thread, timeoutMillis, MILLISECONDS);
785786
completed.assertCompletionNotExpected(timeoutMillis);
786-
assertFalse(Thread.State.TERMINATED.equals(thread.getState()));
787+
assertThat(thread.getState()).isNotEqualTo(Thread.State.TERMINATED);
787788
}
788789
}
789790

android/guava/src/com/google/common/base/SmallCharMatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static int smear(int hashCode) {
5555
}
5656

5757
private boolean checkFilter(int c) {
58-
return 1 == (1 & (filter >> c));
58+
return ((filter >> c) & 1) == 1;
5959
}
6060

6161
// This is all essentially copied from ImmutableSet, but we have to duplicate because

android/guava/src/com/google/common/base/Utf8.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ private static boolean isWellFormedSlowPath(byte[] bytes, int off, int end) {
164164
// Overlong? 5 most significant bits must not all be zero.
165165
|| (byte1 == (byte) 0xE0 && byte2 < (byte) 0xA0)
166166
// Check for illegal surrogate codepoints.
167-
|| (byte1 == (byte) 0xED && (byte) 0xA0 <= byte2)
167+
|| (byte1 == (byte) 0xED && byte2 >= (byte) 0xA0)
168168
// Third byte trailing-byte test.
169169
|| bytes[index++] > (byte) 0xBF) {
170170
return false;

android/guava/src/com/google/common/hash/LittleEndianByteArray.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.security.AccessController;
2424
import java.security.PrivilegedActionException;
2525
import java.security.PrivilegedExceptionAction;
26+
import java.util.Objects;
2627
import sun.misc.Unsafe;
2728

2829
/**
@@ -267,7 +268,7 @@ static LittleEndianBytes makeGetter() {
267268
*
268269
*/
269270
String arch = System.getProperty("os.arch");
270-
if ("amd64".equals(arch)) {
271+
if (Objects.equals(arch, "amd64")) {
271272
return ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)
272273
? UnsafeByteArray.UNSAFE_LITTLE_ENDIAN
273274
: UnsafeByteArray.UNSAFE_BIG_ENDIAN;

0 commit comments

Comments
 (0)