Skip to content

Commit 11a18aa

Browse files
committed
Add ElementAt and ElementAtOrDefault to rxjava-scala
1 parent 25555b4 commit 11a18aa

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-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
@@ -564,4 +564,13 @@ class RxScalaDemo extends JUnitSuite {
564564
println(result)
565565
}
566566

567+
@Test def elementAtExample(): Unit = {
568+
val o = List("red", "green", "blue").toObservable
569+
println(o(2).toBlockingObservable.single)
570+
}
571+
572+
@Test def elementAtOrDefaultExample(): Unit = {
573+
val o : Observable[Seq[Char]] = List("red".toList, "green".toList, "blue".toList).toObservable.elementAtOrDefault(3, "black".toSeq)
574+
println(o.toBlockingObservable.single)
575+
}
567576
}

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,6 +2305,65 @@ trait Observable[+T]
23052305
def delaySubscription(delay: Duration, scheduler: Scheduler): Observable[T] = {
23062306
toScalaObservable[T](asJavaObservable.delaySubscription(delay.length, delay.unit, scheduler))
23072307
}
2308+
2309+
/**
2310+
* Returns an Observable that emits the single item at a specified index in a sequence of emissions from a
2311+
* source Observbable.
2312+
*
2313+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/elementAt.png">
2314+
*
2315+
* @param index
2316+
* the zero-based index of the item to retrieve
2317+
* @return an Observable that emits a single item: the item at the specified position in the sequence of
2318+
* those emitted by the source Observable
2319+
* @throws IndexOutOfBoundsException
2320+
* if index is greater than or equal to the number of items emitted by the source
2321+
* Observable
2322+
* @throws IndexOutOfBoundsException
2323+
* if index is less than 0
2324+
* @see `Observable.elementAt`
2325+
*/
2326+
def apply(index: Int): Observable[T] = elementAt(index)
2327+
2328+
/**
2329+
* Returns an Observable that emits the single item at a specified index in a sequence of emissions from a
2330+
* source Observbable.
2331+
*
2332+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/elementAt.png">
2333+
*
2334+
* @param index
2335+
* the zero-based index of the item to retrieve
2336+
* @return an Observable that emits a single item: the item at the specified position in the sequence of
2337+
* those emitted by the source Observable
2338+
* @throws IndexOutOfBoundsException
2339+
* if index is greater than or equal to the number of items emitted by the source
2340+
* Observable
2341+
* @throws IndexOutOfBoundsException
2342+
* if index is less than 0
2343+
*/
2344+
def elementAt(index: Int): Observable[T] = {
2345+
toScalaObservable[T](asJavaObservable.elementAt(index))
2346+
}
2347+
2348+
/**
2349+
* Returns an Observable that emits the item found at a specified index in a sequence of emissions from a
2350+
* source Observable, or a default item if that index is out of range.
2351+
*
2352+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/elementAtOrDefault.png">
2353+
*
2354+
* @param index
2355+
* the zero-based index of the item to retrieve
2356+
* @param defaultValue
2357+
* the default item
2358+
* @return an Observable that emits the item at the specified position in the sequence emitted by the source
2359+
* Observable, or the default item if that index is outside the bounds of the source sequence
2360+
* @throws IndexOutOfBoundsException
2361+
* if {@code index} is less than 0
2362+
*/
2363+
def elementAtOrDefault[U >: T](index: Int, default: U): Observable[U] = {
2364+
val thisJava = asJavaObservable.asInstanceOf[rx.Observable[U]]
2365+
toScalaObservable[U](thisJava.elementAtOrDefault(index, default))
2366+
}
23082367
}
23092368

23102369
/**

0 commit comments

Comments
 (0)