- 
                Notifications
    You must be signed in to change notification settings 
- Fork 137
Getting started with a simple example
Starting with a collection of words we will count the letters of each 'uncorrupted' word. And let's assume all the corrupted words, very handily, start with '$'.
List<String> list = Arrays.asList("hello","world","$da^","along","$orrupted");
new SimpleReact().reactToCollection(list)
(assume they all start with $)
new SimpleReact().reactToCollection(list)
.filter(it -> !it.startsWith("$"))
The then method (inspired by the promises spec then method) behaves a bit like the map method in the JDK Streams api. Converting an input into an output. For those familar with the Javascript promises spec - we've broken out then into multiple parts - failure handling is managed by the onFail and capture methods.
new SimpleReact().reactToCollection(list)
    .filter(it -> !it.startsWith("$"))
    .then(it -> it.length())
At this point we would get back a collection of [5,5,5] if we called block. The block() method, blocks the current thread until the reactive flow has completed - returning all results (look for overloaded block methods to return only a selection of results).
We can call block and use the JDK 8 Streams api to sum them.
int count  = new SimpleReact()
	.reactToCollection(list)
	.filter(it -> !it.startsWith("$"))
	.then(it -> it.length())
	.block().stream().reduce(0, (acc,next) -> acc+next );
assertThat(count, is(15));
 int count  = new SimpleReact()
	.reactToCollection(list)
	.filter(it -> !it.startsWith("$"))
	.peek(it -> System.out.println(it))  // hello,world,along
	.then(it -> it.length())
	.peek(it -> System.out.println(it))  //5,5,5 [e.g. hello.length()==5] 
	.block().stream().reduce(0, (acc,next) -> acc+next );
List list = Arrays.asList("hello","world","$da^","along","$orrupted",null);
	List<String> list = Arrays.asList("hello","world","$da^","along",null);
	int count  = new SimpleReact()
			.reactToCollection(list)
			.capture(e -> e.printStackTrace())
			.filter(it -> !it.startsWith("$"))
			.then(it -> it.length())
			.block().stream().reduce(0, (acc,next) -> acc+next );
	assertThat(count, is(15));
Note a StrackTrace is output when this code is run.
	List<String> list = Arrays.asList("hello","world","$da^","along","$orrupted",null);
	int count  = new SimpleReact()
			.reactToCollection(list)
			.capture(e -> e.printStackTrace())
			.filter(it -> !it.startsWith("$"))
		
			.onFail(e -> { if(e.getCause() instanceof NullPointerException) { return "null"; } return "";})
			.then(it -> it.length())
			.block().stream().reduce(0, (acc,next) -> acc+next );
	assertThat(count, is(19));
Note that no StackTrace is output when this code is run, as the exception has been handled.
oops - my bad