Skip to content

Commit 6788900

Browse files
committed
Added a klass parameter
1 parent c42be1d commit 6788900

File tree

2 files changed

+76
-8
lines changed

2 files changed

+76
-8
lines changed

rxjava-core/src/main/java/rx/Observable.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import rx.operators.OperationAverage;
3434
import rx.operators.OperationBuffer;
3535
import rx.operators.OperationCache;
36+
import rx.operators.OperationCast;
3637
import rx.operators.OperationCombineLatest;
3738
import rx.operators.OperationConcat;
3839
import rx.operators.OperationDebounce;
@@ -4335,18 +4336,18 @@ public BlockingObservable<T> toBlockingObservable() {
43354336
/**
43364337
* Converts the elements of an observable sequence to the specified type.
43374338
*
4339+
* @param klass
4340+
* The target class type which the elements will be converted to.
4341+
*
43384342
* @return An observable sequence that contains each element of the source
43394343
* sequence converted to the specified type.
43404344
*
4341-
* @see <a href="http://msdn.microsoft.com/en-us/library/hh211842(v=vs.103).aspx">MSDN: Observable.Cast</a>
4345+
* @see <a
4346+
* href="http://msdn.microsoft.com/en-us/library/hh211842(v=vs.103).aspx">MSDN:
4347+
* Observable.Cast</a>
43424348
*/
4343-
public <R> Observable<R> cast() {
4344-
return map(new Func1<T, R>() {
4345-
@SuppressWarnings("unchecked")
4346-
public R call(T t) {
4347-
return (R) t;
4348-
}
4349-
});
4349+
public <R> Observable<R> cast(final Class<R> klass) {
4350+
return create(OperationCast.cast(this, klass));
43504351
}
43514352

43524353
/**
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package rx.operators;
2+
3+
import static org.mockito.Mockito.mock;
4+
import static org.mockito.Mockito.never;
5+
import static org.mockito.Mockito.times;
6+
import static org.mockito.Mockito.verify;
7+
8+
import org.junit.Test;
9+
10+
import rx.Observable;
11+
import rx.Observable.OnSubscribeFunc;
12+
import rx.Observer;
13+
import rx.util.functions.Func1;
14+
15+
/**
16+
* Converts the elements of an observable sequence to the specified type.
17+
*/
18+
public class OperationCast {
19+
20+
public static <T, R> OnSubscribeFunc<R> cast(
21+
Observable<? extends T> source, final Class<R> klass) {
22+
return OperationMap.map(source, new Func1<T, R>() {
23+
@SuppressWarnings("unchecked")
24+
public R call(T t) {
25+
if (klass.isAssignableFrom(t.getClass())) {
26+
return (R) t;
27+
} else {
28+
throw new ClassCastException(t.getClass()
29+
+ " cannot be cast to " + klass);
30+
}
31+
}
32+
});
33+
}
34+
35+
public static class UnitTest {
36+
37+
@Test
38+
public void testCast() {
39+
Observable<?> source = Observable.from(1, 2);
40+
Observable<Integer> observable = Observable.create(cast(source,
41+
Integer.class));
42+
43+
@SuppressWarnings("unchecked")
44+
Observer<Integer> aObserver = mock(Observer.class);
45+
observable.subscribe(aObserver);
46+
verify(aObserver, times(1)).onNext(1);
47+
verify(aObserver, times(1)).onNext(1);
48+
verify(aObserver, never()).onError(
49+
org.mockito.Matchers.any(Throwable.class));
50+
verify(aObserver, times(1)).onCompleted();
51+
}
52+
53+
@Test
54+
public void testCastWithWrongType() {
55+
Observable<?> source = Observable.from(1, 2);
56+
Observable<Boolean> observable = Observable.create(cast(source,
57+
Boolean.class));
58+
59+
@SuppressWarnings("unchecked")
60+
Observer<Boolean> aObserver = mock(Observer.class);
61+
observable.subscribe(aObserver);
62+
verify(aObserver, times(1)).onError(
63+
org.mockito.Matchers.any(ClassCastException.class));
64+
}
65+
}
66+
67+
}

0 commit comments

Comments
 (0)