From 31e245a4f52d09b85fe3dcafb6802d48f6f1b251 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Tue, 17 Dec 2019 11:51:59 -0500 Subject: [PATCH] Replace some ObjecsHelpers methods with Java 8 APIs --- .../reactivex/rxjava3/core/Notification.java | 3 +- .../rxjava3/internal/functions/Functions.java | 2 +- .../internal/functions/ObjectHelper.java | 42 +------------------ .../operators/maybe/MaybeContains.java | 4 +- .../operators/single/SingleEquals.java | 4 +- .../schedulers/TrampolineScheduler.java | 5 +-- .../internal/util/NotificationLite.java | 4 +- .../rxjava3/observers/BaseTestConsumer.java | 8 ++-- .../rxjava3/schedulers/TestScheduler.java | 5 +-- .../reactivex/rxjava3/schedulers/Timed.java | 5 ++- .../internal/functions/ObjectHelperTest.java | 21 ---------- .../testsupport/BaseTestConsumerEx.java | 6 +-- .../rxjava3/testsupport/TestHelper.java | 5 +-- 13 files changed, 27 insertions(+), 87 deletions(-) diff --git a/src/main/java/io/reactivex/rxjava3/core/Notification.java b/src/main/java/io/reactivex/rxjava3/core/Notification.java index 23139896cb..b3270483a7 100644 --- a/src/main/java/io/reactivex/rxjava3/core/Notification.java +++ b/src/main/java/io/reactivex/rxjava3/core/Notification.java @@ -16,6 +16,7 @@ import io.reactivex.rxjava3.annotations.*; import io.reactivex.rxjava3.internal.functions.ObjectHelper; import io.reactivex.rxjava3.internal.util.NotificationLite; +import java.util.Objects; /** * Represents the reactive signal types: onNext, onError and onComplete and @@ -95,7 +96,7 @@ public Throwable getError() { public boolean equals(Object obj) { if (obj instanceof Notification) { Notification n = (Notification) obj; - return ObjectHelper.equals(value, n.value); + return Objects.equals(value, n.value); } return false; } diff --git a/src/main/java/io/reactivex/rxjava3/internal/functions/Functions.java b/src/main/java/io/reactivex/rxjava3/internal/functions/Functions.java index 87aae2e1e8..2e44b86d04 100644 --- a/src/main/java/io/reactivex/rxjava3/internal/functions/Functions.java +++ b/src/main/java/io/reactivex/rxjava3/internal/functions/Functions.java @@ -275,7 +275,7 @@ static final class EqualsPredicate implements Predicate { @Override public boolean test(T t) throws Exception { - return ObjectHelper.equals(t, value); + return Objects.equals(t, value); } } diff --git a/src/main/java/io/reactivex/rxjava3/internal/functions/ObjectHelper.java b/src/main/java/io/reactivex/rxjava3/internal/functions/ObjectHelper.java index 981d697ec8..65f5a70c82 100644 --- a/src/main/java/io/reactivex/rxjava3/internal/functions/ObjectHelper.java +++ b/src/main/java/io/reactivex/rxjava3/internal/functions/ObjectHelper.java @@ -13,6 +13,7 @@ package io.reactivex.rxjava3.internal.functions; import io.reactivex.rxjava3.functions.BiPredicate; +import java.util.Objects; /** * Utility methods containing the backport of Java 7's Objects utility class. @@ -41,45 +42,6 @@ public static T requireNonNull(T object, String message) { return object; } - /** - * Compares two potentially null objects with each other using Object.equals. - * @param o1 the first object - * @param o2 the second object - * @return the comparison result - */ - public static boolean equals(Object o1, Object o2) { // NOPMD - return o1 == o2 || (o1 != null && o1.equals(o2)); - } - - /** - * Returns the hashCode of a non-null object or zero for a null object. - * @param o the object to get the hashCode for. - * @return the hashCode - */ - public static int hashCode(Object o) { - return o != null ? o.hashCode() : 0; - } - - /** - * Compares two integer values similar to Integer.compare. - * @param v1 the first value - * @param v2 the second value - * @return the comparison result - */ - public static int compare(int v1, int v2) { - return v1 < v2 ? -1 : (v1 > v2 ? 1 : 0); - } - - /** - * Compares two long values similar to Long.compare. - * @param v1 the first value - * @param v2 the second value - * @return the comparison result - */ - public static int compare(long v1, long v2) { - return v1 < v2 ? -1 : (v1 > v2 ? 1 : 0); - } - static final BiPredicate EQUALS = new BiObjectPredicate(); /** @@ -125,7 +87,7 @@ public static long verifyPositive(long value, String paramName) { static final class BiObjectPredicate implements BiPredicate { @Override public boolean test(Object o1, Object o2) { - return ObjectHelper.equals(o1, o2); + return Objects.equals(o1, o2); } } } diff --git a/src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeContains.java b/src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeContains.java index 55b68fec3d..8737c6ae37 100644 --- a/src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeContains.java +++ b/src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeContains.java @@ -16,8 +16,8 @@ import io.reactivex.rxjava3.core.*; import io.reactivex.rxjava3.disposables.Disposable; import io.reactivex.rxjava3.internal.disposables.DisposableHelper; -import io.reactivex.rxjava3.internal.functions.ObjectHelper; import io.reactivex.rxjava3.internal.fuseable.HasUpstreamMaybeSource; +import java.util.Objects; /** * Signals true if the source signals a value that is object-equals with the provided @@ -81,7 +81,7 @@ public void onSubscribe(Disposable d) { @Override public void onSuccess(Object value) { upstream = DisposableHelper.DISPOSED; - downstream.onSuccess(ObjectHelper.equals(value, this.value)); + downstream.onSuccess(Objects.equals(value, this.value)); } @Override diff --git a/src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleEquals.java b/src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleEquals.java index 8a9f62473f..0a839ab747 100644 --- a/src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleEquals.java +++ b/src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleEquals.java @@ -13,11 +13,11 @@ package io.reactivex.rxjava3.internal.operators.single; +import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; import io.reactivex.rxjava3.core.*; import io.reactivex.rxjava3.disposables.*; -import io.reactivex.rxjava3.internal.functions.ObjectHelper; import io.reactivex.rxjava3.plugins.RxJavaPlugins; public final class SingleEquals extends Single { @@ -68,7 +68,7 @@ public void onSuccess(T value) { values[index] = value; if (count.incrementAndGet() == 2) { - downstream.onSuccess(ObjectHelper.equals(values[0], values[1])); + downstream.onSuccess(Objects.equals(values[0], values[1])); } } diff --git a/src/main/java/io/reactivex/rxjava3/internal/schedulers/TrampolineScheduler.java b/src/main/java/io/reactivex/rxjava3/internal/schedulers/TrampolineScheduler.java index cc61d797e9..71c054258b 100644 --- a/src/main/java/io/reactivex/rxjava3/internal/schedulers/TrampolineScheduler.java +++ b/src/main/java/io/reactivex/rxjava3/internal/schedulers/TrampolineScheduler.java @@ -23,7 +23,6 @@ import io.reactivex.rxjava3.core.Scheduler; import io.reactivex.rxjava3.disposables.*; import io.reactivex.rxjava3.internal.disposables.EmptyDisposable; -import io.reactivex.rxjava3.internal.functions.ObjectHelper; import io.reactivex.rxjava3.plugins.RxJavaPlugins; /** @@ -165,9 +164,9 @@ static final class TimedRunnable implements Comparable { @Override public int compareTo(TimedRunnable that) { - int result = ObjectHelper.compare(execTime, that.execTime); + int result = Long.compare(execTime, that.execTime); if (result == 0) { - return ObjectHelper.compare(count, that.count); + return Integer.compare(count, that.count); } return result; } diff --git a/src/main/java/io/reactivex/rxjava3/internal/util/NotificationLite.java b/src/main/java/io/reactivex/rxjava3/internal/util/NotificationLite.java index dbc54c788a..c37aa71da2 100644 --- a/src/main/java/io/reactivex/rxjava3/internal/util/NotificationLite.java +++ b/src/main/java/io/reactivex/rxjava3/internal/util/NotificationLite.java @@ -14,11 +14,11 @@ import java.io.Serializable; +import java.util.Objects; import org.reactivestreams.*; import io.reactivex.rxjava3.core.Observer; import io.reactivex.rxjava3.disposables.Disposable; -import io.reactivex.rxjava3.internal.functions.ObjectHelper; /** * Lightweight notification handling utility class. @@ -52,7 +52,7 @@ public int hashCode() { public boolean equals(Object obj) { if (obj instanceof ErrorNotification) { ErrorNotification n = (ErrorNotification) obj; - return ObjectHelper.equals(e, n.e); + return Objects.equals(e, n.e); } return false; } diff --git a/src/main/java/io/reactivex/rxjava3/observers/BaseTestConsumer.java b/src/main/java/io/reactivex/rxjava3/observers/BaseTestConsumer.java index 59aa62e796..f8a0356a75 100644 --- a/src/main/java/io/reactivex/rxjava3/observers/BaseTestConsumer.java +++ b/src/main/java/io/reactivex/rxjava3/observers/BaseTestConsumer.java @@ -284,7 +284,7 @@ public final U assertValue(T value) { throw fail("expected: " + valueAndClass(value) + " but was: " + values); } T v = values.get(0); - if (!ObjectHelper.equals(value, v)) { + if (!Objects.equals(value, v)) { throw fail("expected: " + valueAndClass(value) + " but was: " + valueAndClass(v)); } return (U)this; @@ -330,7 +330,7 @@ public final U assertValueAt(int index, T value) { } T v = values.get(index); - if (!ObjectHelper.equals(value, v)) { + if (!Objects.equals(value, v)) { throw fail("expected: " + valueAndClass(value) + " but was: " + valueAndClass(v)); } return (U)this; @@ -421,7 +421,7 @@ public final U assertValues(T... values) { for (int i = 0; i < s; i++) { T v = this.values.get(i); T u = values[i]; - if (!ObjectHelper.equals(u, v)) { + if (!Objects.equals(u, v)) { throw fail("Values at position " + i + " differ; expected: " + valueAndClass(u) + " but was: " + valueAndClass(v)); } } @@ -466,7 +466,7 @@ public final U assertValueSequence(Iterable sequence) { T u = expectedIterator.next(); T v = actualIterator.next(); - if (!ObjectHelper.equals(u, v)) { + if (!Objects.equals(u, v)) { throw fail("Values at position " + i + " differ; expected: " + valueAndClass(u) + " but was: " + valueAndClass(v)); } i++; diff --git a/src/main/java/io/reactivex/rxjava3/schedulers/TestScheduler.java b/src/main/java/io/reactivex/rxjava3/schedulers/TestScheduler.java index 33a5b943e1..8b70760236 100644 --- a/src/main/java/io/reactivex/rxjava3/schedulers/TestScheduler.java +++ b/src/main/java/io/reactivex/rxjava3/schedulers/TestScheduler.java @@ -20,7 +20,6 @@ import io.reactivex.rxjava3.core.Scheduler; import io.reactivex.rxjava3.disposables.*; import io.reactivex.rxjava3.internal.disposables.EmptyDisposable; -import io.reactivex.rxjava3.internal.functions.ObjectHelper; /** * A special, non thread-safe scheduler for testing operators that require @@ -76,9 +75,9 @@ public String toString() { @Override public int compareTo(TimedRunnable o) { if (time == o.time) { - return ObjectHelper.compare(count, o.count); + return Long.compare(count, o.count); } - return ObjectHelper.compare(time, o.time); + return Long.compare(time, o.time); } } diff --git a/src/main/java/io/reactivex/rxjava3/schedulers/Timed.java b/src/main/java/io/reactivex/rxjava3/schedulers/Timed.java index b7ac9c828f..fff21ad682 100644 --- a/src/main/java/io/reactivex/rxjava3/schedulers/Timed.java +++ b/src/main/java/io/reactivex/rxjava3/schedulers/Timed.java @@ -13,6 +13,7 @@ package io.reactivex.rxjava3.schedulers; +import java.util.Objects; import java.util.concurrent.TimeUnit; import io.reactivex.rxjava3.annotations.NonNull; @@ -80,9 +81,9 @@ public long time(@NonNull TimeUnit unit) { public boolean equals(Object other) { if (other instanceof Timed) { Timed o = (Timed) other; - return ObjectHelper.equals(value, o.value) + return Objects.equals(value, o.value) && time == o.time - && ObjectHelper.equals(unit, o.unit); + && Objects.equals(unit, o.unit); } return false; } diff --git a/src/test/java/io/reactivex/rxjava3/internal/functions/ObjectHelperTest.java b/src/test/java/io/reactivex/rxjava3/internal/functions/ObjectHelperTest.java index 75c78f4746..16c182061d 100644 --- a/src/test/java/io/reactivex/rxjava3/internal/functions/ObjectHelperTest.java +++ b/src/test/java/io/reactivex/rxjava3/internal/functions/ObjectHelperTest.java @@ -27,13 +27,6 @@ public void utilityClass() { TestHelper.checkUtilityClass(ObjectHelper.class); } - @Test - public void hashCodeOf() { - assertEquals(0, ObjectHelper.hashCode(null)); - - assertEquals(((Integer)1).hashCode(), ObjectHelper.hashCode(1)); - } - @Test public void verifyPositiveInt() throws Exception { assertEquals(1, ObjectHelper.verifyPositive(1, "param")); @@ -53,18 +46,4 @@ public void verifyPositiveIntFail() throws Exception { public void verifyPositiveLongFail() throws Exception { assertEquals(-1L, ObjectHelper.verifyPositive(-1L, "param")); } - - @Test - public void compare() { - assertEquals(-1, ObjectHelper.compare(0, 2)); - assertEquals(0, ObjectHelper.compare(0, 0)); - assertEquals(1, ObjectHelper.compare(2, 0)); - } - - @Test - public void compareLong() { - assertEquals(-1, ObjectHelper.compare(0L, 2L)); - assertEquals(0, ObjectHelper.compare(0L, 0L)); - assertEquals(1, ObjectHelper.compare(2L, 0L)); - } } diff --git a/src/test/java/io/reactivex/rxjava3/testsupport/BaseTestConsumerEx.java b/src/test/java/io/reactivex/rxjava3/testsupport/BaseTestConsumerEx.java index d997a065ba..2c68b0151a 100644 --- a/src/test/java/io/reactivex/rxjava3/testsupport/BaseTestConsumerEx.java +++ b/src/test/java/io/reactivex/rxjava3/testsupport/BaseTestConsumerEx.java @@ -16,10 +16,10 @@ import java.util.List; import io.reactivex.rxjava3.functions.Predicate; -import io.reactivex.rxjava3.internal.functions.ObjectHelper; import io.reactivex.rxjava3.internal.fuseable.QueueFuseable; import io.reactivex.rxjava3.internal.util.ExceptionHelper; import io.reactivex.rxjava3.observers.BaseTestConsumer; +import java.util.Objects; /** * Base class with shared infrastructure to support TestSubscriber and TestObserver. @@ -74,7 +74,7 @@ public final U assertNever(T value) { for (int i = 0; i < s; i++) { T v = this.values.get(i); - if (ObjectHelper.equals(v, value)) { + if (Objects.equals(v, value)) { throw fail("Value at position " + i + " is equal to " + valueAndClass(value) + "; Expected them to be different"); } } @@ -158,7 +158,7 @@ public final U assertErrorMessage(String message) { if (s == 1) { Throwable e = errors.get(0); String errorMessage = e.getMessage(); - if (!ObjectHelper.equals(message, errorMessage)) { + if (!Objects.equals(message, errorMessage)) { throw fail("Error message differs; exptected: " + message + " but was: " + errorMessage); } } else { diff --git a/src/test/java/io/reactivex/rxjava3/testsupport/TestHelper.java b/src/test/java/io/reactivex/rxjava3/testsupport/TestHelper.java index 1f2a834650..1ce53f01ae 100644 --- a/src/test/java/io/reactivex/rxjava3/testsupport/TestHelper.java +++ b/src/test/java/io/reactivex/rxjava3/testsupport/TestHelper.java @@ -36,7 +36,6 @@ import io.reactivex.rxjava3.disposables.*; import io.reactivex.rxjava3.exceptions.*; import io.reactivex.rxjava3.functions.*; -import io.reactivex.rxjava3.internal.functions.ObjectHelper; import io.reactivex.rxjava3.internal.fuseable.*; import io.reactivex.rxjava3.internal.operators.completable.CompletableToFlowable; import io.reactivex.rxjava3.internal.operators.maybe.MaybeToFlowable; @@ -196,7 +195,7 @@ public static void assertError(List list, int index, Class list, int index, Class