Skip to content

Commit

Permalink
Merge pull request #1071 from benjchristensen/merge-asObservable
Browse files Browse the repository at this point in the history
Manual Merge of AsObservable
  • Loading branch information
benjchristensen committed Apr 23, 2014
2 parents 21b6747 + 45c9dc8 commit f04cd5f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 15 deletions.
4 changes: 2 additions & 2 deletions rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import rx.observers.SafeSubscriber;
import rx.operators.OnSubscribeFromIterable;
import rx.operators.OnSubscribeRange;
import rx.operators.OperationAsObservable;
import rx.operators.OperationBuffer;
import rx.operators.OperationCombineLatest;
import rx.operators.OperationConcat;
Expand Down Expand Up @@ -93,6 +92,7 @@
import rx.operators.OperatorAll;
import rx.operators.OperatorAmb;
import rx.operators.OperatorAny;
import rx.operators.OperatorAsObservable;
import rx.operators.OperatorCache;
import rx.operators.OperatorCast;
import rx.operators.OperatorDoOnEach;
Expand Down Expand Up @@ -2952,7 +2952,7 @@ public final Observable<Boolean> all(Func1<? super T, Boolean> predicate) {
* @return an Observable that hides the identity of this Observable
*/
public final Observable<T> asObservable() {
return create(new OperationAsObservable<T>(this));
return lift(new OperatorAsObservable<T>());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,20 @@
*/
package rx.operators;

import rx.Observable;
import rx.Observable.OnSubscribeFunc;
import rx.Observer;
import rx.Subscription;
import rx.observers.Subscribers;
import rx.Observable.Operator;
import rx.Subscriber;

/**
* Hides the identity of another observable.
*
* @param <T>
* the return value type of the wrapped observable.
*/
public final class OperationAsObservable<T> implements OnSubscribeFunc<T> {
private final Observable<? extends T> source;

public OperationAsObservable(Observable<? extends T> source) {
this.source = source;
}
public final class OperatorAsObservable<T> implements Operator<T, T> {

@Override
public Subscription onSubscribe(final Observer<? super T> t1) {
return source.unsafeSubscribe(Subscribers.from(t1));
public Subscriber<? super T> call(Subscriber<? super T> s) {
return s;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package rx.operators;

import static org.junit.Assert.assertFalse;
import org.junit.Test;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
import rx.Observable;
import rx.Observer;
import rx.subjects.PublishSubject;

public class OperatorAsObservableTest {
@Test
public void testHiding() {
PublishSubject<Integer> src = PublishSubject.create();

Observable<Integer> dst = src.asObservable();

assertFalse(dst instanceof PublishSubject);

Observer<Object> o = mock(Observer.class);

dst.subscribe(o);

src.onNext(1);
src.onCompleted();

verify(o).onNext(1);
verify(o).onCompleted();
verify(o, never()).onError(any(Throwable.class));
}
@Test
public void testHidingError() {
PublishSubject<Integer> src = PublishSubject.create();

Observable<Integer> dst = src.asObservable();

assertFalse(dst instanceof PublishSubject);

Observer<Object> o = mock(Observer.class);

dst.subscribe(o);

src.onError(new OperationReduceTest.CustomException());

verify(o, never()).onNext(any());
verify(o, never()).onCompleted();
verify(o).onError(any(OperationReduceTest.CustomException.class));
}
}

0 comments on commit f04cd5f

Please sign in to comment.