Created
September 3, 2016 17:47
-
-
Save Stwissel/00b59999c5606b3ef50cbf2348862928 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.HashMap; | |
import java.util.Map; | |
import rx.Observable; | |
import rx.functions.Action0; | |
import rx.functions.Action1; | |
import rx.functions.Action2; | |
import rx.functions.Func1; | |
public class LegoRunner { | |
public static void main(String[] args) { | |
LegoRunner lr = new LegoRunner(); | |
Collection<Lego> payload = lr.getLegoSample(); | |
lr.setupSubscriptionActions(); | |
lr.p("Total Elements", payload.size()); | |
System.out.println("----- TEST1 ---------"); | |
lr.test1(payload); | |
System.out.println("----- TEST2 ---------"); | |
lr.test2(payload); | |
System.out.println("----- TEST3 ---------"); | |
lr.test3(payload); | |
System.out.println("----- TEST4 ---------"); | |
lr.test4(payload); | |
System.out.println("----- TEST5 ---------"); | |
lr.test5(payload); | |
System.out.println("----- TEST6 ---------"); | |
lr.test6(payload); | |
System.out.println("----- TEST7 ---------"); | |
lr.test7(payload); | |
System.out.println("----- TEST8 ---------"); | |
lr.test8(payload); | |
System.out.println("----- TEST9 ---------"); | |
lr.test9(payload); | |
System.out.println("---- TEST10 ---------"); | |
lr.test10(payload); | |
System.out.println("---- TEST11 ---------"); | |
lr.test11(payload); | |
System.out.println("---- TEST12 ---------"); | |
lr.test12(payload); | |
System.out.println("----- DONE ---------"); | |
} | |
private Action1<Lego> onNextLego; | |
private Action1<Integer> onNextInteger; | |
private Action1<String> onNextString; | |
private Action1<Throwable> onError; | |
private Action0 onComplete; | |
private Func1<Lego, Boolean> yellowFilter; | |
private Func1<Lego, Boolean> boxFilter; | |
private Func1<Lego, Boolean> xlFilter; | |
private Func1<Lego, String> colorMap; | |
private Func1<Lego, String> shapeMap; | |
private Func1<Lego, String> colorKeySelector; | |
// Simple warm up | |
public void test1(Collection<Lego> payload) { | |
Observable<Lego> observable1 = Observable.from(payload); | |
observable1.subscribe(onNextLego, onError, onComplete); | |
} | |
// 3 only | |
public void test2(Collection<Lego> payload) { | |
Observable<Lego> observable1 = Observable.from(payload); | |
observable1.take(3).subscribe(onNextLego, onError, onComplete); | |
} | |
// yellow only | |
public void test3(Collection<Lego> payload) { | |
Observable<Lego> observable1 = Observable.from(payload); | |
observable1.filter(yellowFilter).subscribe(onNextLego, onError, onComplete); | |
} | |
//yellow boxes | |
public void test4(Collection<Lego> payload) { | |
Observable<Lego> observable1 = Observable.from(payload); | |
observable1.filter(yellowFilter).filter(boxFilter).subscribe(onNextLego, onError, onComplete); | |
} | |
// XL Boxes | |
public void test5(Collection<Lego> payload) { | |
Observable<Lego> observable1 = Observable.from(payload); | |
observable1.filter(xlFilter).filter(boxFilter).subscribe(onNextLego, onError, onComplete); | |
} | |
public void test6(Collection<Lego> payload) { | |
Observable<Lego> observable1 = Observable.from(payload); | |
observable1.count().subscribe(onNextInteger, onError, onComplete); | |
} | |
public void test7(Collection<Lego> payload) { | |
Observable<Lego> observable1 = Observable.from(payload); | |
observable1.map(colorMap).subscribe(onNextString, onError, onComplete); | |
} | |
public void test8(Collection<Lego> payload) { | |
Observable<Lego> observable1 = Observable.from(payload); | |
observable1.map(shapeMap).count().subscribe(onNextInteger, onError, onComplete); | |
} | |
public void test9(Collection<Lego> payload) { | |
Observable<Lego> observable1 = Observable.from(payload); | |
observable1.map(colorMap).distinct().subscribe(onNextString, onError, onComplete); | |
} | |
public void test10(Collection<Lego> payload) { | |
Observable<Lego> observable1 = Observable.from(payload); | |
observable1.map(shapeMap).distinct().subscribe(onNextString, onError, onComplete); | |
observable1.map(shapeMap).distinct().count().subscribe(onNextInteger, onError, onComplete); | |
} | |
public void test11(Collection<Lego> payload) { | |
Observable<Lego> observable1 = Observable.from(payload); | |
observable1.collect(new HashMap<String,Integer>(), new Action2<Map<String,Integer>,Lego>(){ | |
@Override | |
public void call(Map<String, Integer> t1, Lego t2) { | |
if (t1.containsKey(t2.color)) { | |
t1.put(t2.color,(t1.get(t2.color).intValue()+1)); | |
} else { | |
t1.put(t2.color, 1); | |
} | |
}}).subscribe(new Action1<Map<String,Integer>>(){ | |
@Override | |
public void call(Map<String, Integer> t1) { | |
for (Map.Entry<String, Integer> entry : t1.entrySet()) { | |
System.out.format("Color: %s , count: %d\n", entry.getKey(), entry.getValue()); | |
} | |
}}); | |
//observable1.groupBy(colorKeySelector).map(shapeMap).subscribe(onNextString, onError, onComplete); | |
} | |
public void test12(Collection<Lego> payload) { | |
Observable<Lego> observable1 = Observable.from(payload); | |
observable1.map(shapeMap).distinct().subscribe(onNextString, onError, onComplete); | |
observable1.map(shapeMap).distinct().count().subscribe(onNextInteger, onError, onComplete); | |
} | |
/** | |
* Filter, Map and other setup here | |
*/ | |
public void setupSubscriptionActions() { | |
this.onNextLego = new Action1<Lego>() { | |
@Override | |
public void call(Lego t1) { | |
System.out.println(t1); | |
} | |
}; | |
this.onNextInteger = new Action1<Integer>() { | |
@Override | |
public void call(Integer t1) { | |
System.out.println(t1); | |
} | |
}; | |
this.onNextString = new Action1<String>() { | |
@Override | |
public void call(String t1) { | |
System.out.println(t1); | |
} | |
}; | |
this.onError = new Action1<Throwable>() { | |
@Override | |
public void call(Throwable t1) { | |
t1.printStackTrace(); | |
} | |
}; | |
this.onComplete = new Action0() { | |
@Override | |
public void call() { | |
System.out.println("Done"); | |
} | |
}; | |
this.yellowFilter = new Func1<Lego, Boolean>() { | |
@Override | |
public Boolean call(Lego t1) { | |
return t1.color.equals("yellow"); | |
} | |
}; | |
this.boxFilter = new Func1<Lego, Boolean>() { | |
@Override | |
public Boolean call(Lego t1) { | |
return t1.shape.equals("box"); | |
} | |
}; | |
this.xlFilter = new Func1<Lego, Boolean>() { | |
@Override | |
public Boolean call(Lego t1) { | |
return t1.size.equals("xl"); | |
} | |
}; | |
this.colorMap = new Func1<Lego, String>() { | |
@Override | |
public String call(Lego t1) { | |
return t1.color; | |
} | |
}; | |
this.shapeMap = new Func1<Lego, String>() { | |
@Override | |
public String call(Lego t1) { | |
return t1.shape; | |
} | |
}; | |
this.colorKeySelector = new Func1<Lego, String>() { | |
@Override | |
public String call(Lego t1) { | |
// TODO Auto-generated method stub | |
return t1.color; | |
} | |
}; | |
} | |
public Collection<Lego> getLegoSample() { | |
Collection<Lego> result = new ArrayList<Lego>(); | |
result.add(new Lego("red", "cube", "xl", 6)); | |
result.add(new Lego("blue", "box", "s", 2)); | |
result.add(new Lego("green", "cube", "l", 4)); | |
result.add(new Lego("yellow", "odd", "m", 3)); | |
result.add(new Lego("red", "sphere", "xs", 0)); | |
result.add(new Lego("blue", "box", "xl", 8)); | |
result.add(new Lego("green", "odd", "s", 1)); | |
result.add(new Lego("green", "odd", "m", 1)); | |
result.add(new Lego("green", "odd", "l", 1)); | |
result.add(new Lego("yellow", "odd", "m", 3)); | |
result.add(new Lego("blue", "box", "m", 4)); | |
result.add(new Lego("green", "cube", "l", 4)); | |
result.add(new Lego("yellow", "sphere", "m", 3)); | |
result.add(new Lego("red", "sphere", "xl", 7)); | |
result.add(new Lego("blue", "cube", "xl", 8)); | |
result.add(new Lego("green", "odd", "s", 1)); | |
result.add(new Lego("yellow", "box", "s", 1)); | |
result.add(new Lego("yellow", "sphere", "l", 1)); | |
return result; | |
} | |
private void p(final String what, final int howmany) { | |
System.out.format(" '%s' has %d members\n", what, howmany); | |
} | |
} | |
//Helping to sort lego blocks | |
public class Lego { | |
public final String color; | |
public final String shape; | |
public final String size; | |
public final int nippels; | |
public Lego(final String color, final String shape, final String size, final int nippels) { | |
this.color = color; | |
this.shape = shape; | |
this.size = size; | |
this.nippels = nippels; | |
} | |
@Override | |
public String toString() { | |
return String.format("Color: %s, Shape: %s, Size: %s, Nipples: %d",this.color,this.shape,this.size,this.nippels); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment