Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the JDK Objects.equals instead of the Guava Objects.equal. #1364

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Objects;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.jspecify.annotations.Nullable;

/**
Expand Down Expand Up @@ -265,7 +265,7 @@ private Transforming(

@Override
public boolean compare(A actual, E expected) {
return Objects.equal(actualTransform.apply(actual), expectedTransform.apply(expected));
return Objects.equals(actualTransform.apply(actual), expectedTransform.apply(expected));
}

@Override
Expand Down Expand Up @@ -333,7 +333,7 @@ private static final class Equality<T extends @Nullable Object> extends Correspo

@Override
public boolean compare(T actual, T expected) {
return Objects.equal(actual, expected);
return Objects.equals(actual, expected);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import static com.google.common.truth.SubjectUtils.retainMatchingToString;

import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
Expand All @@ -66,6 +65,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.jspecify.annotations.Nullable;

Expand Down Expand Up @@ -127,7 +127,7 @@ protected String actualCustomStringRepresentation() {
@Override
public void isEqualTo(@Nullable Object expected) {
@SuppressWarnings("UndefinedEquals") // method contract requires testing iterables for equality
boolean equal = Objects.equal(actual, expected);
boolean equal = Objects.equals(actual, expected);
if (equal) {
return;
}
Expand Down Expand Up @@ -460,7 +460,7 @@ private Ordered containsExactlyElementsIn(
// cannot succeed, so we can check the rest of the elements more normally.
// Since any previous pairs of elements we iterated over were equal, they have no
// effect on the result now.
if (!Objects.equal(actualElement, requiredElement)) {
if (!Objects.equals(actualElement, requiredElement)) {
if (isFirst && !actualIter.hasNext() && !requiredIter.hasNext()) {
/*
* There's exactly one actual element and exactly one expected element, and they don't
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/com/google/common/truth/MapSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import static com.google.common.truth.SubjectUtils.retainMatchingToString;
import static java.util.Collections.singletonList;

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.LinkedHashMultiset;
Expand All @@ -41,6 +40,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.jspecify.annotations.Nullable;

Expand All @@ -64,7 +64,7 @@ protected MapSubject(FailureMetadata metadata, @Nullable Map<?, ?> map) {

@Override
public final void isEqualTo(@Nullable Object other) {
if (Objects.equal(actual, other)) {
if (Objects.equals(actual, other)) {
return;
}

Expand Down Expand Up @@ -141,7 +141,7 @@ public final void containsEntry(@Nullable Object key, @Nullable Object value) {
} else if (actual.containsValue(value)) {
Set<@Nullable Object> keys = new LinkedHashSet<>();
for (Map.Entry<?, ?> actualEntry : actual.entrySet()) {
if (Objects.equal(actualEntry.getValue(), value)) {
if (Objects.equals(actualEntry.getValue(), value)) {
keys.add(actualEntry.getKey());
}
}
Expand Down Expand Up @@ -260,7 +260,7 @@ public final Ordered containsAtLeastEntriesIn(Map<?, ?> expectedMap) {
@CanIgnoreReturnValue
private boolean containsEntriesInAnyOrder(Map<?, ?> expectedMap, boolean allowUnexpected) {
MapDifference<@Nullable Object, @Nullable Object, @Nullable Object> diff =
MapDifference.create(checkNotNull(actual), expectedMap, allowUnexpected, Objects::equal);
MapDifference.create(checkNotNull(actual), expectedMap, allowUnexpected, Objects::equals);
if (diff.isEmpty()) {
return true;
}
Expand Down
14 changes: 7 additions & 7 deletions core/src/main/java/com/google/common/truth/MultimapSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import static com.google.common.truth.SubjectUtils.retainMatchingToString;

import com.google.common.base.Joiner;
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.LinkedHashMultiset;
import com.google.common.collect.LinkedListMultimap;
Expand All @@ -45,6 +44,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.jspecify.annotations.Nullable;

Expand Down Expand Up @@ -136,7 +136,7 @@ public final void containsEntry(@Nullable Object key, @Nullable Object value) {
} else if (actual.containsValue(value)) {
Set<@Nullable Object> keys = new LinkedHashSet<>();
for (Map.Entry<?, ?> actualEntry : actual.entries()) {
if (Objects.equal(actualEntry.getValue(), value)) {
if (Objects.equals(actualEntry.getValue(), value)) {
keys.add(actualEntry.getKey());
}
}
Expand Down Expand Up @@ -182,7 +182,7 @@ public IterableSubject valuesForKey(@Nullable Object key) {
@Override
public final void isEqualTo(@Nullable Object other) {
@SuppressWarnings("UndefinedEquals") // the contract of this method is to follow Multimap.equals
boolean isEqual = Objects.equal(actual, other);
boolean isEqual = Objects.equals(actual, other);
if (isEqual) {
return;
}
Expand Down Expand Up @@ -438,7 +438,7 @@ public void inOrder() {
*/
private static boolean advanceToFind(Iterator<?> iterator, @Nullable Object value) {
while (iterator.hasNext()) {
if (Objects.equal(iterator.next(), value)) {
if (Objects.equals(iterator.next(), value)) {
return true;
}
}
Expand Down Expand Up @@ -503,9 +503,9 @@ private static String countDuplicatesMultimap(Multimap<?, ?> multimap) {
LinkedListMultimap.create();
for (Map.Entry<?, ?> entry : multimap.entries()) {
Object key =
Objects.equal(entry.getKey(), "") ? HUMAN_UNDERSTANDABLE_EMPTY_STRING : entry.getKey();
Objects.equals(entry.getKey(), "") ? HUMAN_UNDERSTANDABLE_EMPTY_STRING : entry.getKey();
Object value =
Objects.equal(entry.getValue(), "")
Objects.equals(entry.getValue(), "")
? HUMAN_UNDERSTANDABLE_EMPTY_STRING
: entry.getValue();
annotatedMultimap.put(key, value);
Expand Down Expand Up @@ -821,7 +821,7 @@ Correspondence<Map.Entry<K, A>, Map.Entry<K, E>> entryCorrespondence(
Correspondence<? super A, ? super E> valueCorrespondence) {
return Correspondence.from(
(Map.Entry<K, A> actual, Map.Entry<K, E> expected) ->
Objects.equal(actual.getKey(), expected.getKey())
Objects.equals(actual.getKey(), expected.getKey())
&& valueCorrespondence.compare(actual.getValue(), expected.getValue()),
lenientFormat(
"has a key that is equal to and a value that %s the key and value of",
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/com/google/common/truth/Subject.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import static com.google.common.truth.SubjectUtils.sandwich;
import static java.util.Arrays.asList;

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
Expand All @@ -53,6 +52,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.jspecify.annotations.Nullable;

/**
Expand Down Expand Up @@ -628,7 +628,7 @@ private static boolean gwtSafeObjectEquals(@Nullable Object actual, @Nullable Ob
} else if (actual instanceof Float && expected instanceof Float) {
return Float.floatToIntBits((Float) actual) == Float.floatToIntBits((Float) expected);
} else {
return Objects.equal(actual, expected);
return Objects.equals(actual, expected);
}
}

Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/com/google/common/truth/SubjectUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import com.google.common.base.Equivalence;
import com.google.common.base.Equivalence.Wrapper;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableList;
Expand All @@ -37,6 +36,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.jspecify.annotations.Nullable;

/**
Expand Down Expand Up @@ -187,7 +187,7 @@ public String toString() {
new Equivalence<Object>() {
@Override
protected boolean doEquivalent(Object a, Object b) {
return Objects.equal(a, b);
return Objects.equals(a, b);
}

@Override
Expand Down Expand Up @@ -270,7 +270,7 @@ static String iterableToStringWithTypeInfo(Iterable<?> itemsIterable) {
List<@Nullable Object> result = Lists.newArrayList();
for (Object item : items) {
for (Object itemToCheck : stringValueToItemsToCheck.get(String.valueOf(item))) {
if (!Objects.equal(itemToCheck, item)) {
if (!Objects.equals(itemToCheck, item)) {
result.add(item);
break;
}
Expand Down Expand Up @@ -373,7 +373,7 @@ private static List<String> addTypeInfoToEveryItem(Iterable<?> items) {
if (Iterables.contains(items, "")) {
List<T> annotatedItems = Lists.newArrayList();
for (T item : items) {
if (Objects.equal(item, "")) {
if (Objects.equals(item, "")) {
// This is a safe cast because know that at least one instance of T (this item) is a
// String.
@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import static com.google.common.truth.Fact.fact;
import static com.google.common.truth.Fact.simpleFact;

import com.google.common.base.Objects;
import com.google.common.truth.FailureMetadata;
import com.google.common.truth.IntegerSubject;
import com.google.common.truth.Subject;
import com.google.errorprone.annotations.CheckReturnValue;
import com.google.protobuf.MessageLite;
import java.util.Objects;
import java.util.regex.Pattern;
import org.jspecify.annotations.Nullable;

Expand Down Expand Up @@ -108,7 +108,7 @@ final String actualCustomStringRepresentationForProtoPackageMembersToCall() {
@Override
public void isEqualTo(@Nullable Object expected) {
// TODO(user): Do better here when MessageLite descriptors are available.
if (Objects.equal(actual, expected)) {
if (Objects.equals(actual, expected)) {
return;
}

Expand Down Expand Up @@ -175,7 +175,7 @@ private static final class LiteProtoAsStringSubject extends Subject {

@Override
public void isNotEqualTo(@Nullable Object expected) {
if (Objects.equal(actual, expected)) {
if (Objects.equals(actual, expected)) {
if (actual == null) {
super.isNotEqualTo(expected);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
import static com.google.common.truth.Fact.simpleFact;
import static com.google.common.truth.extensions.proto.FieldScopeUtil.asList;

import com.google.common.base.Objects;
import com.google.common.truth.FailureMetadata;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.Message;
import com.google.protobuf.TypeRegistry;
import java.util.Arrays;
import java.util.Objects;
import org.jspecify.annotations.Nullable;

/**
Expand Down Expand Up @@ -895,7 +895,7 @@ public void isNotEqualTo(@Nullable Message expected) {
*/
boolean testIsEqualTo(@Nullable Message expected) {
if (notMessagesWithSameDescriptor(protoSubject.actual, expected)) {
return Objects.equal(protoSubject.actual, expected);
return Objects.equals(protoSubject.actual, expected);
} else {
return protoSubject
.makeDifferencer(expected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand All @@ -48,6 +47,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.jspecify.annotations.Nullable;

Expand Down Expand Up @@ -749,7 +749,7 @@ private SingularField compareSingularPrimitive(
config.floatCorrespondenceMap().get(rootDescriptor, subScopeId)
));
} else {
result.markModifiedIf(!Objects.equal(actual, expected));
result.markModifiedIf(!Objects.equals(actual, expected));
}
}

Expand Down Expand Up @@ -935,7 +935,7 @@ private SingularField compareUnknownPrimitive(

result.markRemovedIf(actual == null);
result.markAddedIf(expected == null);
result.markModifiedIf(!Objects.equal(actual, expected));
result.markModifiedIf(!Objects.equals(actual, expected));

SingularField.Builder singularFieldBuilder =
SingularField.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.google.common.truth.refactorings;

import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE;
import static com.google.common.base.Objects.equal;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.ImmutableSetMultimap.toImmutableSetMultimap;
Expand Down Expand Up @@ -80,6 +79,7 @@
import com.sun.tools.javac.tree.JCTree;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import javax.lang.model.element.Modifier;
Expand Down Expand Up @@ -333,7 +333,7 @@ private static Set<Tree> findTypeReferences(Symbol classSymbol, VisitorState sta
new TreeScanner<Void, Void>() {
@Override
public @Nullable Void scan(Tree node, Void unused) {
if (equal(getSymbol(node), classSymbol)
if (Objects.equals(getSymbol(node), classSymbol)
&& getDeclaredSymbol(node) == null // Don't touch the ClassTree that we're replacing.
) {
references.add(node);
Expand Down