-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Add the cast operator to Single. #5332
Conversation
Signed-off-by: Mike Burns <burnsm523@gmail.com>
Codecov Report
@@ Coverage Diff @@
## 1.x #5332 +/- ##
============================================
+ Coverage 84.33% 84.34% +<.01%
- Complexity 2883 2885 +2
============================================
Files 290 291 +1
Lines 18124 18129 +5
Branches 2479 2479
============================================
+ Hits 15285 15291 +6
Misses 1969 1969
+ Partials 870 869 -1
Continue to review full report at Codecov.
|
src/main/java/rx/Single.java
Outdated
* @param klass the type token to use for casting the success result from the current Single | ||
* @return the new Single instance | ||
*/ | ||
public final <R> Single<R> cast(final Class<R> klass) { |
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
src/main/java/rx/Single.java
Outdated
* @return the new Single instance | ||
*/ | ||
public final <R> Single<R> cast(final Class<R> klass) { | ||
return lift(new OperatorCast<T, R>(klass)); |
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.
I didn't realize you could reuse Observable operators on Singles. There's probably a bit of additional overhead, but the reuse is really nice.
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.
Actually you should not reuse them but develop/port them directly. Especially this type of Map derivative that could be just a Func1 wrapping the cast and delegating to map.
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.
@akarnokd I will update the PR with a simplified implementation. Thanks for the suggestion.
Signed-off-by: Inferno23 <burnsm523@gmail.com>
src/main/java/rx/Single.java
Outdated
* @param klass the type token to use for casting the success result from the current Single | ||
* @return the new Single instance | ||
*/ | ||
public final <R> Single<R> cast(final Class<R> klass) { |
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
*/ | ||
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Updated to match the code style.
Signed-off-by: Inferno23 <burnsm523@gmail.com>
Signed-off-by: Inferno23 <burnsm523@gmail.com>
@@ -0,0 +1,27 @@ | |||
/* |
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.
Probably want to fix these copyright notices :)
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.
@dano Thanks!
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.
Thanks @dano. IntelliJ default settings getting the better of me again.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
This too.
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 fix the copyright headers.
Signed-off-by: Inferno23 <burnsm523@gmail.com>
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Should be onNext(2)
, same mistake in OperatorCastTest
as well.
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.
@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.
Signed-off-by: Inferno23 <burnsm523@gmail.com>
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.
👍
Adding the cast operator to Single. This exists in the 2.x branch but not in the 1.x branch, so callers have had to do unnatural operations like
.map(SomeClass.class:cast)
to get around this. Added tests similar to those for Observable.cast.Signed-off-by: Mike Burns burnsm523@gmail.com