Skip to content

Instantly share code, notes, and snippets.

@mgandin
Last active August 29, 2015 14:07
Show Gist options
  • Save mgandin/2c4e5c4923628c29f73f to your computer and use it in GitHub Desktop.
Save mgandin/2c4e5c4923628c29f73f to your computer and use it in GitHub Desktop.
Hello RXJava World
package fr.mga.mashup;
import rx.Observable;
public class Hello {
public String msg(String ... msg) {
HelloFunction function = new HelloFunction();
HelloReduce reduce = new HelloReduce();
return Observable.from(msg).map(function).reduce(reduce).toBlocking().first();
}
public String msgPair(String ... msg) {
HelloFunction function = new HelloFunction();
HelloReduce reduce = new HelloReduce();
return Observable.from(msg).filter(new HelloFilter("b")).map(function).reduce(reduce).toBlocking().first();
}
}
package fr.mga.mashup;
import rx.functions.Func1;
public class HelloFilter implements Func1<String, Boolean> {
private String endWith;
public HelloFilter(String endWith) {
this.endWith = endWith;
}
@Override public Boolean call(String s) {
return s.endsWith(endWith);
}
}
package fr.mga.mashup;
import rx.functions.Func1;
public class HelloFunction implements Func1<String,String> {
@Override public String call(String s) {
return "hello " + s +" !\n";
}
}
package fr.mga.mashup;
import rx.functions.Func2;
public class HelloReduce implements Func2<String,String,String> {
@Override public String call(String s, String s2) {
return s + s2;
}
}
package fr.mga.mashup;
import org.fest.assertions.api.Assertions;
import org.junit.Test;
public class HelloTest {
@Test
public void should_aggregate_some_hello() {
Hello hello = new Hello();
String helloAggregate = hello.msg("bob","john","troy","rob");
Assertions.assertThat(helloAggregate).isEqualTo("hello bob !\nhello john !\nhello troy !\n"
+ "hello rob !\n");
}
@Test
public void should_aggregate_only_hello_that_end_by_b() {
Hello hello = new Hello();
String helloAggregate = hello.msgPair("bob","john","troy","rob");
Assertions.assertThat(helloAggregate).isEqualTo("hello bob !\nhello rob !\n");
}
}
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.mga.mashup</groupId>
<artifactId>mashup</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<junit.version>4.11</junit.version>
<festassert.version>2.0M10</festassert.version>
<rxjava.version>1.0.0-rc.3</rxjava.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<source.level>1.7</source.level>
<target.level>1.7</target.level>
</properties>
<dependencies>
<dependency>
<groupId>io.reactivex</groupId>
<artifactId>rxjava</artifactId>
<version>${rxjava.version}</version>
</dependency>
<!-- Unit test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert-core</artifactId>
<version>${festassert.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${source.level}</source>
<target>${target.level}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment