Skip to content

Commit 652f2a1

Browse files
JakeWhartonakarnokd
authored andcommitted
Replace some ObjecsHelpers methods with Java 8 APIs (#6770)
1 parent 2392349 commit 652f2a1

File tree

13 files changed

+27
-87
lines changed

13 files changed

+27
-87
lines changed

src/main/java/io/reactivex/rxjava3/core/Notification.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import io.reactivex.rxjava3.annotations.*;
1717
import io.reactivex.rxjava3.internal.functions.ObjectHelper;
1818
import io.reactivex.rxjava3.internal.util.NotificationLite;
19+
import java.util.Objects;
1920

2021
/**
2122
* Represents the reactive signal types: onNext, onError and onComplete and
@@ -95,7 +96,7 @@ public Throwable getError() {
9596
public boolean equals(Object obj) {
9697
if (obj instanceof Notification) {
9798
Notification<?> n = (Notification<?>) obj;
98-
return ObjectHelper.equals(value, n.value);
99+
return Objects.equals(value, n.value);
99100
}
100101
return false;
101102
}

src/main/java/io/reactivex/rxjava3/internal/functions/Functions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static final class EqualsPredicate<T> implements Predicate<T> {
275275

276276
@Override
277277
public boolean test(T t) throws Exception {
278-
return ObjectHelper.equals(t, value);
278+
return Objects.equals(t, value);
279279
}
280280
}
281281

src/main/java/io/reactivex/rxjava3/internal/functions/ObjectHelper.java

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package io.reactivex.rxjava3.internal.functions;
1414

1515
import io.reactivex.rxjava3.functions.BiPredicate;
16+
import java.util.Objects;
1617

1718
/**
1819
* Utility methods containing the backport of Java 7's Objects utility class.
@@ -41,45 +42,6 @@ public static <T> T requireNonNull(T object, String message) {
4142
return object;
4243
}
4344

44-
/**
45-
* Compares two potentially null objects with each other using Object.equals.
46-
* @param o1 the first object
47-
* @param o2 the second object
48-
* @return the comparison result
49-
*/
50-
public static boolean equals(Object o1, Object o2) { // NOPMD
51-
return o1 == o2 || (o1 != null && o1.equals(o2));
52-
}
53-
54-
/**
55-
* Returns the hashCode of a non-null object or zero for a null object.
56-
* @param o the object to get the hashCode for.
57-
* @return the hashCode
58-
*/
59-
public static int hashCode(Object o) {
60-
return o != null ? o.hashCode() : 0;
61-
}
62-
63-
/**
64-
* Compares two integer values similar to Integer.compare.
65-
* @param v1 the first value
66-
* @param v2 the second value
67-
* @return the comparison result
68-
*/
69-
public static int compare(int v1, int v2) {
70-
return v1 < v2 ? -1 : (v1 > v2 ? 1 : 0);
71-
}
72-
73-
/**
74-
* Compares two long values similar to Long.compare.
75-
* @param v1 the first value
76-
* @param v2 the second value
77-
* @return the comparison result
78-
*/
79-
public static int compare(long v1, long v2) {
80-
return v1 < v2 ? -1 : (v1 > v2 ? 1 : 0);
81-
}
82-
8345
static final BiPredicate<Object, Object> EQUALS = new BiObjectPredicate();
8446

8547
/**
@@ -125,7 +87,7 @@ public static long verifyPositive(long value, String paramName) {
12587
static final class BiObjectPredicate implements BiPredicate<Object, Object> {
12688
@Override
12789
public boolean test(Object o1, Object o2) {
128-
return ObjectHelper.equals(o1, o2);
90+
return Objects.equals(o1, o2);
12991
}
13092
}
13193
}

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeContains.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import io.reactivex.rxjava3.core.*;
1717
import io.reactivex.rxjava3.disposables.Disposable;
1818
import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
19-
import io.reactivex.rxjava3.internal.functions.ObjectHelper;
2019
import io.reactivex.rxjava3.internal.fuseable.HasUpstreamMaybeSource;
20+
import java.util.Objects;
2121

2222
/**
2323
* Signals true if the source signals a value that is object-equals with the provided
@@ -81,7 +81,7 @@ public void onSubscribe(Disposable d) {
8181
@Override
8282
public void onSuccess(Object value) {
8383
upstream = DisposableHelper.DISPOSED;
84-
downstream.onSuccess(ObjectHelper.equals(value, this.value));
84+
downstream.onSuccess(Objects.equals(value, this.value));
8585
}
8686

8787
@Override

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleEquals.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
package io.reactivex.rxjava3.internal.operators.single;
1515

16+
import java.util.Objects;
1617
import java.util.concurrent.atomic.AtomicInteger;
1718

1819
import io.reactivex.rxjava3.core.*;
1920
import io.reactivex.rxjava3.disposables.*;
20-
import io.reactivex.rxjava3.internal.functions.ObjectHelper;
2121
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
2222

2323
public final class SingleEquals<T> extends Single<Boolean> {
@@ -68,7 +68,7 @@ public void onSuccess(T value) {
6868
values[index] = value;
6969

7070
if (count.incrementAndGet() == 2) {
71-
downstream.onSuccess(ObjectHelper.equals(values[0], values[1]));
71+
downstream.onSuccess(Objects.equals(values[0], values[1]));
7272
}
7373
}
7474

src/main/java/io/reactivex/rxjava3/internal/schedulers/TrampolineScheduler.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import io.reactivex.rxjava3.core.Scheduler;
2424
import io.reactivex.rxjava3.disposables.*;
2525
import io.reactivex.rxjava3.internal.disposables.EmptyDisposable;
26-
import io.reactivex.rxjava3.internal.functions.ObjectHelper;
2726
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
2827

2928
/**
@@ -165,9 +164,9 @@ static final class TimedRunnable implements Comparable<TimedRunnable> {
165164

166165
@Override
167166
public int compareTo(TimedRunnable that) {
168-
int result = ObjectHelper.compare(execTime, that.execTime);
167+
int result = Long.compare(execTime, that.execTime);
169168
if (result == 0) {
170-
return ObjectHelper.compare(count, that.count);
169+
return Integer.compare(count, that.count);
171170
}
172171
return result;
173172
}

src/main/java/io/reactivex/rxjava3/internal/util/NotificationLite.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
import java.io.Serializable;
1616

17+
import java.util.Objects;
1718
import org.reactivestreams.*;
1819

1920
import io.reactivex.rxjava3.core.Observer;
2021
import io.reactivex.rxjava3.disposables.Disposable;
21-
import io.reactivex.rxjava3.internal.functions.ObjectHelper;
2222

2323
/**
2424
* Lightweight notification handling utility class.
@@ -52,7 +52,7 @@ public int hashCode() {
5252
public boolean equals(Object obj) {
5353
if (obj instanceof ErrorNotification) {
5454
ErrorNotification n = (ErrorNotification) obj;
55-
return ObjectHelper.equals(e, n.e);
55+
return Objects.equals(e, n.e);
5656
}
5757
return false;
5858
}

src/main/java/io/reactivex/rxjava3/observers/BaseTestConsumer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public final U assertValue(T value) {
284284
throw fail("expected: " + valueAndClass(value) + " but was: " + values);
285285
}
286286
T v = values.get(0);
287-
if (!ObjectHelper.equals(value, v)) {
287+
if (!Objects.equals(value, v)) {
288288
throw fail("expected: " + valueAndClass(value) + " but was: " + valueAndClass(v));
289289
}
290290
return (U)this;
@@ -330,7 +330,7 @@ public final U assertValueAt(int index, T value) {
330330
}
331331

332332
T v = values.get(index);
333-
if (!ObjectHelper.equals(value, v)) {
333+
if (!Objects.equals(value, v)) {
334334
throw fail("expected: " + valueAndClass(value) + " but was: " + valueAndClass(v));
335335
}
336336
return (U)this;
@@ -421,7 +421,7 @@ public final U assertValues(T... values) {
421421
for (int i = 0; i < s; i++) {
422422
T v = this.values.get(i);
423423
T u = values[i];
424-
if (!ObjectHelper.equals(u, v)) {
424+
if (!Objects.equals(u, v)) {
425425
throw fail("Values at position " + i + " differ; expected: " + valueAndClass(u) + " but was: " + valueAndClass(v));
426426
}
427427
}
@@ -466,7 +466,7 @@ public final U assertValueSequence(Iterable<? extends T> sequence) {
466466
T u = expectedIterator.next();
467467
T v = actualIterator.next();
468468

469-
if (!ObjectHelper.equals(u, v)) {
469+
if (!Objects.equals(u, v)) {
470470
throw fail("Values at position " + i + " differ; expected: " + valueAndClass(u) + " but was: " + valueAndClass(v));
471471
}
472472
i++;

src/main/java/io/reactivex/rxjava3/schedulers/TestScheduler.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import io.reactivex.rxjava3.core.Scheduler;
2121
import io.reactivex.rxjava3.disposables.*;
2222
import io.reactivex.rxjava3.internal.disposables.EmptyDisposable;
23-
import io.reactivex.rxjava3.internal.functions.ObjectHelper;
2423

2524
/**
2625
* A special, non thread-safe scheduler for testing operators that require
@@ -76,9 +75,9 @@ public String toString() {
7675
@Override
7776
public int compareTo(TimedRunnable o) {
7877
if (time == o.time) {
79-
return ObjectHelper.compare(count, o.count);
78+
return Long.compare(count, o.count);
8079
}
81-
return ObjectHelper.compare(time, o.time);
80+
return Long.compare(time, o.time);
8281
}
8382
}
8483

src/main/java/io/reactivex/rxjava3/schedulers/Timed.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
package io.reactivex.rxjava3.schedulers;
1515

16+
import java.util.Objects;
1617
import java.util.concurrent.TimeUnit;
1718

1819
import io.reactivex.rxjava3.annotations.NonNull;
@@ -80,9 +81,9 @@ public long time(@NonNull TimeUnit unit) {
8081
public boolean equals(Object other) {
8182
if (other instanceof Timed) {
8283
Timed<?> o = (Timed<?>) other;
83-
return ObjectHelper.equals(value, o.value)
84+
return Objects.equals(value, o.value)
8485
&& time == o.time
85-
&& ObjectHelper.equals(unit, o.unit);
86+
&& Objects.equals(unit, o.unit);
8687
}
8788
return false;
8889
}

src/test/java/io/reactivex/rxjava3/internal/functions/ObjectHelperTest.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ public void utilityClass() {
2727
TestHelper.checkUtilityClass(ObjectHelper.class);
2828
}
2929

30-
@Test
31-
public void hashCodeOf() {
32-
assertEquals(0, ObjectHelper.hashCode(null));
33-
34-
assertEquals(((Integer)1).hashCode(), ObjectHelper.hashCode(1));
35-
}
36-
3730
@Test
3831
public void verifyPositiveInt() throws Exception {
3932
assertEquals(1, ObjectHelper.verifyPositive(1, "param"));
@@ -53,18 +46,4 @@ public void verifyPositiveIntFail() throws Exception {
5346
public void verifyPositiveLongFail() throws Exception {
5447
assertEquals(-1L, ObjectHelper.verifyPositive(-1L, "param"));
5548
}
56-
57-
@Test
58-
public void compare() {
59-
assertEquals(-1, ObjectHelper.compare(0, 2));
60-
assertEquals(0, ObjectHelper.compare(0, 0));
61-
assertEquals(1, ObjectHelper.compare(2, 0));
62-
}
63-
64-
@Test
65-
public void compareLong() {
66-
assertEquals(-1, ObjectHelper.compare(0L, 2L));
67-
assertEquals(0, ObjectHelper.compare(0L, 0L));
68-
assertEquals(1, ObjectHelper.compare(2L, 0L));
69-
}
7049
}

src/test/java/io/reactivex/rxjava3/testsupport/BaseTestConsumerEx.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
import java.util.List;
1717

1818
import io.reactivex.rxjava3.functions.Predicate;
19-
import io.reactivex.rxjava3.internal.functions.ObjectHelper;
2019
import io.reactivex.rxjava3.internal.fuseable.QueueFuseable;
2120
import io.reactivex.rxjava3.internal.util.ExceptionHelper;
2221
import io.reactivex.rxjava3.observers.BaseTestConsumer;
22+
import java.util.Objects;
2323

2424
/**
2525
* Base class with shared infrastructure to support TestSubscriber and TestObserver.
@@ -74,7 +74,7 @@ public final U assertNever(T value) {
7474

7575
for (int i = 0; i < s; i++) {
7676
T v = this.values.get(i);
77-
if (ObjectHelper.equals(v, value)) {
77+
if (Objects.equals(v, value)) {
7878
throw fail("Value at position " + i + " is equal to " + valueAndClass(value) + "; Expected them to be different");
7979
}
8080
}
@@ -158,7 +158,7 @@ public final U assertErrorMessage(String message) {
158158
if (s == 1) {
159159
Throwable e = errors.get(0);
160160
String errorMessage = e.getMessage();
161-
if (!ObjectHelper.equals(message, errorMessage)) {
161+
if (!Objects.equals(message, errorMessage)) {
162162
throw fail("Error message differs; exptected: " + message + " but was: " + errorMessage);
163163
}
164164
} else {

src/test/java/io/reactivex/rxjava3/testsupport/TestHelper.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import io.reactivex.rxjava3.disposables.*;
3737
import io.reactivex.rxjava3.exceptions.*;
3838
import io.reactivex.rxjava3.functions.*;
39-
import io.reactivex.rxjava3.internal.functions.ObjectHelper;
4039
import io.reactivex.rxjava3.internal.fuseable.*;
4140
import io.reactivex.rxjava3.internal.operators.completable.CompletableToFlowable;
4241
import io.reactivex.rxjava3.internal.operators.maybe.MaybeToFlowable;
@@ -196,7 +195,7 @@ public static void assertError(List<Throwable> list, int index, Class<? extends
196195
err.initCause(ex);
197196
throw err;
198197
}
199-
if (!ObjectHelper.equals(message, ex.getMessage())) {
198+
if (!Objects.equals(message, ex.getMessage())) {
200199
AssertionError err = new AssertionError("Message " + message + " expected but got " + ex.getMessage());
201200
err.initCause(ex);
202201
throw err;
@@ -216,7 +215,7 @@ public static void assertUndeliverable(List<Throwable> list, int index, Class<?
216215
err.initCause(list.get(index));
217216
throw err;
218217
}
219-
if (!ObjectHelper.equals(message, ex.getMessage())) {
218+
if (!Objects.equals(message, ex.getMessage())) {
220219
AssertionError err = new AssertionError("Message " + message + " expected but got " + ex.getMessage());
221220
err.initCause(ex);
222221
throw err;

0 commit comments

Comments
 (0)