-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Add the cast operator to Single. #5332
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
Changes from 2 commits
88965ce
6b55cd6
7f82f5e
4e3ab81
7f85598
995b652
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably want to fix these copyright notices :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dano Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @dano. IntelliJ default settings getting the better of me again. |
||
* This is the confidential unpublished intellectual property of EMC Corporation, | ||
* and includes without limitation exclusive copyright and trade secret rights | ||
* of EMC throughout the world. | ||
*/ | ||
package rx.internal.operators; | ||
|
||
import rx.functions.Func1; | ||
|
||
/** | ||
* Converts the element of a Single to the specified type. | ||
* @param <T> the input value type | ||
* @param <R> the output value type | ||
*/ | ||
public class SingleOperatorCast<T, R> implements Func1<T, R> { | ||
|
||
final Class<R> castClass; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 4 spaces. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. Updated to match the code style. |
||
|
||
public SingleOperatorCast(Class<R> castClass) { | ||
this.castClass = castClass; | ||
} | ||
|
||
@Override | ||
public R call(T t) { | ||
return castClass.cast(t); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* This is the confidential unpublished intellectual property of EMC Corporation, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This too. |
||
* and includes without limitation exclusive copyright and trade secret rights | ||
* of EMC throughout the world. | ||
*/ | ||
package rx.internal.operators; | ||
; | ||
import org.junit.Test; | ||
import rx.Observer; | ||
import rx.Single; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
public class SingleOperatorCastTest { | ||
|
||
@Test | ||
public void testSingleCast() { | ||
Single<?> source = Single.just(1); | ||
Single<Integer> single = source.cast(Integer.class); | ||
|
||
@SuppressWarnings("unchecked") | ||
Observer<Integer> observer = mock(Observer.class); | ||
single.subscribe(observer); | ||
verify(observer, times(1)).onNext(1); | ||
verify(observer, times(1)).onNext(1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @artem-zinnatullin I agree that in OperatorCastTest the second invocation should use 2. However, here I think the second invocation is actually unnecessary because only one item gets emitted from the Single. I can remove this line since it is not needed. |
||
verify(observer, never()).onError( | ||
org.mockito.Matchers.any(Throwable.class)); | ||
verify(observer, times(1)).onCompleted(); | ||
} | ||
|
||
@Test | ||
public void testSingleCastWithWrongType() { | ||
Single<?> source = Single.just(1); | ||
Single<Boolean> single = source.cast(Boolean.class); | ||
|
||
@SuppressWarnings("unchecked") | ||
Observer<Boolean> observer = mock(Observer.class); | ||
single.subscribe(observer); | ||
verify(observer, times(1)).onError( | ||
org.mockito.Matchers.any(ClassCastException.class)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: param doesn't need
final
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JakeWharton I was following the example of Observable.cast(). Would you like me to remove
final
from here or shall I leave it to remain consistent?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add
@Experimental
and@since 1.3.1 - experimental
to the javadoc