Last active
September 22, 2016 13:04
-
-
Save nateridderman/701514e0c44a52e7f157bc80b10bf3aa to your computer and use it in GitHub Desktop.
RxJava samples
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 com.gdg.unittesting.rxjava; | |
import org.junit.Test; | |
import java.util.Collections; | |
import java.util.concurrent.TimeUnit; | |
import rx.Observable; | |
import rx.functions.Action1; | |
import rx.observables.StringObservable; | |
public class RxJavaTest { | |
String hipsterIpsum = "Pop-up butcher tumblr trust fund, celiac crucifix franzen you probably haven't heard of them 3 wolf moon mumblecore flexitarian blue bottle fashion axe taxidermy banjo. Literally chillwave austin lumbersexual. Sriracha authentic raw denim, ethical shabby chic squid selvage etsy cray meditation scenester. Quinoa retro portland kinfolk raclette chia vinyl church-key freegan, lyft gastropub mixtape tumblr gluten-free venmo."; | |
@Test | |
public void hello() { | |
String[] names = {"John", "Cougar", "Mellencamp"}; | |
Observable.from(names).subscribe(new Action1<String>() { | |
@Override | |
public void call(String s) { | |
System.out.println("Hello " + s + "!"); | |
} | |
}); | |
} | |
@Test | |
public void helloRetrolambda() { | |
String[] names = {"John", "Cougar", "Mellencamp"}; | |
Observable.from(names).subscribe(s -> { | |
System.out.println("Hello " + s + "!"); | |
}); | |
} | |
@Test | |
public void helloChars() { | |
Observable<String> ipsumObservable = Observable.just(hipsterIpsum); | |
StringObservable.split(ipsumObservable, " ") | |
.filter (s -> { | |
if (Character.isUpperCase(s.charAt(0))) | |
return true; | |
else | |
return false; | |
}) | |
.subscribe(s -> { | |
System.out.println(s); | |
}); | |
} | |
@Test | |
public void delaySortBroken() { | |
Observable<String> ipsumObservable = Observable.just(hipsterIpsum); | |
StringObservable.split(ipsumObservable, " ") | |
.flatMap(s -> | |
Observable.just(s).delay(5*s.length(), TimeUnit.MILLISECONDS) | |
) | |
.subscribe(s -> { | |
System.out.println(s); | |
}); | |
} | |
@Test | |
public void delaySortBroken2() { | |
Observable<String> ipsumObservable = Observable.just(hipsterIpsum); | |
StringObservable.split(ipsumObservable, " ") | |
.flatMap(s -> Observable.just(s).delay(5*s.length(), TimeUnit.MICROSECONDS)) | |
.subscribe(s -> { | |
System.out.println(s); | |
}); | |
} | |
@Test | |
public void delaySortWorking() { | |
Observable<String> ipsumObservable = Observable.just(hipsterIpsum); | |
StringObservable.split(ipsumObservable, " ") | |
.flatMap(s -> Observable.just(s).delay(5*s.length(), TimeUnit.MILLISECONDS)) | |
.toBlocking() | |
.subscribe(s -> { | |
System.out.println(s); | |
}); | |
} | |
@Test | |
public void oneWordPerLength() { | |
Observable<String> ipsumObservable = Observable.just(hipsterIpsum); | |
StringObservable.split(ipsumObservable, " ") | |
.flatMap(s -> { | |
Tuple tuple = new Tuple(s, s.length()); | |
return Observable.just(tuple); | |
}) | |
.distinct() | |
.subscribe(s -> { | |
System.out.println(s.word); | |
}); | |
} | |
@Test | |
public void oneWordPerLengthSorted() { | |
Observable<String> ipsumObservable = Observable.just(hipsterIpsum); | |
StringObservable.split(ipsumObservable, " ") | |
.flatMap(s -> { | |
Tuple tuple = new Tuple(s, s.length()); | |
return Observable.just(tuple); | |
}) | |
.distinct() | |
.toList() | |
.subscribe(list -> { | |
Collections.sort(list); | |
for (Tuple tuple : list) | |
System.out.println(tuple.word); | |
}); | |
} | |
private class Tuple implements Comparable { | |
String word; | |
int length; | |
Tuple (String word, int length) { | |
this.word = word; | |
this.length = length; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
Tuple tuple = (Tuple) o; | |
return length == tuple.length; | |
} | |
@Override | |
public int hashCode() { | |
return length; | |
} | |
@Override | |
public int compareTo(Object another) { | |
return (this.length > ((Tuple) another).length) ? 1 : -1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava-string:1.1.0'
compile 'io.reactivex:rxjava:1.2.0'