Skip to content

Commit 2aabbd3

Browse files
Observable.from: refactor varargs to overloads
#359 Varargs cause compiler warnings
1 parent ccc10cf commit 2aabbd3

File tree

8 files changed

+361
-24
lines changed

8 files changed

+361
-24
lines changed

language-adaptors/rxjava-scala/src/test/scala/rx/lang/scala/RxImplicitsTests.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class UnitTestSuite extends JUnitSuite {
7575
}
7676

7777
@Test def testSingleOrDefault {
78-
assertEquals(0, Observable.from[Int]().toBlockingObservable.singleOrDefault(0))
78+
assertEquals(0, Observable.empty[Int]().toBlockingObservable.singleOrDefault(0))
7979
assertEquals(1, Observable.from(1).toBlockingObservable.singleOrDefault(0))
8080
try {
8181
Observable.from(1, 2, 3).toBlockingObservable.singleOrDefault(0)
@@ -232,13 +232,13 @@ class UnitTestSuite extends JUnitSuite {
232232
@Test def testLastOrDefault {
233233
val observable = Observable.from(1, 2, 3, 4)
234234
assertEquals(4, observable.toBlockingObservable.lastOrDefault(5))
235-
assertEquals(5, Observable.from[Int]().toBlockingObservable.lastOrDefault(5))
235+
assertEquals(5, Observable.empty[Int]().toBlockingObservable.lastOrDefault(5))
236236
}
237237

238238
@Test def testLastOrDefaultPredicate {
239239
val observable = Observable.from(1, 2, 3, 4)
240240
assertEquals(3, observable.toBlockingObservable.lastOrDefault(5, isOdd))
241-
assertEquals(5, Observable.from[Int]().toBlockingObservable.lastOrDefault(5, isOdd))
241+
assertEquals(5, Observable.empty[Int]().toBlockingObservable.lastOrDefault(5, isOdd))
242242
}
243243

244244
@Test def testMap {

rxjava-core/src/main/java/rx/Observable.java

Lines changed: 304 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.ArrayList;
1919
import java.util.Arrays;
2020
import java.util.Collection;
21+
import java.util.Collections;
2122
import java.util.List;
2223
import java.util.concurrent.Future;
2324
import java.util.concurrent.TimeUnit;
@@ -529,24 +530,322 @@ public static <T> Observable<T> error(Throwable exception) {
529530
public static <T> Observable<T> from(Iterable<? extends T> iterable) {
530531
return create(OperationToObservableIterable.toObservableIterable(iterable));
531532
}
532-
533+
533534
/**
534535
* Converts an Array into an Observable.
535536
* <p>
536537
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
537538
*
539+
* <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,
540+
* it in not possible to unsubscribe from the sequence before it completes.
541+
*
542+
* @param array
543+
* the source sequence
544+
* @param <T>
545+
* the type of items in the {@link Iterable} sequence and the type of items to be
546+
* emitted by the resulting Observable
547+
* @return an Observable that emits each item in the source {@link Iterable} sequence
548+
*/
549+
public static <T> Observable<T> from(T[] items) {
550+
return create(OperationToObservableIterable.toObservableIterable(Arrays.asList(items)));
551+
}
552+
553+
/**
554+
* Converts a series of items into an Observable.
555+
* <p>
556+
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
557+
*
538558
* <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,
539559
* it in not possible to unsubscribe from the sequence before it completes.
540560
*
541-
* @param items
542-
* the source Array
561+
* @param t1
562+
* item
543563
* @param <T>
544564
* the type of items in the Array, and the type of items to be emitted by the
545565
* resulting Observable
546566
* @return an Observable that emits each item in the source Array
547567
*/
548-
public static <T> Observable<T> from(T... items) {
549-
return create(OperationToObservableIterable.toObservableIterable(Arrays.asList(items)));
568+
@SuppressWarnings("unchecked")
569+
// suppress unchecked because we are using varargs inside the method
570+
public static <T> Observable<T> from(T t1) {
571+
return from(Arrays.asList(t1));
572+
}
573+
574+
/**
575+
* Converts a series of items into an Observable.
576+
* <p>
577+
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
578+
*
579+
* <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,
580+
* it in not possible to unsubscribe from the sequence before it completes.
581+
*
582+
* @param t1
583+
* item
584+
* @param t2
585+
* item
586+
* @param <T>
587+
* the type of items in the Array, and the type of items to be emitted by the
588+
* resulting Observable
589+
* @return an Observable that emits each item in the source Array
590+
*/
591+
@SuppressWarnings("unchecked")
592+
// suppress unchecked because we are using varargs inside the method
593+
public static <T> Observable<T> from(T t1, T t2) {
594+
return from(Arrays.asList(t1, t2));
595+
}
596+
597+
/**
598+
* Converts a series of items into an Observable.
599+
* <p>
600+
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
601+
*
602+
* <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,
603+
* it in not possible to unsubscribe from the sequence before it completes.
604+
*
605+
* @param t1
606+
* item
607+
* @param t2
608+
* item
609+
* @param t3
610+
* item
611+
* @param <T>
612+
* the type of items in the Array, and the type of items to be emitted by the
613+
* resulting Observable
614+
* @return an Observable that emits each item in the source Array
615+
*/
616+
@SuppressWarnings("unchecked")
617+
// suppress unchecked because we are using varargs inside the method
618+
public static <T> Observable<T> from(T t1, T t2, T t3) {
619+
return from(Arrays.asList(t1, t2, t3));
620+
}
621+
622+
/**
623+
* Converts a series of items into an Observable.
624+
* <p>
625+
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
626+
*
627+
* <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,
628+
* it in not possible to unsubscribe from the sequence before it completes.
629+
*
630+
* @param t1
631+
* item
632+
* @param t2
633+
* item
634+
* @param t3
635+
* item
636+
* @param t4
637+
* item
638+
* @param <T>
639+
* the type of items in the Array, and the type of items to be emitted by the
640+
* resulting Observable
641+
* @return an Observable that emits each item in the source Array
642+
*/
643+
@SuppressWarnings("unchecked")
644+
// suppress unchecked because we are using varargs inside the method
645+
public static <T> Observable<T> from(T t1, T t2, T t3, T t4) {
646+
return from(Arrays.asList(t1, t2, t3, t4));
647+
}
648+
649+
/**
650+
* Converts a series of items into an Observable.
651+
* <p>
652+
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
653+
*
654+
* <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,
655+
* it in not possible to unsubscribe from the sequence before it completes.
656+
*
657+
* @param t1
658+
* item
659+
* @param t2
660+
* item
661+
* @param t3
662+
* item
663+
* @param t4
664+
* item
665+
* @param t5
666+
* item
667+
* @param <T>
668+
* the type of items in the Array, and the type of items to be emitted by the
669+
* resulting Observable
670+
* @return an Observable that emits each item in the source Array
671+
*/
672+
@SuppressWarnings("unchecked")
673+
// suppress unchecked because we are using varargs inside the method
674+
public static <T> Observable<T> from(T t1, T t2, T t3, T t4, T t5) {
675+
return from(Arrays.asList(t1, t2, t3, t4, t5));
676+
}
677+
678+
/**
679+
* Converts a series of items into an Observable.
680+
* <p>
681+
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
682+
*
683+
* <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,
684+
* it in not possible to unsubscribe from the sequence before it completes.
685+
*
686+
* @param t1
687+
* item
688+
* @param t2
689+
* item
690+
* @param t3
691+
* item
692+
* @param t4
693+
* item
694+
* @param t5
695+
* item
696+
* @param t6
697+
* item
698+
* @param <T>
699+
* the type of items in the Array, and the type of items to be emitted by the
700+
* resulting Observable
701+
* @return an Observable that emits each item in the source Array
702+
*/
703+
@SuppressWarnings("unchecked")
704+
// suppress unchecked because we are using varargs inside the method
705+
public static <T> Observable<T> from(T t1, T t2, T t3, T t4, T t5, T t6) {
706+
return from(Arrays.asList(t1, t2, t3, t4, t5, t6));
707+
}
708+
709+
/**
710+
* Converts a series of items into an Observable.
711+
* <p>
712+
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
713+
*
714+
* <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,
715+
* it in not possible to unsubscribe from the sequence before it completes.
716+
*
717+
* @param t1
718+
* item
719+
* @param t2
720+
* item
721+
* @param t3
722+
* item
723+
* @param t4
724+
* item
725+
* @param t5
726+
* item
727+
* @param t6
728+
* item
729+
* @param t7
730+
* item
731+
* @param <T>
732+
* the type of items in the Array, and the type of items to be emitted by the
733+
* resulting Observable
734+
* @return an Observable that emits each item in the source Array
735+
*/
736+
@SuppressWarnings("unchecked")
737+
// suppress unchecked because we are using varargs inside the method
738+
public static <T> Observable<T> from(T t1, T t2, T t3, T t4, T t5, T t6, T t7) {
739+
return from(Arrays.asList(t1, t2, t3, t4, t5, t6, t7));
740+
}
741+
742+
/**
743+
* Converts a series of items into an Observable.
744+
* <p>
745+
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
746+
*
747+
* <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,
748+
* it in not possible to unsubscribe from the sequence before it completes.
749+
*
750+
* @param t1
751+
* item
752+
* @param t2
753+
* item
754+
* @param t3
755+
* item
756+
* @param t4
757+
* item
758+
* @param t5
759+
* item
760+
* @param t6
761+
* item
762+
* @param t7
763+
* item
764+
* @param t8
765+
* item
766+
* @param <T>
767+
* the type of items in the Array, and the type of items to be emitted by the
768+
* resulting Observable
769+
* @return an Observable that emits each item in the source Array
770+
*/
771+
@SuppressWarnings("unchecked")
772+
// suppress unchecked because we are using varargs inside the method
773+
public static <T> Observable<T> from(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8) {
774+
return from(Arrays.asList(t1, t2, t3, t4, t5, t6, t7, t8));
775+
}
776+
777+
/**
778+
* Converts a series of items into an Observable.
779+
* <p>
780+
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
781+
*
782+
* <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,
783+
* it in not possible to unsubscribe from the sequence before it completes.
784+
*
785+
* @param t1
786+
* item
787+
* @param t2
788+
* item
789+
* @param t3
790+
* item
791+
* @param t4
792+
* item
793+
* @param t5
794+
* item
795+
* @param t6
796+
* item
797+
* @param t7
798+
* item
799+
* @param t8
800+
* item
801+
* @param t9
802+
* item
803+
* @param <T>
804+
* the type of items in the Array, and the type of items to be emitted by the
805+
* resulting Observable
806+
* @return an Observable that emits each item in the source Array
807+
*/
808+
@SuppressWarnings("unchecked")
809+
// suppress unchecked because we are using varargs inside the method
810+
public static <T> Observable<T> from(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8, T t9) {
811+
return from(Arrays.asList(t1, t2, t3, t4, t5, t6, t7, t8, t9));
812+
}
813+
814+
/**
815+
* Converts a series of items into an Observable.
816+
* <p>
817+
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/from.png">
818+
*
819+
* <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,
820+
* it in not possible to unsubscribe from the sequence before it completes.
821+
*
822+
* @param t1
823+
* item
824+
* @param t2
825+
* item
826+
* @param t3
827+
* item
828+
* @param t4
829+
* item
830+
* @param t5
831+
* item
832+
* @param t6
833+
* item
834+
* @param t7
835+
* item
836+
* @param t8
837+
* item
838+
* @param t10
839+
* item
840+
* @param <T>
841+
* the type of items in the Array, and the type of items to be emitted by the
842+
* resulting Observable
843+
* @return an Observable that emits each item in the source Array
844+
*/
845+
@SuppressWarnings("unchecked")
846+
// suppress unchecked because we are using varargs inside the method
847+
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) {
848+
return from(Arrays.asList(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10));
550849
}
551850

552851
/**

rxjava-core/src/main/java/rx/observables/BlockingObservable.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ public void testLast() {
387387

388388
@Test
389389
public void testLastEmptyObservable() {
390-
BlockingObservable<Object> obs = BlockingObservable.from(Observable.from());
390+
BlockingObservable<Object> obs = BlockingObservable.from(Observable.empty());
391391

392392
assertNull(obs.last());
393393
}
@@ -412,7 +412,7 @@ public void testLastOrDefault1() {
412412

413413
@Test
414414
public void testLastOrDefault2() {
415-
BlockingObservable<Object> observable = BlockingObservable.from(Observable.from());
415+
BlockingObservable<Object> observable = BlockingObservable.from(Observable.empty());
416416
assertEquals("default", observable.lastOrDefault("default"));
417417
}
418418

@@ -460,7 +460,7 @@ public void testSingle() {
460460

461461
@Test
462462
public void testSingleDefault() {
463-
BlockingObservable<Object> observable = BlockingObservable.from(Observable.from());
463+
BlockingObservable<Object> observable = BlockingObservable.from(Observable.empty());
464464
assertEquals("default", observable.singleOrDefault("default"));
465465
}
466466

0 commit comments

Comments
 (0)