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

Add the cast operator to Single. #5332

Merged
merged 6 commits into from
May 8, 2017
Merged

Conversation

Mikey-Burns
Copy link

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

Signed-off-by: Mike Burns <burnsm523@gmail.com>
@codecov
Copy link

codecov bot commented May 4, 2017

Codecov Report

Merging #5332 into 1.x will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@             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
Impacted Files Coverage Δ Complexity Δ
...java/rx/internal/operators/SingleOperatorCast.java 100% <100%> (ø) 2 <2> (?)
src/main/java/rx/Single.java 74.06% <100%> (+0.09%) 83 <1> (+1) ⬆️
...ternal/operators/OperatorOnBackpressureLatest.java 79.04% <0%> (-3.81%) 3% <0%> (ø)
...n/java/rx/subjects/SubjectSubscriptionManager.java 80.71% <0%> (-1.43%) 15% <0%> (-1%)
...n/java/rx/subscriptions/CompositeSubscription.java 75.32% <0%> (-1.3%) 24% <0%> (ø)
...in/java/rx/internal/operators/OperatorPublish.java 78.9% <0%> (+0.42%) 8% <0%> (ø) ⬇️
...main/java/rx/internal/operators/OperatorMerge.java 86.83% <0%> (+0.46%) 7% <0%> (ø) ⬇️
.../java/rx/internal/operators/BackpressureUtils.java 70.45% <0%> (+2.27%) 28% <0%> (+1%) ⬆️
.../rx/internal/schedulers/CachedThreadScheduler.java 90.29% <0%> (+2.91%) 6% <0%> (ø) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8119e78...995b652. Read the comment docs.

* @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) {
Copy link
Contributor

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

Copy link
Author

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?

Copy link
Member

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

* @return the new Single instance
*/
public final <R> Single<R> cast(final Class<R> klass) {
return lift(new OperatorCast<T, R>(klass));
Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Author

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>
* @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) {
Copy link
Member

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 spaces.

Copy link
Author

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.

@akarnokd akarnokd added this to the 1.4 milestone May 4, 2017
Signed-off-by: Inferno23 <burnsm523@gmail.com>
Signed-off-by: Inferno23 <burnsm523@gmail.com>
@@ -0,0 +1,27 @@
/*
Copy link

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 :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dano Thanks!

Copy link
Author

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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This too.

Copy link
Member

@akarnokd akarnokd left a 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);
Copy link
Contributor

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.

Copy link
Author

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>
Copy link
Contributor

@artem-zinnatullin artem-zinnatullin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@akarnokd akarnokd merged commit a5c0cd0 into ReactiveX:1.x May 8, 2017
@Mikey-Burns Mikey-Burns deleted the singleCast branch May 9, 2017 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants