Skip to content

Instantly share code, notes, and snippets.

@Stwissel
Created August 30, 2016 12:32
Show Gist options
  • Save Stwissel/58d27d2a463ed9adeb4039ff3a5aa0da to your computer and use it in GitHub Desktop.
Save Stwissel/58d27d2a463ed9adeb4039ff3a5aa0da to your computer and use it in GitHub Desktop.
import rx.Observable;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func0;
import rx.functions.Func1;
/**
* @author stw
*
*/
public class RXTest {
public static void main(String[] args) {
Observable<FancyObject> source = Observable.using(new Func0<ComplicatedObject>() {
@Override
public ComplicatedObject call() {
return new ComplicatedObject();
}
}, new Func1<ComplicatedObject, Observable<FancyObject>>() {
@Override
public Observable<FancyObject> call(ComplicatedObject co) {
return Observable.from(co);
}
},
new Action1<ComplicatedObject>() {
@Override
public void call(ComplicatedObject co) {
co.tearDown();
}
});
source.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.Date;
import java.util.UUID;
/**
* @author stw
*
*/
public class FancyObject {
private String name = UUID.randomUUID().toString();
private Date created = new Date();
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreated() {
return this.created;
}
public void setCreated(Date created) {
this.created = created;
}
}
/* --------------- */
import java.util.Iterator;
public class FancyIterator implements Iterator<FancyObject> {
private final ComplicatedObject theObject;
private int fancyCount = 0;
public FancyIterator(ComplicatedObject co) {
this.theObject = co;
}
public boolean hasNext() {
return this.theObject.hasObject(this.fancyCount);
}
public FancyObject next() {
FancyObject result = this.theObject.getOne(this.fancyCount);
this.fancyCount++;
return result;
}
}
/* --------------- */
import java.util.Iterator;
import java.util.Vector;
/**
* @author stw
*
*/
public class ComplicatedObject implements Iterable<FancyObject> {
private boolean isInitialized = false;
Vector<FancyObject> allOfThem = new Vector<FancyObject>();
public Iterator<FancyObject> iterator() {
return new FancyIterator(this);
}
public boolean hasObject(int whichone) {
if (!this.isInitialized) {
this.setupAccesstoFancyObject();
}
return (whichone < this.allOfThem.size());
}
public FancyObject getOne(int whichone) {
if (!this.isInitialized) {
this.setupAccesstoFancyObject();
}
if (whichone < this.allOfThem.size()) {
return this.allOfThem.get(whichone);
}
// If we ask bejond...
this.isInitialized = false;
this.tearDown();
return null;
}
private void setupAccesstoFancyObject() {
System.out.println("Initializing fancy objects");
for (int i = 0; i < 20; i++) {
this.allOfThem.addElement(new FancyObject());
}
this.isInitialized = true;
}
public void tearDown() {
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