Created
February 13, 2017 01:16
-
-
Save mmynsted/973eaae009941521afe3be7b6314bb09 to your computer and use it in GitHub Desktop.
Simple example of getting an item on an interval, filtering, taking, and printing...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rxExample; | |
import io.reactivex.Observable; | |
import io.reactivex.schedulers.TestScheduler; | |
import java.util.Collections; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
import static java.util.Arrays.asList; | |
/** | |
* Created by mmynsted on 2/8/17. | |
*/ | |
public class ShowItem { | |
public void printGoodItem() { | |
TestScheduler scheduler = new TestScheduler(); //control Observable's timing | |
//Some test data | |
List<List<Integer>> testSource = asList( | |
Collections.emptyList(), | |
asList(1), | |
asList(1, 2), | |
asList( 1, 2, 3)); | |
Iterator<List<Integer>> iter = testSource.iterator(); | |
Observable.interval(1, TimeUnit.SECONDS, scheduler) //1 sec interval with TestScheduler | |
.map(y -> getTestItem(iter)) //map the int from iterval to a test item | |
.filter(y -> y.size() > 1) //filter out any items where size < 1 | |
.take(1) //take only the first item | |
.subscribe(System.out::println); //print the item | |
scheduler.advanceTimeBy(5, TimeUnit.SECONDS); | |
} | |
private List<Integer> getTestItem(Iterator<List<Integer>> iter) { | |
List<Integer> result = Collections.emptyList(); | |
if (iter != null) { | |
if (iter.hasNext()) { | |
result = iter.next(); | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment