Skip to content

Commit

Permalink
Replace some ObjecsHelpers methods with Java 8 APIs (#6770)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton authored and akarnokd committed Dec 17, 2019
1 parent 2392349 commit 652f2a1
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 87 deletions.
3 changes: 2 additions & 1 deletion src/main/java/io/reactivex/rxjava3/core/Notification.java
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
Expand Up @@ -275,7 +275,7 @@ static final class EqualsPredicate<T> implements Predicate<T> {

@Override
public boolean test(T t) throws Exception {
return ObjectHelper.equals(t, value);
return Objects.equals(t, value);
}
}

Expand Down
Expand Up @@ -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.
Expand Down Expand Up @@ -41,45 +42,6 @@ public static <T> 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<Object, Object> EQUALS = new BiObjectPredicate();

/**
Expand Down Expand Up @@ -125,7 +87,7 @@ public static long verifyPositive(long value, String paramName) {
static final class BiObjectPredicate implements BiPredicate<Object, Object> {
@Override
public boolean test(Object o1, Object o2) {
return ObjectHelper.equals(o1, o2);
return Objects.equals(o1, o2);
}
}
}
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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<T> extends Single<Boolean> {
Expand Down Expand Up @@ -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]));
}
}

Expand Down
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -165,9 +164,9 @@ static final class TimedRunnable implements Comparable<TimedRunnable> {

@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;
}
Expand Down
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
Expand Down Expand Up @@ -466,7 +466,7 @@ public final U assertValueSequence(Iterable<? extends T> 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++;
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/io/reactivex/rxjava3/schedulers/Timed.java
Expand Up @@ -13,6 +13,7 @@

package io.reactivex.rxjava3.schedulers;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.annotations.NonNull;
Expand Down Expand Up @@ -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;
}
Expand Down
Expand Up @@ -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"));
Expand All @@ -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));
}
}
Expand Up @@ -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.
Expand Down Expand Up @@ -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");
}
}
Expand Down Expand Up @@ -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 {
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -196,7 +195,7 @@ public static void assertError(List<Throwable> list, int index, Class<? extends
err.initCause(ex);
throw err;
}
if (!ObjectHelper.equals(message, ex.getMessage())) {
if (!Objects.equals(message, ex.getMessage())) {
AssertionError err = new AssertionError("Message " + message + " expected but got " + ex.getMessage());
err.initCause(ex);
throw err;
Expand All @@ -216,7 +215,7 @@ public static void assertUndeliverable(List<Throwable> list, int index, Class<?
err.initCause(list.get(index));
throw err;
}
if (!ObjectHelper.equals(message, ex.getMessage())) {
if (!Objects.equals(message, ex.getMessage())) {
AssertionError err = new AssertionError("Message " + message + " expected but got " + ex.getMessage());
err.initCause(ex);
throw err;
Expand Down

0 comments on commit 652f2a1

Please sign in to comment.