Created
August 30, 2016 02:45
-
-
Save Stwissel/9899999370c2741030ca75df226a4533 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 rx.Observable; | |
import rx.functions.Action0; | |
import rx.functions.Action1; | |
/** | |
* @author stw | |
* | |
*/ | |
public class RXTest { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
ComplicatedObject co = new ComplicatedObject(); | |
Observable<FancyObject> fancy = Observable.from(co); | |
fancy.take(3).subscribe(new Action1<FancyObject>() { | |
public void call(FancyObject item) { | |
System.out.println(item.getName()); | |
} | |
}, new Action1<Throwable>() { | |
public void call(Throwable error) { | |
System.out.println("Error encountered: " + error.getMessage()); | |
} | |
}, new Action0() { | |
public void call() { | |
System.out.println("Sequence complete"); | |
} | |
} | |
); | |
} | |
} | |
/* ---------- */ | |
import rx.Observable; | |
import rx.functions.Action0; | |
import rx.functions.Action1; | |
public class RXTest { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
ComplicatedObject co = new ComplicatedObject(); | |
Observable<FancyObject> fancy = Observable.from(co); | |
fancy.take(3).subscribe(new Action1<FancyObject>() { | |
public void call(FancyObject item) { | |
System.out.println(item.getName()); | |
} | |
}, new Action1<Throwable>() { | |
public void call(Throwable error) { | |
System.out.println("Error encountered: " + error.getMessage()); | |
} | |
}, new Action0() { | |
public void call() { | |
System.out.println("Sequence complete"); | |
} | |
} | |
); | |
} | |
} | |
/* ---------- */ | |
import java.util.Iterator; | |
import java.util.Vector; | |
/** | |
* @author stw | |
* | |
*/ | |
public class ComplicatedObject implements Iterator<FancyObject>, Iterable<FancyObject> { | |
private boolean isInitialized = false; | |
private int fancyCount = 0; | |
Vector<FancyObject> allOfThem = new Vector<FancyObject>(); | |
public boolean hasNext() { | |
if (!isInitialized) { | |
this.setupAccesstoFancyObject(); | |
} | |
boolean result = fancyCount < this.allOfThem.size(); | |
if (!result) { | |
this.teardownAccessToFancyObjects(); | |
} | |
return result; | |
} | |
public Iterator<FancyObject> iterator() { | |
return this; | |
} | |
public FancyObject next() { | |
FancyObject result = null; | |
if (fancyCount < this.allOfThem.size()) { | |
result = this.allOfThem.get(fancyCount); | |
fancyCount++; | |
} | |
return result; | |
} | |
private void setupAccesstoFancyObject() { | |
System.out.println("Initializing fancy objects"); | |
for (int i = 0; i < 20; i++) { | |
this.allOfThem.addElement(new FancyObject()); | |
} | |
this.isInitialized = true; | |
} | |
private void teardownAccessToFancyObjects() { | |
System.out.println("I'm doing proper cleanup here"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment