Skip to content

Commit

Permalink
Observable.from: refactor varargs to overloads
Browse files Browse the repository at this point in the history
#359 Varargs cause compiler warnings
  • Loading branch information
benjchristensen committed Sep 9, 2013
1 parent ccc10cf commit 2aabbd3
Show file tree
Hide file tree
Showing 8 changed files with 361 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class UnitTestSuite extends JUnitSuite {
}

@Test def testSingleOrDefault {
assertEquals(0, Observable.from[Int]().toBlockingObservable.singleOrDefault(0))
assertEquals(0, Observable.empty[Int]().toBlockingObservable.singleOrDefault(0))
assertEquals(1, Observable.from(1).toBlockingObservable.singleOrDefault(0))
try {
Observable.from(1, 2, 3).toBlockingObservable.singleOrDefault(0)
Expand Down Expand Up @@ -232,13 +232,13 @@ class UnitTestSuite extends JUnitSuite {
@Test def testLastOrDefault {
val observable = Observable.from(1, 2, 3, 4)
assertEquals(4, observable.toBlockingObservable.lastOrDefault(5))
assertEquals(5, Observable.from[Int]().toBlockingObservable.lastOrDefault(5))
assertEquals(5, Observable.empty[Int]().toBlockingObservable.lastOrDefault(5))
}

@Test def testLastOrDefaultPredicate {
val observable = Observable.from(1, 2, 3, 4)
assertEquals(3, observable.toBlockingObservable.lastOrDefault(5, isOdd))
assertEquals(5, Observable.from[Int]().toBlockingObservable.lastOrDefault(5, isOdd))
assertEquals(5, Observable.empty[Int]().toBlockingObservable.lastOrDefault(5, isOdd))
}

@Test def testMap {
Expand Down
309 changes: 304 additions & 5 deletions rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -529,24 +530,322 @@ public static <T> Observable<T> error(Throwable exception) {
public static <T> Observable<T> from(Iterable<? extends T> iterable) {
return create(OperationToObservableIterable.toObservableIterable(iterable));
}

/**
* Converts an Array into an Observable.
* <p>
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
*
* <p>Implementation note: the entire iterable sequence will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned,
* it in not possible to unsubscribe from the sequence before it completes.
*
* @param array
* the source sequence
* @param <T>
* the type of items in the {@link Iterable} sequence and the type of items to be
* emitted by the resulting Observable
* @return an Observable that emits each item in the source {@link Iterable} sequence
*/
public static <T> Observable<T> from(T[] items) {
return create(OperationToObservableIterable.toObservableIterable(Arrays.asList(items)));
}

/**
* Converts a series of items into an Observable.
* <p>
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
*
* <p>Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned,
* it in not possible to unsubscribe from the sequence before it completes.
*
* @param items
* the source Array
* @param t1
* item
* @param <T>
* the type of items in the Array, and the type of items to be emitted by the
* resulting Observable
* @return an Observable that emits each item in the source Array
*/
public static <T> Observable<T> from(T... items) {
return create(OperationToObservableIterable.toObservableIterable(Arrays.asList(items)));
@SuppressWarnings("unchecked")
// suppress unchecked because we are using varargs inside the method
public static <T> Observable<T> from(T t1) {
return from(Arrays.asList(t1));
}

/**
* Converts a series of items into an Observable.
* <p>
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
*
* <p>Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned,
* it in not possible to unsubscribe from the sequence before it completes.
*
* @param t1
* item
* @param t2
* item
* @param <T>
* the type of items in the Array, and the type of items to be emitted by the
* resulting Observable
* @return an Observable that emits each item in the source Array
*/
@SuppressWarnings("unchecked")
// suppress unchecked because we are using varargs inside the method
public static <T> Observable<T> from(T t1, T t2) {
return from(Arrays.asList(t1, t2));
}

/**
* Converts a series of items into an Observable.
* <p>
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
*
* <p>Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned,
* it in not possible to unsubscribe from the sequence before it completes.
*
* @param t1
* item
* @param t2
* item
* @param t3
* item
* @param <T>
* the type of items in the Array, and the type of items to be emitted by the
* resulting Observable
* @return an Observable that emits each item in the source Array
*/
@SuppressWarnings("unchecked")
// suppress unchecked because we are using varargs inside the method
public static <T> Observable<T> from(T t1, T t2, T t3) {
return from(Arrays.asList(t1, t2, t3));
}

/**
* Converts a series of items into an Observable.
* <p>
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
*
* <p>Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned,
* it in not possible to unsubscribe from the sequence before it completes.
*
* @param t1
* item
* @param t2
* item
* @param t3
* item
* @param t4
* item
* @param <T>
* the type of items in the Array, and the type of items to be emitted by the
* resulting Observable
* @return an Observable that emits each item in the source Array
*/
@SuppressWarnings("unchecked")
// suppress unchecked because we are using varargs inside the method
public static <T> Observable<T> from(T t1, T t2, T t3, T t4) {
return from(Arrays.asList(t1, t2, t3, t4));
}

/**
* Converts a series of items into an Observable.
* <p>
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
*
* <p>Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned,
* it in not possible to unsubscribe from the sequence before it completes.
*
* @param t1
* item
* @param t2
* item
* @param t3
* item
* @param t4
* item
* @param t5
* item
* @param <T>
* the type of items in the Array, and the type of items to be emitted by the
* resulting Observable
* @return an Observable that emits each item in the source Array
*/
@SuppressWarnings("unchecked")
// suppress unchecked because we are using varargs inside the method
public static <T> Observable<T> from(T t1, T t2, T t3, T t4, T t5) {
return from(Arrays.asList(t1, t2, t3, t4, t5));
}

/**
* Converts a series of items into an Observable.
* <p>
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
*
* <p>Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned,
* it in not possible to unsubscribe from the sequence before it completes.
*
* @param t1
* item
* @param t2
* item
* @param t3
* item
* @param t4
* item
* @param t5
* item
* @param t6
* item
* @param <T>
* the type of items in the Array, and the type of items to be emitted by the
* resulting Observable
* @return an Observable that emits each item in the source Array
*/
@SuppressWarnings("unchecked")
// suppress unchecked because we are using varargs inside the method
public static <T> Observable<T> from(T t1, T t2, T t3, T t4, T t5, T t6) {
return from(Arrays.asList(t1, t2, t3, t4, t5, t6));
}

/**
* Converts a series of items into an Observable.
* <p>
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
*
* <p>Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned,
* it in not possible to unsubscribe from the sequence before it completes.
*
* @param t1
* item
* @param t2
* item
* @param t3
* item
* @param t4
* item
* @param t5
* item
* @param t6
* item
* @param t7
* item
* @param <T>
* the type of items in the Array, and the type of items to be emitted by the
* resulting Observable
* @return an Observable that emits each item in the source Array
*/
@SuppressWarnings("unchecked")
// suppress unchecked because we are using varargs inside the method
public static <T> Observable<T> from(T t1, T t2, T t3, T t4, T t5, T t6, T t7) {
return from(Arrays.asList(t1, t2, t3, t4, t5, t6, t7));
}

/**
* Converts a series of items into an Observable.
* <p>
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
*
* <p>Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned,
* it in not possible to unsubscribe from the sequence before it completes.
*
* @param t1
* item
* @param t2
* item
* @param t3
* item
* @param t4
* item
* @param t5
* item
* @param t6
* item
* @param t7
* item
* @param t8
* item
* @param <T>
* the type of items in the Array, and the type of items to be emitted by the
* resulting Observable
* @return an Observable that emits each item in the source Array
*/
@SuppressWarnings("unchecked")
// suppress unchecked because we are using varargs inside the method
public static <T> Observable<T> from(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8) {
return from(Arrays.asList(t1, t2, t3, t4, t5, t6, t7, t8));
}

/**
* Converts a series of items into an Observable.
* <p>
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
*
* <p>Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned,
* it in not possible to unsubscribe from the sequence before it completes.
*
* @param t1
* item
* @param t2
* item
* @param t3
* item
* @param t4
* item
* @param t5
* item
* @param t6
* item
* @param t7
* item
* @param t8
* item
* @param t9
* item
* @param <T>
* the type of items in the Array, and the type of items to be emitted by the
* resulting Observable
* @return an Observable that emits each item in the source Array
*/
@SuppressWarnings("unchecked")
// suppress unchecked because we are using varargs inside the method
public static <T> Observable<T> from(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8, T t9) {
return from(Arrays.asList(t1, t2, t3, t4, t5, t6, t7, t8, t9));
}

/**
* Converts a series of items into an Observable.
* <p>
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
*
* <p>Implementation note: the entire array will be immediately emitted each time an {@link Observer} subscribes. Since this occurs before the {@link Subscription} is returned,
* it in not possible to unsubscribe from the sequence before it completes.
*
* @param t1
* item
* @param t2
* item
* @param t3
* item
* @param t4
* item
* @param t5
* item
* @param t6
* item
* @param t7
* item
* @param t8
* item
* @param t10
* item
* @param <T>
* the type of items in the Array, and the type of items to be emitted by the
* resulting Observable
* @return an Observable that emits each item in the source Array
*/
@SuppressWarnings("unchecked")
// suppress unchecked because we are using varargs inside the method
public static <T> Observable<T> from(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8, T t9, T t10) {
return from(Arrays.asList(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public void testLast() {

@Test
public void testLastEmptyObservable() {
BlockingObservable<Object> obs = BlockingObservable.from(Observable.from());
BlockingObservable<Object> obs = BlockingObservable.from(Observable.empty());

assertNull(obs.last());
}
Expand All @@ -412,7 +412,7 @@ public void testLastOrDefault1() {

@Test
public void testLastOrDefault2() {
BlockingObservable<Object> observable = BlockingObservable.from(Observable.from());
BlockingObservable<Object> observable = BlockingObservable.from(Observable.empty());
assertEquals("default", observable.lastOrDefault("default"));
}

Expand Down Expand Up @@ -460,7 +460,7 @@ public void testSingle() {

@Test
public void testSingleDefault() {
BlockingObservable<Object> observable = BlockingObservable.from(Observable.from());
BlockingObservable<Object> observable = BlockingObservable.from(Observable.empty());
assertEquals("default", observable.singleOrDefault("default"));
}

Expand Down
Loading

4 comments on commit 2aabbd3

@gbenson-nflx
Copy link

Choose a reason for hiding this comment

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

Am wondering what the reason was for changing Observable from(T... items) into a set of methods with individual signatures such as Observable from(T t1, T t2), etc.? Seems to create a more confusing API as well as really mucking things up for auto-completers...

@samuelgruetter
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree. The reason is #359 . Not respecting that would be even worse.

And if you want a nicer API, use the Scala adaptor (disclaimer: I wrote it ;-) )

@gbenson-nflx
Copy link

Choose a reason for hiding this comment

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

Ugly and unfortunate. Thanks for the clarification.

@benjchristensen
Copy link
Member Author

Choose a reason for hiding this comment

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

Yes it is ugly and unfortunate. The vararg implementation in Java is very annoying how it's not type-safe.

Please sign in to comment.