initial commit

This commit is contained in:
Sander Hautvast 2016-10-11 20:24:38 +02:00
commit caccd8a3c5
80 changed files with 2649 additions and 0 deletions

32
.idea/compiler.xml generated Normal file
View file

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
<entry name="!?*.aj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
<profile default="false" name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<processorPath useClasspath="true" />
<module name="JavaRx2" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="JavaRx2" target="1.8" />
</bytecodeTargetLevel>
</component>
</project>

3
.idea/copyright/profiles_settings.xml generated Normal file
View file

@ -0,0 +1,3 @@
<component name="CopyrightManager">
<settings default="" />
</component>

View file

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: io.reactivex.rxjava2:rxjava:2.0.0-RC4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/io/reactivex/rxjava2/rxjava/2.0.0-RC4/rxjava-2.0.0-RC4.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/io/reactivex/rxjava2/rxjava/2.0.0-RC4/rxjava-2.0.0-RC4-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/io/reactivex/rxjava2/rxjava/2.0.0-RC4/rxjava-2.0.0-RC4-sources.jar!/" />
</SOURCES>
</library>
</component>

View file

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: org.reactivestreams:reactive-streams:1.0.0">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/reactivestreams/reactive-streams/1.0.0/reactive-streams-1.0.0.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/reactivestreams/reactive-streams/1.0.0/reactive-streams-1.0.0-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/reactivestreams/reactive-streams/1.0.0/reactive-streams-1.0.0-sources.jar!/" />
</SOURCES>
</library>
</component>

42
.idea/misc.xml generated Normal file
View file

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
<OptionsSetting value="true" id="Checkout" />
<OptionsSetting value="true" id="Update" />
<OptionsSetting value="true" id="Status" />
<OptionsSetting value="true" id="Edit" />
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="masterDetails">
<states>
<state key="ProjectJDKs.UI">
<settings>
<last-edited>1.8</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>

8
.idea/modules.xml generated Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/JavaRx2.iml" filepath="$PROJECT_DIR$/JavaRx2.iml" />
</modules>
</component>
</project>

124
.idea/uiDesigner.xml generated Normal file
View file

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

1643
.idea/workspace.xml generated Normal file

File diff suppressed because it is too large Load diff

16
JavaRx2.iml Normal file
View file

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: io.reactivex.rxjava2:rxjava:2.0.0-RC4" level="project" />
<orderEntry type="library" name="Maven: org.reactivestreams:reactive-streams:1.0.0" level="project" />
</component>
</module>

1
README.md Normal file
View file

@ -0,0 +1 @@
# rxjava2examples

39
pom.xml Normal file
View file

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nl.kadaster.exp</groupId>
<artifactId>exp</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>2.0.0-RC4</version>
</dependency>
<dependency>
<groupId>org.reactivestreams</groupId>
<artifactId>reactive-streams</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

17
rx.iml Normal file
View file

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: io.reactivex.rxjava2:rxjava:2.0.0-RC2" level="project" />
<orderEntry type="library" name="Maven: org.reactivestreams:reactive-streams:1.0.0" level="project" />
</component>
</module>

View file

@ -0,0 +1,15 @@
package rx.combine;
import io.reactivex.Observable;
public class Zip {
public static void main(String[] args) {
Observable<String> strings = Observable.fromArray(new String[]{"a", "b", "c"});
Observable<Integer> integers = Observable.fromArray(new Integer[]{1, 2, 3});
strings.zipWith(integers, (string, integer) -> {
return string + integer;
}).subscribe(value -> System.out.println(value));
}
}

View file

@ -0,0 +1,16 @@
package rx.conditional;
import io.reactivex.Observable;
import static java.lang.System.out;
public class All {
public static void main(String[] args) {
Observable.range(1, 5).all(v -> {
return v % 2 == 0;
}).subscribe(b -> out.println(b));
}
}

View file

@ -0,0 +1,22 @@
package rx.conditional;
import io.reactivex.Observable;
import static io.reactivex.Observable.amb;
import static io.reactivex.Observable.timer;
import static java.lang.System.out;
import static java.util.Arrays.asList;
import static java.util.concurrent.TimeUnit.SECONDS;
public class Amb {
public static void main(String[] args) throws InterruptedException {
Observable<Integer> timer1 = timer(1, SECONDS).map(t->{return 1;});
Observable<Integer> timer2 = timer(2, SECONDS).map(t->{return 2;});
Observable<Integer> timer3 = timer(3, SECONDS).map(t->{return 3;});
amb(asList(timer1,timer2,timer3)).subscribe(v-> out.println(v));
SECONDS.sleep(5);
}
}

View file

