-
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
implement Maybe.switchIfEmpty(Single) #5582
Conversation
Codecov Report
@@ Coverage Diff @@
## 2.x #5582 +/- ##
============================================
- Coverage 96.15% 96.13% -0.03%
- Complexity 5821 5825 +4
============================================
Files 631 632 +1
Lines 41421 41459 +38
Branches 5739 5742 +3
============================================
+ Hits 39830 39858 +28
- Misses 630 645 +15
+ Partials 961 956 -5
Continue to review full report at Codecov.
|
@@ -37,16 +37,6 @@ public void empty() { | |||
} | |||
|
|||
@Test | |||
public void defaultIfEmptyNonEmpty() { |
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.
Why were these removed?
*/ | ||
@CheckReturnValue | ||
@SchedulerSupport(SchedulerSupport.NONE) | ||
public final Maybe<T> switchIfEmpty(SingleSource<? extends T> other) { |
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.
The issue mentioned Single
as return type.
* the alternate SingleSource to subscribe to if the main does not emit any items | ||
* @return a Maybe that emits the items emitted by the source Maybe or the item of an | ||
* alternate SingleSource if the source Maybe is empty. | ||
*/ |
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 the appropriate experimental tags and annotations:
@since 2.1.4 - experimental
* alternate SingleSource if the source Maybe is empty. | ||
*/ | ||
@CheckReturnValue | ||
@SchedulerSupport(SchedulerSupport.NONE) |
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
@@ -24,11 +24,11 @@ | |||
* | |||
* @param <T> the value type | |||
*/ | |||
public final class MaybeSwitchIfEmpty<T> extends AbstractMaybeWithUpstream<T, T> { | |||
public final class MaybeSwitchIfEmptyToMaybe<T> extends AbstractMaybeWithUpstream<T, T> { |
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'd keep the original name, less change per PR, and name the new version MaybeSwitchIfEmptySingle
.
@@ -24,7 +24,7 @@ | |||
import io.reactivex.processors.PublishProcessor; | |||
import io.reactivex.schedulers.Schedulers; | |||
|
|||
public class MaybeSwitchIfEmptyTest { | |||
public class MaybeSwitchIfEmptyToMaybeTest { |
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.
Same as before, please keep the original name to this test.
@@ -2213,7 +2213,7 @@ public final T blockingGet(T defaultValue) { | |||
@SchedulerSupport(SchedulerSupport.NONE) | |||
public final Maybe<T> defaultIfEmpty(T defaultItem) { | |||
ObjectHelper.requireNonNull(defaultItem, "item is null"); | |||
return switchIfEmpty(just(defaultItem)); | |||
return switchIfEmpty(Single.just(defaultItem)); |
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.
Why was this changed, the original just
should still work?!
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.
Of course the original just
still works. Just thought that it would be better to wrap the T defaultItem
into Single
instead of Maybe
because the defaultItem
cannot be empty.
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 restore the original state. The less change to existing and practically unrelated code regarding the PR the better.
I'm only 50% convinced for the need of this overload. |
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.
LGTM, few comments
|
||
final SingleObserver<? super T> actual; | ||
|
||
final AtomicReference<Disposable> parent; |
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.
OtherSingleObserver
can extend AtomicReference
to save field, should we do that @akarnokd?
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.
If you look at L95, you'll see that parent
is SwitchIfEmptyMaybeObserver which is declared as AtomicReference<Disposable>
thus this is not a new instace of an AtomicReference
here.
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.
Yes, I saw that, but wouldn't it save field anyway? Though it's super nit
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.
The downstream can interact with only one replaceable reference to the original or the alternative disposable, so you'll need an extra field either way.
|
||
@Test | ||
public void nonEmpty() { | ||
Maybe.just(1).switchIfEmpty(Single.just(2)).test().assertResult(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.
assertValuesOnly()
@vanniktech?
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.
onSuccess
translates to onNext
+ onComplete
in TestObserver
so this requires assertResult
.
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.
AssertValuesOnly is mostly for Observable & Flowable
@Test | ||
public void error() { | ||
Maybe.<Integer>error(new TestException()).switchIfEmpty(Single.just(2)) | ||
.test().assertFailure(TestException.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.
Would be great to also check that no values were emitted to check that it didn't switch to fallback Single
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.
assertFailure
does check values:
public final U assertFailure(Class<? extends Throwable> error, T... values) {
return assertSubscribed()
.assertValues(values)
.assertError(error)
.assertNotComplete();
}
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.
Yes assertFailure does the job under the hood
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 like it
Adds
Maybe.switchIfEmpty(Single)
, fixes #4544.