Skip to content

Commit bb78809

Browse files
committed
Add 'contains' to RxScala
1 parent baf568d commit bb78809

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-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
@@ -25,6 +25,7 @@ import scala.language.implicitConversions
2525

2626
import org.junit.Assert.assertEquals
2727
import org.junit.Assert.assertTrue
28+
import org.junit.Assert.assertFalse
2829
import org.junit.Ignore
2930
import org.junit.Test
3031
import org.scalatest.junit.JUnitSuite
@@ -645,6 +646,14 @@ class RxScalaDemo extends JUnitSuite {
645646
println(m.toBlockingObservable.single)
646647
}
647648

649+
@Test def containsExample(): Unit = {
650+
val o1 = List(1, 2, 3).toObservable.contains(2)
651+
assertTrue(o1.toBlockingObservable.single)
652+
653+
val o2 = List(1, 2, 3).toObservable.contains(4)
654+
assertFalse(o2.toBlockingObservable.single)
655+
}
656+
648657
@Test def retryExample1(): Unit = {
649658
val o : Observable[String] = List("alice", "bob", "carol").toObservable
650659
assertEquals(List("alice", "bob", "carol"), o.retry.toBlockingObservable.toList)

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,22 @@ trait Observable[+T]
10611061
toScalaObservable[T](asJavaObservable.cache())
10621062
}
10631063

1064+
/**
1065+
* Returns an Observable that emits a Boolean that indicates whether the source Observable emitted a
1066+
* specified item.
1067+
*
1068+
* Note: this method uses `==` to compare elements. It's a bit different from RxJava which uses `Object.equals`.
1069+
* <p>
1070+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/contains.png">
1071+
*
1072+
*@param elem the item to search for in the emissions from the source Observable
1073+
* @return an Observable that emits `true` if the specified item is emitted by the source Observable,
1074+
* or `false` if the source Observable completes without emitting that item
1075+
*/
1076+
def contains(elem: Any): Observable[Boolean] = {
1077+
exists(_ == elem)
1078+
}
1079+
10641080
/**
10651081
* Returns a a pair of a start function and an [[rx.lang.scala.Observable]], which waits until the start function is called before it begins emitting
10661082
* items to those [[rx.lang.scala.Observer]]s that have subscribed to it.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class CompletenessTest extends JUnitSuite {
7171
"all(Func1[_ >: T, Boolean])" -> "forall(T => Boolean)",
7272
"buffer(Long, Long, TimeUnit)" -> "buffer(Duration, Duration)",
7373
"buffer(Long, Long, TimeUnit, Scheduler)" -> "buffer(Duration, Duration, Scheduler)",
74+
"contains(T)" -> "contains(Any)",
7475
"count()" -> "length",
7576
"dematerialize()" -> "dematerialize(<:<[Observable[T], Observable[Notification[U]]])",
7677
"elementAt(Int)" -> "[use `.drop(index).first`]",

0 commit comments

Comments
 (0)