@ -0,0 +1,14 @@
package rx.conditional;
import io.reactivex.Observable;
import static java.lang.System.out;
public class Contains {
public static void main(String[] args) {
Observable.range(1, 5).contains(1).subscribe(b -> out.println(b));
}
}

View file

@ -0,0 +1,21 @@
package rx.conditional;
import io.reactivex.Observable;
import static java.lang.System.out;
import static java.util.concurrent.TimeUnit.SECONDS;
public class SkipUntil {
public static void main(String[] args) throws InterruptedException {
out.println("start");
Observable<Long> range = Observable.intervalRange(0, 10, 0, 1, SECONDS);
Observable<Long> timer = Observable.timer(5, SECONDS);
// range.subscribe(v -> out.println(v));
range.skipUntil(timer).subscribe(v -> out.println(v));
SECONDS.sleep(10);
}
}

View file

@ -0,0 +1,22 @@
package rx.create;
import io.reactivex.Observable;
import java.util.Arrays;
import java.util.stream.Stream;
/*
* create an Observable from any
*/
public class Create {
public static void main(String[] args) {
Observable<Long> observable = Observable.create(emitter -> {
emitter.onNext(System.nanoTime());
emitter.onNext(System.nanoTime());
});
observable.subscribe(l -> System.out.println(l));
System.out.println("java 8:");
Stream.of(new Long[]{System.nanoTime(), System.nanoTime()}).forEach(t -> System.out.println(t));
}
}

View file

@ -0,0 +1,28 @@
package rx.create;
import io.reactivex.Observable;
/*
* defers Observable creation until a subscriber is found
*/
public class Defer {
private static String[] animals;
public static void main(String[] args) {
Observable<String> deferredAnimals = Observable.defer(() -> {
return Observable.fromArray(animals); //Observable is created when subscribe is called
});
animals = new String[]{"bear", "lion", "dog", "parrot"};
deferredAnimals.subscribe(animal -> System.out.println(animal));
}
public static void wontWork(){
Observable<String> deferredAnimals = Observable.fromArray(animals); //Observable is created when animals is uninitialized
animals = new String[]{"bear", "lion", "dog", "parrot"};
deferredAnimals.subscribe(animal -> System.out.println(animal));
}
}

View file

@ -0,0 +1,17 @@
package rx.create;
import io.reactivex.Observable;
import java.util.Arrays;
public class From {
public static void main(String[] args) {
String[] animals = {"bear", "lion", "dog", "parrot"};
Observable.fromArray(animals).subscribe(animal -> System.out.println(animal));
System.out.println("java 8:");
Arrays.asList(animals).stream().forEach(animal -> System.out.println(animal));
}
}

View file

@ -0,0 +1,14 @@
package rx.create;
import io.reactivex.Observable;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
public class Interval {
public static void main(String[] args) throws InterruptedException {
Observable.interval(0, 100, MILLISECONDS).subscribe(value -> System.out.println(System.currentTimeMillis() + ": " + value));
SECONDS.sleep(1);
}
}

View file

@ -0,0 +1,21 @@
package rx.create;
import io.reactivex.Observable;
import java.util.Arrays;
import java.util.stream.Stream;
/*
* creates an Observable from any
*/
public class Just {
public static void main(String[] args) {
Observable.just("hello").subscribe(v -> System.out.println(v));
System.out.println();
System.out.println("java 8:");
Stream.of(Arrays.asList("hello", " ", "world")).forEach(t -> System.out.println(t));
}
}

View file

@ -0,0 +1,12 @@
package rx.create;
import io.reactivex.Observable;
/*
* Does nothing
*/
public class Never {
public static void main(String[] args) {
Observable.never().subscribe(n -> System.out.println(n));
}
}

View file

@ -0,0 +1,24 @@
package rx.create;
import io.reactivex.Observable;
import java.util.stream.IntStream;
/*
* creates a range
*/
public class Range {
public static void main(String[] args) throws InterruptedException {
Observable.range(1, 10).subscribe(v -> System.out.println(v));
System.out.println("\njava 8:");
IntStream.rangeClosed(1, 10).forEach(v -> System.out.println(v));
System.out.println("\nfactorial(5) using reduce:");
System.out.println(IntStream.rangeClosed(1, 5).reduce(1, (x, f) -> {
return x * f;
}));
}
}

View file

