Skip to content

Commit

Permalink
Subscribers/Observers.empty()
Browse files Browse the repository at this point in the history
  • Loading branch information
benjchristensen committed Feb 6, 2014
1 parent cb664ff commit fae7a9b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 50 deletions.
37 changes: 19 additions & 18 deletions rxjava-core/src/main/java/rx/observers/Observers.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
package rx.observers;

import rx.Observer;
import rx.Subscriber;
import rx.util.OnErrorNotImplementedException;
import rx.util.functions.Action0;
import rx.util.functions.Action1;

public class Observers {

/**
* Create an empty Observer that ignores all events.
*/
public static final <T> Observer<T> create() {
return new Observer<T>() {
private static final Observer<Object> EMPTY = new Observer<Object>() {

@Override
public final void onCompleted() {
// do nothing
}
@Override
public final void onCompleted() {
// do nothing
}

@Override
public final void onError(Throwable e) {
throw new OnErrorNotImplementedException(e);
}
@Override
public final void onError(Throwable e) {
throw new OnErrorNotImplementedException(e);
}

@Override
public final void onNext(T args) {
// do nothing
}
@Override
public final void onNext(Object args) {
// do nothing
}

};
};

@SuppressWarnings("unchecked")
public static <T> Observer<T> empty() {
return (Observer<T>) EMPTY;
}

/**
Expand Down
48 changes: 24 additions & 24 deletions rxjava-core/src/main/java/rx/observers/Subscribers.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@

public class Subscribers {

private static final Subscriber<Object> EMPTY = new Subscriber<Object>() {

@Override
public final void onCompleted() {
// do nothing
}

@Override
public final void onError(Throwable e) {
throw new OnErrorNotImplementedException(e);
}

@Override
public final void onNext(Object args) {
// do nothing
}

};

@SuppressWarnings("unchecked")
public static <T> Subscriber<T> empty() {
return (Subscriber<T>) EMPTY;
}

public static <T> Subscriber<T> from(final Observer<? super T> o) {
return new Subscriber<T>() {

Expand All @@ -29,30 +53,6 @@ public void onNext(T t) {
};
}

/**
* Create an empty Subscriber that ignores all events.
*/
public static final <T> Subscriber<T> create() {
return new Subscriber<T>() {

@Override
public final void onCompleted() {
// do nothing
}

@Override
public final void onError(Throwable e) {
throw new OnErrorNotImplementedException(e);
}

@Override
public final void onNext(T args) {
// do nothing
}

};
}

/**
* Create an Subscriber that receives `onNext` and ignores `onError` and `onCompleted`.
*/
Expand Down
4 changes: 1 addition & 3 deletions rxjava-core/src/main/java/rx/observers/TestObserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
*/
public class TestObserver<T> implements Observer<T> {

private final Observer<Object> EMPTY = new EmptyObserver<Object>();

private final Observer<T> delegate;
private final ArrayList<T> onNextEvents = new ArrayList<T>();
Expand All @@ -39,9 +38,8 @@ public TestObserver(Observer<T> delegate) {
this.delegate = delegate;
}

@SuppressWarnings("unchecked")
public TestObserver() {
this.delegate = (Observer<T>) EMPTY;
this.delegate = Observers.empty();
}

@Override
Expand Down
5 changes: 1 addition & 4 deletions rxjava-core/src/main/java/rx/observers/TestSubscriber.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
*/
public class TestSubscriber<T> extends Subscriber<T> {

private final Subscriber<Object> EMPTY = Subscribers.create();

private final TestObserver<T> testObserver;

public TestSubscriber(Subscriber<T> delegate) {
Expand All @@ -38,9 +36,8 @@ public TestSubscriber(Observer<T> delegate) {
this.testObserver = new TestObserver<T>(delegate);
}

@SuppressWarnings("unchecked")
public TestSubscriber() {
this.testObserver = new TestObserver<T>((Subscriber<T>) EMPTY);
this.testObserver = new TestObserver<T>(Subscribers.<T>empty());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Subscriber<? super T1> call(final Subscriber<? super R> subscriber) {
try {
if (!iterator.hasNext()) {
subscriber.onCompleted();
return Subscribers.create();
return Subscribers.empty();
}
} catch (Throwable e) {
subscriber.onError(e);
Expand Down

0 comments on commit fae7a9b

Please sign in to comment.