Skip to content

Commit a2274d3

Browse files
Merge pull request #1056 from zsxwing/scala-drop
Add drop(skip) and dropRight(skipLast) to rxscala
2 parents 315a994 + 6b4d8f5 commit a2274d3

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,32 @@ class RxScalaDemo extends JUnitSuite {
400400
assertEquals(1, List(1).toObservable.toBlockingObservable.single)
401401
}
402402

403+
@Test def dropExample() {
404+
val o = List(1, 2, 3, 4).toObservable
405+
assertEquals(List(3, 4), o.drop(2).toBlockingObservable.toList)
406+
}
407+
408+
@Test def dropWithTimeExample() {
409+
val o = List(1, 2, 3, 4).toObservable.zip(
410+
Observable.interval(500 millis, IOScheduler())).map(_._1) // emit every 500 millis
411+
println(
412+
o.drop(1250 millis, IOScheduler()).toBlockingObservable.toList // output List(3, 4)
413+
)
414+
}
415+
416+
@Test def dropRightExample() {
417+
val o = List(1, 2, 3, 4).toObservable
418+
assertEquals(List(1, 2), o.dropRight(2).toBlockingObservable.toList)
419+
}
420+
421+
@Test def dropRightWithTimeExample() {
422+
val o = List(1, 2, 3, 4).toObservable.zip(
423+
Observable.interval(500 millis, IOScheduler())).map(_._1) // emit every 500 millis
424+
println(
425+
o.dropRight(750 millis, IOScheduler()).toBlockingObservable.toList // output List(1, 2)
426+
)
427+
}
428+
403429
def square(x: Int): Int = {
404430
println(s"$x*$x is being calculated on thread ${Thread.currentThread().getId}")
405431
Thread.sleep(100) // calculating a square is heavy work :)

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,35 @@ trait Observable[+T]
12311231
toScalaObservable[T](asJavaObservable.skip(n))
12321232
}
12331233

1234+
/**
1235+
* Returns an Observable that drops values emitted by the source Observable before a specified time window
1236+
* elapses.
1237+
*
1238+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/skip.t.png">
1239+
*
1240+
* @param time the length of the time window to drop
1241+
* @return an Observable that drops values emitted by the source Observable before the time window defined
1242+
* by `time` elapses and emits the remainder
1243+
*/
1244+
def drop(time: Duration): Observable[T] = {
1245+
toScalaObservable(asJavaObservable.skip(time.length, time.unit))
1246+
}
1247+
1248+
/**
1249+
* Returns an Observable that drops values emitted by the source Observable before a specified time window
1250+
* elapses.
1251+
*
1252+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/skip.t.png">
1253+
*
1254+
* @param time the length of the time window to drop
1255+
* @param scheduler the `Scheduler` on which the timed wait happens
1256+
* @return an Observable that drops values emitted by the source Observable before the time window defined
1257+
* by `time` elapses and emits the remainder
1258+
*/
1259+
def drop(time: Duration, scheduler: Scheduler): Observable[T] = {
1260+
toScalaObservable(asJavaObservable.skip(time.length, time.unit, scheduler))
1261+
}
1262+
12341263
/**
12351264
* Returns an Observable that bypasses all items from the source Observable as long as the specified
12361265
* condition holds true. Emits all further source items as soon as the condition becomes false.
@@ -1246,6 +1275,58 @@ trait Observable[+T]
12461275
toScalaObservable(asJavaObservable.skipWhile(predicate))
12471276
}
12481277

1278+
/**
1279+
* Returns an Observable that drops a specified number of items from the end of the sequence emitted by the
1280+
* source Observable.
1281+
* <p>
1282+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/skipLast.png">
1283+
* <p>
1284+
* This Observer accumulates a queue long enough to store the first `n` items. As more items are
1285+
* received, items are taken from the front of the queue and emitted by the returned Observable. This causes
1286+
* such items to be delayed.
1287+
*
1288+
* @param n number of items to drop from the end of the source sequence
1289+
* @return an Observable that emits the items emitted by the source Observable except for the dropped ones
1290+
* at the end
1291+
* @throws IndexOutOfBoundsException if `n` is less than zero
1292+
*/
1293+
def dropRight(n: Int): Observable[T] = {
1294+
toScalaObservable(asJavaObservable.skipLast(n))
1295+
}
1296+
1297+
/**
1298+
* Returns an Observable that drops items emitted by the source Observable during a specified time window
1299+
* before the source completes.
1300+
* <p>
1301+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/skipLast.t.png">
1302+
*
1303+
* Note: this action will cache all items until "onCompleted" arrives. So don't use it on an infinite Observable.
1304+
*
1305+
* @param time the length of the time window
1306+
* @return an Observable that drops those items emitted by the source Observable in a time window before the
1307+
* source completes defined by `time`
1308+
*/
1309+
def dropRight(time: Duration): Observable[T] = {
1310+
toScalaObservable(asJavaObservable.skipLast(time.length, time.unit))
1311+
}
1312+
1313+
/**
1314+
* Returns an Observable that drops items emitted by the source Observable during a specified time window
1315+
* (defined on a specified scheduler) before the source completes.
1316+
* <p>
1317+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/skipLast.ts.png">
1318+
*
1319+
* Note: this action will cache all items until "onCompleted" arrives. So don't use it on an infinite Observable.
1320+
*
1321+
* @param time the length of the time window
1322+
* @param scheduler the scheduler used as the time source
1323+
* @return an Observable that drops those items emitted by the source Observable in a time window before the
1324+
* source completes defined by `time` and `scheduler`
1325+
*/
1326+
def dropRight(time: Duration, scheduler: Scheduler): Observable[T] = {
1327+
toScalaObservable(asJavaObservable.skipLast(time.length, time.unit, scheduler))
1328+
}
1329+
12491330
/**
12501331
* Returns an Observable that emits only the first `num` items emitted by the source
12511332
* Observable.

rxjava-core/src/main/java/rx/Observable.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5566,6 +5566,8 @@ public final Observable<T> skipLast(int count) {
55665566
* before the source completes.
55675567
* <p>
55685568
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/skipLast.t.png">
5569+
*
5570+
* Note: this action will cache all items until "onCompleted" arrives. So don't use it on an infinite Observable.
55695571
*
55705572
* @param time
55715573
* the length of the time window
@@ -5585,7 +5587,9 @@ public final Observable<T> skipLast(long time, TimeUnit unit) {
55855587
* (defined on a specified scheduler) before the source completes.
55865588
* <p>
55875589
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/skipLast.ts.png">
5588-
*
5590+
*
5591+
* Note: this action will cache all items until "onCompleted" arrives. So don't use it on an infinite Observable.
5592+
*
55895593
* @param time
55905594
* the length of the time window
55915595
* @param unit

0 commit comments

Comments
 (0)