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

2.x: Add Single.fromObservable(ObservableSource) #4760

Merged
merged 2 commits into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/main/java/io/reactivex/Single.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import io.reactivex.internal.operators.completable.*;
import io.reactivex.internal.operators.flowable.*;
import io.reactivex.internal.operators.maybe.*;
import io.reactivex.internal.operators.observable.ObservableConcatMap;
import io.reactivex.internal.operators.observable.*;
import io.reactivex.internal.operators.single.*;
import io.reactivex.internal.util.*;
import io.reactivex.observers.TestObserver;
Expand Down Expand Up @@ -573,6 +573,27 @@ public static <T> Single<T> fromPublisher(final Publisher<? extends T> publisher
return RxJavaPlugins.onAssembly(new SingleFromPublisher<T>(publisher));
}

/**
* Wraps a specific ObservableSource into a Single and signals its single element or error.
* <p>If the ObservableSource is empty, a NoSuchElementException is signalled.
* If the source has more than one element, an IndexOutOfBoundsException is signalled.
* <p>
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code fromObservable} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param observableSource the source Observable, not null
* @param <T>
* the type of the item emitted by the {@link Single}.
* @return the new Single instance
*/
@SchedulerSupport(SchedulerSupport.NONE)
public static <T> Single<T> fromObservable(ObservableSource<? extends T> observableSource) {
ObjectHelper.requireNonNull(observableSource, "observableSource is null");
return RxJavaPlugins.onAssembly(new ObservableSingleSingle<T>(observableSource, null));
}

/**
* Returns a {@code Single} that emits a specified item.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

public final class ObservableSingleSingle<T> extends Single<T> {

final ObservableSource<T> source;
final ObservableSource<? extends T> source;

final T defaultValue;

public ObservableSingleSingle(ObservableSource<T> source, T defaultValue) {
public ObservableSingleSingle(ObservableSource<? extends T> source, T defaultValue) {
this.source = source;
this.defaultValue = defaultValue;
}
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/io/reactivex/single/SingleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.*;

import io.reactivex.*;
import io.reactivex.Observable;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

needed to add this import otherwise:

SingleTest.java:562: error: reference to Observable is ambiguous Single.fromObservable(Observable.just(1))

import io.reactivex.disposables.*;
import io.reactivex.exceptions.TestException;
import io.reactivex.functions.*;
Expand Down Expand Up @@ -541,5 +542,35 @@ public Integer apply(Single<Integer> v) throws Exception {
}
}).intValue());
}

@Test
public void fromObservableEmpty() {
Single.fromObservable(Observable.empty())
.test()
.assertFailure(NoSuchElementException.class);
}

@Test
public void fromObservableMoreThan1Elements() {
Single.fromObservable(Observable.just(1, 2))
.test()
.assertFailure(IllegalArgumentException.class)
.assertErrorMessage("Sequence contains more than one element!");
}

@Test
public void fromObservableOneElement() {
Single.fromObservable(Observable.just(1))
.test()
.assertResult(1);
}

@Test
public void fromObservableError() {
Single.fromObservable(Observable.error(new RuntimeException("some error")))
.test()
.assertFailure(RuntimeException.class)
.assertErrorMessage("some error");
}
}