@ -0,0 +1,19 @@
package rx.create;
import io.reactivex.Observable;
/*
* throws an exception...
*/
public class Throw {
public static void main(String[] args) {
try {
Observable.error(new Exception("something went wrong")).subscribe(e -> System.out.println("oops: " + e));
} catch (Throwable e) {
System.out.println("hold your horses, we're catching an exception:");
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,43 @@
package rx.create;
import io.reactivex.Observable;
/*
* throws an exception...
*
* seems simple enough, but wait, where's the catch?
*/
public class ThrowExplained {
public static void main(String[] args) {
error();
try {
the_trick();
} catch (Throwable t) {
System.out.println("can I catch it?");
}
}
public static void error() {
try {
Observable.error(new Exception("something went wrong")).subscribe(e -> System.out.println("oops: " + e));
} catch (Throwable e) {
System.out.println("hold your horses, we're catching an exception:");
e.printStackTrace();
}
}
public static void the_trick() {
try {
Thread currentThread = Thread.currentThread();
Thread.UncaughtExceptionHandler handler = currentThread.getUncaughtExceptionHandler();
handler.uncaughtException(currentThread, new Exception("looks like something went wrong here too"));
} catch (Throwable e) {
System.out.println("trying to catch this exception");
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,19 @@
package rx.create;
import io.reactivex.Observable;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.out;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
public class Timer {
public static void main(String[] args) throws InterruptedException {
out.println(currentTimeMillis());
Observable.timer(1000, MILLISECONDS).map(t -> {
return "peekaboo";
}).subscribe(v -> out.println(currentTimeMillis()+" "+v));
MILLISECONDS.sleep(1200);
}
}

View file

@ -0,0 +1,38 @@
package rx.filter;
import io.reactivex.Observable;
import java.util.Random;
import static io.reactivex.Observable.create;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.out;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
public class Debounce {
public static void main(String[] args) throws InterruptedException {
Observable<Long> keys = create(observer -> {
Random random = new Random();
for (int i = 0; i < 50; i++) {
long t = currentTimeMillis();
int w = random.nextInt(500);
out.println("time " + t + ", wait " + w);
observer.onNext(t);
sleep(w);
}
});
keys.debounce(400, MILLISECONDS).subscribe(key -> out.println("event " + key));
}
public static void sleep(int w) {
try {
Thread.sleep(w);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,28 @@
package rx.filter;
import io.reactivex.Observable;
import java.util.Random;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.out;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static rx.filter.Debounce.sleep;
public class Debounce2 {
public static void main(String[] args) {
Random random = new Random();
Observable<Integer> range = Observable.range(1, 50);
Observable<Long> times = range.map(i -> {
long t = currentTimeMillis();
int w = random.nextInt(500);
out.println("time " + t + ", wait " + w);
sleep(w);
return t;
});
times.debounce(400, MILLISECONDS).subscribe(key -> out.println("event " + key));
}
}

View file

@ -0,0 +1,29 @@
package rx.filter;
import io.reactivex.Observable;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.out;
import static rx.filter.Debounce.sleep;
public class Debounce3 {
public static void main(String[] args) {
Random random = new Random();
Observable<Integer> waittimes = Observable.range(1, 50).map(r -> {
return random.nextInt(500);
});
Observable<Long> timestamps = waittimes.map(waittime -> {
long t = currentTimeMillis();
out.println("timestamp " + t + ", wait " + waittime);
sleep(waittime);
return t;
});
timestamps.debounce(400, TimeUnit.MILLISECONDS).subscribe(key -> out.println("event " + key));
}
}

View file

@ -0,0 +1,31 @@
package rx.filter;
import io.reactivex.Observable;
import java.util.Random;
import static io.reactivex.Observable.range;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.out;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static rx.filter.Debounce.sleep;
public class Debounce4 {
public static void main(String[] args) {
Random random = new Random();
range(1, 50)
.map(r -> {
return random.nextInt(500);
})
.map(waittime -> {
long t = currentTimeMillis();
out.println("timestamp " + t + ", wait " + waittime);
sleep(waittime);
return t;
})
.debounce(400, MILLISECONDS)
.subscribe(key -> out.println("event " + key));
}
}

View file

@ -0,0 +1,20 @@
package rx.filter;
import java.util.Random;
import static io.reactivex.Observable.interval;
import static java.lang.System.out;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
public class Distinct {
public static void main(String[] args) throws InterruptedException {
Random random = new Random();
interval(0, 100, MILLISECONDS).map(t -> {
return random.nextInt(20);
}).distinct().subscribe(w -> out.println(w));
SECONDS.sleep(10);
}
}

View file

@ -0,0 +1,17 @@
package rx.filter;
import io.reactivex.Observable;
import static io.reactivex.Observable.range;
import static java.lang.System.out;
/*
* Emits only the first x elements of an observable.
*/
public class Take {
public static void main(String[] args) {
range(1, 10).take(2).subscribe(w -> out.println(w));
}
}

View file

@ -0,0 +1,44 @@
package rx.transform;
import io.reactivex.Observable;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
/*
* Buffers until a criterium is met and the emits all the buffered values in one emission.
*/
public class Buffer {
public static void main(String[] args) {
Frame frame = getFrame();
JTextField textfield = new JTextField();
frame.add(textfield);
frame.pack();
frame.setVisible(true);
final Observable<String> keys = Observable.create(observableEmitter -> {
textfield.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent keyEvent) {
observableEmitter.onNext(new String(new char[]{keyEvent.getKeyChar()}));
}
});
});
keys.buffer(3).subscribe(key -> System.out.println("event " + key));
}
private static Frame getFrame() {
Frame f = new JFrame("");
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
return f;
}
}

View file

@ -0,0 +1,19 @@
package rx.transform;
import io.reactivex.Observable;
/*
* flattens an observable of observables
*/
public class FlatMap {
public static void main(String[] args) {
Observable<String> observable = Observable.range(1, 4).flatMap(r -> {
return Observable.range(1, r).map(i -> {
return "" + i;
});
});
observable.toList().subscribe(s -> System.out.println(s));
}
}

View file

@ -0,0 +1,22 @@
package rx.transform;
import io.reactivex.Observable;
import static java.lang.System.out;
import static java.util.concurrent.TimeUnit.SECONDS;
/*
* splits an observable in new observables
*/
public class GroupBy {
public static void main(String[] args) throws InterruptedException {
Observable.range(0, 30)
.groupBy(v -> v % 3)
.subscribe(v ->
v.toList().subscribe(w -> out.println(w))
);
}
}

View file

@ -0,0 +1,14 @@
package rx.transform;
import io.reactivex.Observable;
/*
* Applies a mapping function to an observable returning a new Observable
*/
public class Map {
public static void main(String[] args) {
Observable.range(1, 10).map(value -> {
return value + " is " + (value % 2 == 0 ? "even" : "uneven");
}).subscribe(output -> System.out.println(output));
}
}

View file

@ -0,0 +1,15 @@
package rx.transform;
import io.reactivex.Observable;
/*
* Applies a mapping function 'with state', ie the previous output.
* The output reenters together with the new emission from the original observable.
*/
public class Scan {
public static void main(String[] args) {
Observable.range(1, 10).scan((sum, item) -> {
return sum + item;
}).subscribe(value -> System.out.println(value));
}
}

View file

@ -0,0 +1,17 @@
package rx.transform;
import io.reactivex.Observable;
/*
* Splits an observable in multiple, using some criterium
*/
public class Window1 {
static int counter = 1;
public static void main(String[] args) {
Observable.range(0, 100).window(10).forEach(window -> {
System.out.println("\n" + counter++);
window.forEach(value -> System.out.print(value + " "));
});
}
}

View file

@ -0,0 +1,21 @@
package rx.transform;
import io.reactivex.Observable;
import java.util.concurrent.TimeUnit;
/*
* Emits a new window based on elapsed time
*/
public class Window2 {
public static void main(String[] args) throws InterruptedException {
Observable.interval(0, 500, TimeUnit.MILLISECONDS)
.window(5, TimeUnit.SECONDS)
.subscribe(observable -> {
System.out.println("new window");
observable.subscribe(value -> System.out.println(value));
});
TimeUnit.SECONDS.sleep(60);
}
}

View file

@ -0,0 +1,58 @@
package rx.transform;
import io.reactivex.Observable;
import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Buffers observed values until a criterium is met and the emits the buffered values as a new observable.
*/
public class Window3 {
static AtomicBoolean newWindow = new AtomicBoolean(false);
public static void main(String[] args) {
JFrame frame = new JFrame("app");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JTextField textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
newWindow.set(true);
}
});
frame.getContentPane().add(textField);
frame.pack();
frame.setVisible(true);
Observable.interval(1, TimeUnit.SECONDS).window(() -> {
return Observable.fromCallable(new Callable<Observable<String>>() {
@Override
public Observable<String> call() throws Exception {
return Observable.create(emitter -> {
if (newWindow.get()) {
newWindow.set(false);
emitter.onNext("");
}
});
}
});
}).subscribe(observable -> {
System.out.println("interrupt");
observable.subscribe(value -> System.out.println(value));
});
}
}

View file

@ -0,0 +1,18 @@
package rx.utility;
import io.reactivex.Observable;
import static java.lang.System.out;
import static java.util.concurrent.TimeUnit.SECONDS;
public class Delay {
public static void main(String[] args) throws InterruptedException {
out.println("start");
Observable<Integer> rangeFrom1To5 = Observable.range(1,5);
rangeFrom1To5.delay(3, SECONDS).subscribe(v -> out.println("receive "+v));
SECONDS.sleep(5);
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.