15
15
*/
16
16
package rx ;
17
17
18
- import static rx .util .functions .Functions .not ;
18
+ import static rx .util .functions .Functions .* ;
19
19
20
20
import java .util .ArrayList ;
21
21
import java .util .Arrays ;
22
22
import java .util .Collection ;
23
- import java .util .Collections ;
24
23
import java .util .List ;
25
24
import java .util .concurrent .Future ;
26
25
import java .util .concurrent .TimeUnit ;
65
64
import rx .operators .OperationTakeLast ;
66
65
import rx .operators .OperationTakeUntil ;
67
66
import rx .operators .OperationTakeWhile ;
67
+ import rx .operators .OperationThrottleFirst ;
68
+ import rx .operators .OperationDebounce ;
68
69
import rx .operators .OperationTimestamp ;
69
70
import rx .operators .OperationToObservableFuture ;
70
71
import rx .operators .OperationToObservableIterable ;
@@ -1810,6 +1811,182 @@ public static Observable<Long> interval(long interval, TimeUnit unit, Scheduler
1810
1811
return create (OperationInterval .interval (interval , unit , scheduler ));
1811
1812
}
1812
1813
1814
+ /**
1815
+ * Debounces by dropping all values that are followed by newer values before the timeout value expires. The timer resets on each `onNext` call.
1816
+ * <p>
1817
+ * NOTE: If events keep firing faster than the timeout then no data will be emitted.
1818
+ * <p>
1819
+ * <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/debounce.png">
1820
+ * <p>
1821
+ * Information on debounce vs throttle:
1822
+ * <p>
1823
+ * <ul>
1824
+ * <li>http://drupalmotion.com/article/debounce-and-throttle-visual-explanation</li>
1825
+ * <li>http://unscriptable.com/2009/03/20/debouncing-javascript-methods/</li>
1826
+ * <li>http://www.illyriad.co.uk/blog/index.php/2011/09/javascript-dont-spam-your-server-debounce-and-throttle/</li>
1827
+ * </ul>
1828
+ *
1829
+ * @param timeout
1830
+ * The time each value has to be 'the most recent' of the {@link Observable} to ensure that it's not dropped.
1831
+ * @param unit
1832
+ * The {@link TimeUnit} for the timeout.
1833
+ *
1834
+ * @return An {@link Observable} which filters out values which are too quickly followed up with newer values.
1835
+ * @see {@link #throttleWithTimeout};
1836
+ */
1837
+ public Observable <T > debounce (long timeout , TimeUnit unit ) {
1838
+ return create (OperationDebounce .debounce (this , timeout , unit ));
1839
+ }
1840
+
1841
+ /**
1842
+ * Debounces by dropping all values that are followed by newer values before the timeout value expires. The timer resets on each `onNext` call.
1843
+ * <p>
1844
+ * NOTE: If events keep firing faster than the timeout then no data will be emitted.
1845
+ * <p>
1846
+ * <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/debounce.png">
1847
+ * <p>
1848
+ * Information on debounce vs throttle:
1849
+ * <p>
1850
+ * <ul>
1851
+ * <li>http://drupalmotion.com/article/debounce-and-throttle-visual-explanation</li>
1852
+ * <li>http://unscriptable.com/2009/03/20/debouncing-javascript-methods/</li>
1853
+ * <li>http://www.illyriad.co.uk/blog/index.php/2011/09/javascript-dont-spam-your-server-debounce-and-throttle/</li>
1854
+ * </ul>
1855
+ *
1856
+ * @param timeout
1857
+ * The time each value has to be 'the most recent' of the {@link Observable} to ensure that it's not dropped.
1858
+ * @param unit
1859
+ * The unit of time for the specified timeout.
1860
+ * @param scheduler
1861
+ * The {@link Scheduler} to use internally to manage the timers which handle timeout for each event.
1862
+ * @return Observable which performs the throttle operation.
1863
+ * @see {@link #throttleWithTimeout};
1864
+ */
1865
+ public Observable <T > debounce (long timeout , TimeUnit unit , Scheduler scheduler ) {
1866
+ return create (OperationDebounce .debounce (this , timeout , unit ));
1867
+ }
1868
+
1869
+ /**
1870
+ * Debounces by dropping all values that are followed by newer values before the timeout value expires. The timer resets on each `onNext` call.
1871
+ * <p>
1872
+ * NOTE: If events keep firing faster than the timeout then no data will be emitted.
1873
+ * <p>
1874
+ * <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/throttleWithTimeout.png">
1875
+ * <p>
1876
+ * Information on debounce vs throttle:
1877
+ * <p>
1878
+ * <ul>
1879
+ * <li>http://drupalmotion.com/article/debounce-and-throttle-visual-explanation</li>
1880
+ * <li>http://unscriptable.com/2009/03/20/debouncing-javascript-methods/</li>
1881
+ * <li>http://www.illyriad.co.uk/blog/index.php/2011/09/javascript-dont-spam-your-server-debounce-and-throttle/</li>
1882
+ * </ul>
1883
+ *
1884
+ * @param timeout
1885
+ * The time each value has to be 'the most recent' of the {@link Observable} to ensure that it's not dropped.
1886
+ * @param unit
1887
+ * The {@link TimeUnit} for the timeout.
1888
+ *
1889
+ * @return An {@link Observable} which filters out values which are too quickly followed up with newer values.
1890
+ * @see {@link #debounce}
1891
+ */
1892
+ public Observable <T > throttleWithTimeout (long timeout , TimeUnit unit ) {
1893
+ return create (OperationDebounce .debounce (this , timeout , unit ));
1894
+ }
1895
+
1896
+ /**
1897
+ * Debounces by dropping all values that are followed by newer values before the timeout value expires. The timer resets on each `onNext` call.
1898
+ * <p>
1899
+ * NOTE: If events keep firing faster than the timeout then no data will be emitted.
1900
+ * <p>
1901
+ * <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/throttleWithTimeout.png">
1902
+ *
1903
+ * @param timeout
1904
+ * The time each value has to be 'the most recent' of the {@link Observable} to ensure that it's not dropped.
1905
+ * @param unit
1906
+ * The unit of time for the specified timeout.
1907
+ * @param scheduler
1908
+ * The {@link Scheduler} to use internally to manage the timers which handle timeout for each event.
1909
+ * @return Observable which performs the throttle operation.
1910
+ * @see {@link #debounce}
1911
+ */
1912
+ public Observable <T > throttleWithTimeout (long timeout , TimeUnit unit , Scheduler scheduler ) {
1913
+ return create (OperationDebounce .debounce (this , timeout , unit , scheduler ));
1914
+ }
1915
+
1916
+ /**
1917
+ * Throttles by skipping value until `skipDuration` passes and then emits the next received value.
1918
+ * <p>
1919
+ * This differs from {@link #throttleLast} in that this only tracks passage of time whereas {@link #throttleLast} ticks at scheduled intervals.
1920
+ * <p>
1921
+ * <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/throttleFirst.png">
1922
+ *
1923
+ * @param skipDuration
1924
+ * Time to wait before sending another value after emitting last value.
1925
+ * @param unit
1926
+ * The unit of time for the specified timeout.
1927
+ * @param scheduler
1928
+ * The {@link Scheduler} to use internally to manage the timers which handle timeout for each event.
1929
+ * @return Observable which performs the throttle operation.
1930
+ */
1931
+ public Observable <T > throttleFirst (long windowDuration , TimeUnit unit ) {
1932
+ return create (OperationThrottleFirst .throttleFirst (this , windowDuration , unit ));
1933
+ }
1934
+
1935
+ /**
1936
+ * Throttles by skipping value until `skipDuration` passes and then emits the next received value.
1937
+ * <p>
1938
+ * This differs from {@link #throttleLast} in that this only tracks passage of time whereas {@link #throttleLast} ticks at scheduled intervals.
1939
+ * <p>
1940
+ * <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/throttleFirst.png">
1941
+ *
1942
+ * @param skipDuration
1943
+ * Time to wait before sending another value after emitting last value.
1944
+ * @param unit
1945
+ * The unit of time for the specified timeout.
1946
+ * @param scheduler
1947
+ * The {@link Scheduler} to use internally to manage the timers which handle timeout for each event.
1948
+ * @return Observable which performs the throttle operation.
1949
+ */
1950
+ public Observable <T > throttleFirst (long skipDuration , TimeUnit unit , Scheduler scheduler ) {
1951
+ return create (OperationThrottleFirst .throttleFirst (this , skipDuration , unit , scheduler ));
1952
+ }
1953
+
1954
+ /**
1955
+ * Throttles by returning the last value of each interval defined by 'intervalDuration'.
1956
+ * <p>
1957
+ * This differs from {@link #throttleFirst} in that this ticks along at a scheduled interval whereas {@link #throttleFirst} does not tick, it just tracks passage of time.
1958
+ * <p>
1959
+ * <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/throttleLast.png">
1960
+ *
1961
+ * @param intervalDuration
1962
+ * Duration of windows within with the last value will be chosen.
1963
+ * @param unit
1964
+ * The unit of time for the specified interval.
1965
+ * @return Observable which performs the throttle operation.
1966
+ * @see {@link #sample(long, TimeUnit)}
1967
+ */
1968
+ public Observable <T > throttleLast (long intervalDuration , TimeUnit unit ) {
1969
+ return sample (intervalDuration , unit );
1970
+ }
1971
+
1972
+ /**
1973
+ * Throttles by returning the last value of each interval defined by 'intervalDuration'.
1974
+ * <p>
1975
+ * This differs from {@link #throttleFirst} in that this ticks along at a scheduled interval whereas {@link #throttleFirst} does not tick, it just tracks passage of time.
1976
+ * <p>
1977
+ * <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/throttleLast.png">
1978
+ *
1979
+ * @param intervalDuration
1980
+ * Duration of windows within with the last value will be chosen.
1981
+ * @param unit
1982
+ * The unit of time for the specified interval.
1983
+ * @return Observable which performs the throttle operation.
1984
+ * @see {@link #sample(long, TimeUnit, Scheduler)}
1985
+ */
1986
+ public Observable <T > throttleLast (long intervalDuration , TimeUnit unit , Scheduler scheduler ) {
1987
+ return sample (intervalDuration , unit , scheduler );
1988
+ }
1989
+
1813
1990
/**
1814
1991
* Wraps each item emitted by a source Observable in a {@link Timestamped} object.
1815
1992
* <p>
@@ -3025,7 +3202,7 @@ public Observable<T> onErrorReturn(Func1<Throwable, ? extends T> resumeFunction)
3025
3202
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229154(v%3Dvs.103).aspx">MSDN: Observable.Aggregate</a>
3026
3203
* @see <a href="http://en.wikipedia.org/wiki/Fold_(higher-order_function)">Wikipedia: Fold (higher-order function)</a>
3027
3204
*/
3028
- public Observable <T > reduce (Func2 <? super T , ? super T , ? extends T > accumulator ) {
3205
+ public Observable <T > reduce (Func2 <T , T , T > accumulator ) {
3029
3206
return create (OperationScan .scan (this , accumulator )).takeLast (1 );
3030
3207
}
3031
3208
@@ -3206,7 +3383,7 @@ public ConnectableObservable<T> publish() {
3206
3383
*
3207
3384
* @see #reduce(Func2)
3208
3385
*/
3209
- public Observable <T > aggregate (Func2 <? super T , ? super T , ? extends T > accumulator ) {
3386
+ public Observable <T > aggregate (Func2 <T , T , T > accumulator ) {
3210
3387
return reduce (accumulator );
3211
3388
}
3212
3389
@@ -3233,7 +3410,7 @@ public Observable<T> aggregate(Func2<? super T, ? super T, ? extends T> accumula
3233
3410
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229154(v%3Dvs.103).aspx">MSDN: Observable.Aggregate</a>
3234
3411
* @see <a href="http://en.wikipedia.org/wiki/Fold_(higher-order_function)">Wikipedia: Fold (higher-order function)</a>
3235
3412
*/
3236
- public <R > Observable <R > reduce (R initialValue , Func2 <? super R , ? super T , ? extends R > accumulator ) {
3413
+ public <R > Observable <R > reduce (R initialValue , Func2 <R , ? super T , R > accumulator ) {
3237
3414
return create (OperationScan .scan (this , initialValue , accumulator )).takeLast (1 );
3238
3415
}
3239
3416
@@ -3244,7 +3421,7 @@ public <R> Observable<R> reduce(R initialValue, Func2<? super R, ? super T, ? ex
3244
3421
*
3245
3422
* @see #reduce(Object, Func2)
3246
3423
*/
3247
- public <R > Observable <R > aggregate (R initialValue , Func2 <? super R , ? super T , ? extends R > accumulator ) {
3424
+ public <R > Observable <R > aggregate (R initialValue , Func2 <R , ? super T , R > accumulator ) {
3248
3425
return reduce (initialValue , accumulator );
3249
3426
}
3250
3427
@@ -3267,7 +3444,7 @@ public <R> Observable<R> aggregate(R initialValue, Func2<? super R, ? super T, ?
3267
3444
* @return an Observable that emits the results of each call to the accumulator function
3268
3445
* @see <a href="http://msdn.microsoft.com/en-us/library/hh211665(v%3Dvs.103).aspx">MSDN: Observable.Scan</a>
3269
3446
*/
3270
- public Observable <T > scan (Func2 <? super T , ? super T , ? extends T > accumulator ) {
3447
+ public Observable <T > scan (Func2 <T , T , T > accumulator ) {
3271
3448
return create (OperationScan .scan (this , accumulator ));
3272
3449
}
3273
3450
@@ -3328,7 +3505,7 @@ public Observable<T> sample(long period, TimeUnit unit, Scheduler scheduler) {
3328
3505
* @return an Observable that emits the results of each call to the accumulator function
3329
3506
* @see <a href="http://msdn.microsoft.com/en-us/library/hh211665(v%3Dvs.103).aspx">MSDN: Observable.Scan</a>
3330
3507
*/
3331
- public <R > Observable <R > scan (R initialValue , Func2 <? super R , ? super T , ? extends R > accumulator ) {
3508
+ public <R > Observable <R > scan (R initialValue , Func2 <R , ? super T , R > accumulator ) {
3332
3509
return create (OperationScan .scan (this , initialValue , accumulator ));
3333
3510
}
3334
3511
0 commit comments