Skip to content

Commit d5074f4

Browse files
committed
Add 'repeat' to RxScala
1 parent bb78809 commit d5074f4

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,16 @@ class RxScalaDemo extends JUnitSuite {
654654
assertFalse(o2.toBlockingObservable.single)
655655
}
656656

657+
@Test def repeatExample1(): Unit = {
658+
val o : Observable[String] = List("alice", "bob", "carol").toObservable.repeat().take(6)
659+
assertEquals(List("alice", "bob", "carol", "alice", "bob", "carol"), o.toBlockingObservable.toList)
660+
}
661+
662+
@Test def repeatExample2(): Unit = {
663+
val o : Observable[String] = List("alice", "bob", "carol").toObservable.repeat(2)
664+
assertEquals(List("alice", "bob", "carol", "alice", "bob", "carol"), o.toBlockingObservable.toList)
665+
}
666+
657667
@Test def retryExample1(): Unit = {
658668
val o : Observable[String] = List("alice", "bob", "carol").toObservable
659669
assertEquals(List("alice", "bob", "carol"), o.retry.toBlockingObservable.toList)

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,6 +2253,68 @@ trait Observable[+T]
22532253
toScalaObservable[T](asJavaObservable.retry())
22542254
}
22552255

2256+
/**
2257+
* Returns an Observable that repeats the sequence of items emitted by the source Observable indefinitely.
2258+
* <p>
2259+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/repeat.o.png">
2260+
*
2261+
* @return an Observable that emits the items emitted by the source Observable repeatedly and in sequence
2262+
* @see <a href="https://github.com/Netflix/RxJava/wiki/Creating-Observables#wiki-repeat">RxJava Wiki: repeat()</a>
2263+
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229428.aspx">MSDN: Observable.Repeat</a>
2264+
*/
2265+
def repeat(): Observable[T] = {
2266+
toScalaObservable[T](asJavaObservable.repeat())
2267+
}
2268+
2269+
/**
2270+
* Returns an Observable that repeats the sequence of items emitted by the source Observable indefinitely,
2271+
* on a particular Scheduler.
2272+
* <p>
2273+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/repeat.os.png">
2274+
*
2275+
* @param scheduler the Scheduler to emit the items on
2276+
* @return an Observable that emits the items emitted by the source Observable repeatedly and in sequence
2277+
* @see <a href="https://github.com/Netflix/RxJava/wiki/Creating-Observables#wiki-repeat">RxJava Wiki: repeat()</a>
2278+
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229428.aspx">MSDN: Observable.Repeat</a>
2279+
*/
2280+
def repeat(scheduler: Scheduler): Observable[T] = {
2281+
toScalaObservable[T](asJavaObservable.repeat(scheduler))
2282+
}
2283+
2284+
/**
2285+
* Returns an Observable that repeats the sequence of items emitted by the source Observable at most `count` times.
2286+
* <p>
2287+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/repeat.on.png">
2288+
*
2289+
* @param count the number of times the source Observable items are repeated,
2290+
* a count of 0 will yield an empty sequence
2291+
* @return an Observable that repeats the sequence of items emitted by the source Observable at most `count` times
2292+
* @throws IllegalArgumentException if `count` is less than zero
2293+
* @see <a href="https://github.com/Netflix/RxJava/wiki/Creating-Observables#wiki-repeat">RxJava Wiki: repeat()</a>
2294+
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229428.aspx">MSDN: Observable.Repeat</a>
2295+
*/
2296+
def repeat(count: Long): Observable[T] = {
2297+
toScalaObservable[T](asJavaObservable.repeat(count))
2298+
}
2299+
2300+
/**
2301+
* Returns an Observable that repeats the sequence of items emitted by the source Observable
2302+
* at most `count` times, on a particular Scheduler.
2303+
* <p>
2304+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/repeat.ons.png">
2305+
*
2306+
* @param count the number of times the source Observable items are repeated,
2307+
* a count of 0 will yield an empty sequence
2308+
* @param scheduler the `Scheduler` to emit the items on
2309+
* @return an Observable that repeats the sequence of items emitted by the source Observable at most `count` times
2310+
* on a particular Scheduler
2311+
* @see <a href="https://github.com/Netflix/RxJava/wiki/Creating-Observables#wiki-repeat">RxJava Wiki: repeat()</a>
2312+
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229428.aspx">MSDN: Observable.Repeat</a>
2313+
*/
2314+
def repeat(count: Long, scheduler: Scheduler): Observable[T] = {
2315+
toScalaObservable[T](asJavaObservable.repeat(count, scheduler))
2316+
}
2317+
22562318
/**
22572319
* Converts an Observable into a [[rx.lang.scala.observables.BlockingObservable]] (an Observable with blocking
22582320
* operators).

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ class CompletenessTest extends JUnitSuite {
143143
"mergeDelayError(Observable[_ <: T], Observable[_ <: T])" -> "mergeDelayError(Observable[U])",
144144
"mergeDelayError(Observable[_ <: Observable[_ <: T]])" -> "flattenDelayError(<:<[Observable[T], Observable[Observable[U]]])",
145145
"range(Int, Int)" -> "apply(Range)",
146+
"repeat()" -> "repeat()",
147+
"retry()" -> "retry()",
146148
"sequenceEqual(Observable[_ <: T], Observable[_ <: T])" -> "[use `(first zip second) map (p => p._1 == p._2)`]",
147149
"sequenceEqual(Observable[_ <: T], Observable[_ <: T], Func2[_ >: T, _ >: T, Boolean])" -> "[use `(first zip second) map (p => equality(p._1, p._2))`]",
148150
"sum(Observable[Integer])" -> "sum(Numeric[U])",

0 commit comments

Comments
 (0)