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.
|
| Maybe.<Integer>empty().switchIfEmpty(Maybe.just(2)).test().assertResult(2); | ||
| } | ||
|
|
||
| @Test |
| */ | ||
| @CheckReturnValue | ||
| @SchedulerSupport(SchedulerSupport.NONE) | ||
| public final Maybe<T> switchIfEmpty(SingleSource<? extends T> other) { |
There was a problem hiding this comment.
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.
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) |
| * @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.
I'd keep the original name, less change per PR, and name the new version MaybeSwitchIfEmptySingle.
| import io.reactivex.schedulers.Schedulers; | ||
|
|
||
| public class MaybeSwitchIfEmptyTest { | ||
| public class MaybeSwitchIfEmptyToMaybeTest { |
There was a problem hiding this comment.
Same as before, please keep the original name to this test.
| 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.
Why was this changed, the original just should still work?!
There was a problem hiding this comment.
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.
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. |
artem-zinnatullin
left a comment
There was a problem hiding this comment.
LGTM, few comments
|
|
||
| final SingleObserver<? super T> actual; | ||
|
|
||
| final AtomicReference<Disposable> parent; |
There was a problem hiding this comment.
OtherSingleObserver can extend AtomicReference to save field, should we do that @akarnokd?
There was a problem hiding this comment.
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.
Yes, I saw that, but wouldn't it save field anyway? Though it's super nit
There was a problem hiding this comment.
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.
onSuccess translates to onNext + onComplete in TestObserver so this requires assertResult.
There was a problem hiding this comment.
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.
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.
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.
Yes assertFailure does the job under the hood
Adds
Maybe.switchIfEmpty(Single), fixes #4544.