@@ -3813,6 +3813,63 @@ trait Observable[+T]
3813
3813
def foreach (onNext : T => Unit , onError : Throwable => Unit , onComplete : () => Unit ): Unit = {
3814
3814
asJavaObservable.subscribe(onNext, onError, onComplete)
3815
3815
}
3816
+
3817
+ /**
3818
+ * Pivots a sequence of `(K1, Observable[(K2, Observable[U])])`s emitted by an `Observable` so as to swap the group
3819
+ * and and the set on which their items are grouped.
3820
+ * <p>
3821
+ * <img width="640" height="580" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/pivot.png">
3822
+ *
3823
+ * For example an `Observable` such as `this = Observable[(String, Observable[(Boolean, Observable[Integer])])`:
3824
+ * <ul>
3825
+ * <li>o1.odd: 1, 3, 5, 7, 9 on Thread 1</li>
3826
+ * <li>o1.even: 2, 4, 6, 8, 10 on Thread 1</li>
3827
+ * <li>o2.odd: 11, 13, 15, 17, 19 on Thread 2</li>
3828
+ * <li>o2.even: 12, 14, 16, 18, 20 on Thread 2</li>
3829
+ * </ul>
3830
+ * is pivoted to become `this = Observable[(Boolean, Observable[(String, Observable[Integer])])`:
3831
+ *
3832
+ * <ul>
3833
+ * <li>odd.o1: 1, 3, 5, 7, 9 on Thread 1</li>
3834
+ * <li>odd.o2: 11, 13, 15, 17, 19 on Thread 2</li>
3835
+ * <li>even.o1: 2, 4, 6, 8, 10 on Thread 1</li>
3836
+ * <li>even.o2: 12, 14, 16, 18, 20 on Thread 2</li>
3837
+ * </ul>
3838
+ * <p>
3839
+ * <img width="640" height="1140" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/pivot.ex.png">
3840
+ * <p>
3841
+ * <em>Note:</em> A `(K, Observable[_])` will cache the items it is to emit until such time as it
3842
+ * is subscribed to. For this reason, in order to avoid memory leaks, you should not simply ignore those
3843
+ * `(K, Observable[_])`s that do not concern you. Instead, you can signal to them that they may
3844
+ * discard their buffers by applying an operator like `take(0)` to them.
3845
+ *
3846
+ * @return an `Observable`containing a stream of nested `(K1, Observable[(K2, Observable[U])])`s with swapped
3847
+ * inner-outer keys.
3848
+ */
3849
+ def pivot [U , K1 , K2 ](implicit evidence : Observable [T ] <:< Observable [(K1 , Observable [(K2 , Observable [U ])])]): Observable [(K2 , Observable [(K1 , Observable [U ])])] = {
3850
+ import rx .observables .{GroupedObservable => JGroupedObservable }
3851
+ val f1 = new Func1 [(K1 , Observable [(K2 , Observable [U ])]), JGroupedObservable [K1 , JGroupedObservable [K2 , U ]]]() {
3852
+ override def call (t1 : (K1 , Observable [(K2 , Observable [U ])])): JGroupedObservable [K1 , JGroupedObservable [K2 , U ]] = {
3853
+ val jo = t1._2.asJavaObservable.asInstanceOf [rx.Observable [(K2 , Observable [U ])]].map[JGroupedObservable [K2 , U ]](new Func1 [(K2 , Observable [U ]), JGroupedObservable [K2 , U ]]() {
3854
+ override def call (t2 : (K2 , Observable [U ])): JGroupedObservable [K2 , U ] = {
3855
+ JGroupedObservable .from(t2._1, t2._2.asJavaObservable.asInstanceOf [rx.Observable [U ]])
3856
+ }
3857
+ })
3858
+ JGroupedObservable .from(t1._1, jo)
3859
+ }
3860
+ }
3861
+ val o1 : Observable [(K1 , Observable [(K2 , Observable [U ])])] = this
3862
+ val o2 = toScalaObservable[JGroupedObservable [K2 , JGroupedObservable [K1 , U ]]](rx.Observable .pivot(o1.asJavaObservable.map(f1)))
3863
+ o2.map {
3864
+ (jgo1 : JGroupedObservable [K2 , JGroupedObservable [K1 , U ]]) => {
3865
+ val jo = jgo1.map[(K1 , Observable [U ])](new Func1 [JGroupedObservable [K1 , U ], (K1 , Observable [U ])]() {
3866
+ override def call (jgo2 : JGroupedObservable [K1 , U ]): (K1 , Observable [U ]) = (jgo2.getKey, toScalaObservable[U ](jgo2))
3867
+ })
3868
+ (jgo1.getKey, toScalaObservable[(K1 , Observable [U ])](jo))
3869
+ }
3870
+ }
3871
+ }
3872
+
3816
3873
}
3817
3874
3818
3875
/**
0 commit comments