Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manual Merge of AsObservable #1071

Merged
merged 3 commits into from
Apr 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}