Skip to content

Commit c7a52ee

Browse files
committed
RxScala: Add Scala idiomatic methods
1 parent 3e7b1f5 commit c7a52ee

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,10 +1842,7 @@ trait Observable[+T]
18421842
* Observable satisfy the predicate; otherwise, `false`
18431843
*/
18441844
def forall(predicate: T => Boolean): Observable[Boolean] = {
1845-
// type mismatch; found : rx.Observable[java.lang.Boolean] required: rx.Observable[_ <: scala.Boolean]
1846-
// new Observable[Boolean](asJavaNotification.all(predicate))
1847-
// it's more fun in Scala:
1848-
this.map(predicate).foldLeft(true)(_ && _)
1845+
toScalaObservable[java.lang.Boolean](asJavaObservable.all(predicate)).map(_.booleanValue())
18491846
}
18501847

18511848
/**
@@ -4122,6 +4119,35 @@ trait Observable[+T]
41224119
*/
41234120
def toArray[U >: T : ClassTag]: Observable[Array[U]] = // use U >: T because Array is invariant
41244121
toBuffer[U].map(_.toArray)
4122+
4123+
/**
4124+
* Returns an [[Observable]] which only emits elements which do not satisfy a predicate.
4125+
*
4126+
* @param p the predicate used to test elements.
4127+
* @return Returns an [[Observable]] which only emits elements which do not satisfy a predicate.
4128+
*/
4129+
def filterNot(p: T => Boolean): Observable[T] = {
4130+
filter(!p(_))
4131+
}
4132+
4133+
/**
4134+
* Return an [[Observable]] which emits the number of elements in the source [[Observable]] which satisfy a predicate.
4135+
*
4136+
* @param p the predicate used to test elements.
4137+
* @return an [[Observable]] which emits the number of elements in the source [[Observable]] which satisfy a predicate.
4138+
*/
4139+
def count(p: T => Boolean): Observable[Int] = {
4140+
filter(p).length
4141+
}
4142+
4143+
/**
4144+
* Return an [[Observable]] emitting one single `Boolean`, which is `true` if the source [[Observable]] emits any element, and `false` otherwise.
4145+
*
4146+
* @return an [[Observable]] emitting one single Boolean`, which is `true` if the source [[Observable]] emits any element, and `false otherwise.
4147+
*/
4148+
def nonEmpty: Observable[Boolean] = {
4149+
isEmpty.map(!_)
4150+
}
41254151
}
41264152

41274153
/**

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,22 @@ class ObservableTests extends JUnitSuite {
346346
val o = Observable.items(1, 2, 3).toArray
347347
assertArrayEquals(Array(1, 2, 3), o.toBlocking.single)
348348
}
349+
350+
@Test
351+
def testFilterNot() {
352+
val o = Observable.items(1, 2, 3).filterNot(_ > 2)
353+
assertEquals(List(1, 2), o.toBlocking.toList)
354+
}
355+
356+
@Test
357+
def testCount() {
358+
assertEquals(1, Observable.items(1, 2, 3).count(_ > 2).toBlocking.single)
359+
assertEquals(2, Observable.items(1, 2, 3).count(_ <= 2).toBlocking.single)
360+
}
361+
362+
@Test
363+
def testNonEmpty() {
364+
assertEquals(false, Observable.empty.nonEmpty.toBlocking.single)
365+
assertEquals(true, Observable.items(1, 2, 3).nonEmpty.toBlocking.single)
366+
}
349367
}

0 commit comments

Comments
 (0)