Skip to content

Commit 7607f8f

Browse files
committed
Add sequenceEqual to RxScala
1 parent 50b618e commit 7607f8f

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

language-adaptors/rxjava-scala/src/examples/scala/rx/lang/scala/examples/RxScalaDemo.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,4 +790,13 @@ class RxScalaDemo extends JUnitSuite {
790790
assertEquals(List(1, 2, 3, 4), o.toBlockingObservable.toList)
791791
}
792792

793+
@Test def sequenceEqualExampe(): Unit = {
794+
val o1 = List(1, 2, 3).toObservable
795+
val o2 = List(1, 2, 3).toObservable
796+
val o3 = List(1, 2).toObservable
797+
val o4 = List(1.0, 2.0, 3.0).toObservable
798+
assertTrue(o1.sequenceEqual(o2).toBlockingObservable.single)
799+
assertFalse(o1.sequenceEqual(o3).toBlockingObservable.single)
800+
assertTrue(o1.sequenceEqual(o4).toBlockingObservable.single)
801+
}
793802
}

language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/Observable.scala

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2746,6 +2746,38 @@ trait Observable[+T]
27462746
toScalaObservable[util.Map[K, V]](o).map(m => mapFactory() ++ m.toMap)
27472747
}
27482748

2749+
/**
2750+
* Returns an Observable that emits a Boolean value that indicates whether `this` and `that` Observable sequences are the
2751+
* same by comparing the items emitted by each Observable pairwise.
2752+
* <p>
2753+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/sequenceEqual.png">
2754+
*
2755+
* Note: this method uses `==` to compare elements. It's a bit different from RxJava which uses `Object.equals`.
2756+
*
2757+
* @param that the Observable to compare
2758+
* @return an Observable that emits a `Boolean` value that indicates whether the two sequences are the same
2759+
*/
2760+
def sequenceEqual[U >: T](that: Observable[U]): Observable[Boolean] = {
2761+
sequenceEqual(that, (_1: U, _2: U) => _1 == _2)
2762+
}
2763+
2764+
/**
2765+
* Returns an Observable that emits a Boolean value that indicates whether `this` and `that` Observable sequences are the
2766+
* same by comparing the items emitted by each Observable pairwise based on the results of a specified `equality` function.
2767+
* <p>
2768+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/sequenceEqual.png">
2769+
*
2770+
* @param that the Observable to compare
2771+
* @param equality a function used to compare items emitted by each Observable
2772+
* @return an Observable that emits a `Boolean` value that indicates whether the two sequences are the same based on the `equality` function.
2773+
*/
2774+
def sequenceEqual[U >: T](that: Observable[U], equality: (U, U) => Boolean): Observable[Boolean] = {
2775+
val thisJava: rx.Observable[_ <: U] = this.asJavaObservable
2776+
val thatJava: rx.Observable[_ <: U] = that.asJavaObservable
2777+
val equalityJava: Func2[_ >: U, _ >: U, java.lang.Boolean] = equality
2778+
toScalaObservable[java.lang.Boolean](rx.Observable.sequenceEqual[U](thisJava, thatJava, equalityJava)).map(_.booleanValue)
2779+
}
2780+
27492781
/**
27502782
* Lift a function to the current Observable and return a new Observable that when subscribed to will pass
27512783
* the values of the current Observable through the function.

0 commit comments

Comments
 (0)