Last active
December 11, 2015 21:18
-
-
Save darylteo/4661587 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 org.edgeframework.promises.Promise; | |
import org.edgeframework.promises.PromiseHandler; | |
import org.edgeframework.promises.RepromiseHandler; | |
import org.vertx.java.core.AsyncResult; | |
import org.vertx.java.core.AsyncResultHandler; | |
import org.vertx.java.core.Handler; | |
import org.vertx.java.core.buffer.Buffer; | |
import org.vertx.java.core.file.AsyncFile; | |
import org.vertx.java.deploy.Verticle; | |
public class PromiseSandbox extends Verticle { | |
private PromiseSandbox that = this; | |
@Override | |
public void start() throws Exception { | |
System.out.println("start"); | |
this.openFile() | |
.then(new RepromiseHandler<AsyncFile, Buffer>() { | |
@Override | |
public Promise<Buffer> handle(final AsyncFile file) { | |
System.out.println("read"); | |
final Promise<Buffer> result = Promise.defer(); | |
that.getFirst10Bytes(file) | |
.then(new PromiseHandler<Buffer, Void>() { | |
@Override | |
public Void handle(Buffer value) { | |
System.out.println("Close"); | |
file.close(); | |
result.fulfill(value); | |
return null; | |
} | |
}); | |
return result; | |
} | |
}) | |
.then(new PromiseHandler<Buffer, Void>() { | |
@Override | |
public Void handle(Buffer value) { | |
System.out.println("loaded " + value); | |
return null; | |
} | |
}); | |
} | |
private Promise<AsyncFile> openFile() { | |
return Promise.defer(new Handler<Promise<AsyncFile>>() { | |
@Override | |
public void handle(final Promise<AsyncFile> promise) { | |
System.out.println("1. promise handle"); | |
getVertx().fileSystem().open("build.gradle", new AsyncResultHandler<AsyncFile>() { | |
@Override | |
public void handle(AsyncResult<AsyncFile> file) { | |
promise.fulfill(file.result); | |
} | |
}); | |
} | |
}); | |
} | |
private Promise<Buffer> getFirst10Bytes(AsyncFile file) { | |
final Promise<Buffer> result = Promise.defer(); | |
final Buffer buffer = new Buffer(20); | |
file.read(buffer, 0, 0, 20, new AsyncResultHandler<Buffer>() { | |
@Override | |
public void handle(AsyncResult<Buffer> read) { | |
System.out.println("First 20 Bytes read"); | |
buffer.appendBuffer(read.result); | |
result.fulfill(buffer); | |
} | |
}); | |
return result; | |
} | |
} |
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 org.edgeframework.promises.Promise; | |
import org.edgeframework.promises.PromiseHandler; | |
import org.edgeframework.promises.RepromiseHandler; | |
import org.vertx.java.core.AsyncResult; | |
import org.vertx.java.core.AsyncResultHandler; | |
import org.vertx.java.core.Handler; | |
import org.vertx.java.core.buffer.Buffer; | |
import org.vertx.java.core.file.AsyncFile; | |
import org.vertx.java.deploy.Verticle; | |
public class PromiseSandbox extends Verticle { | |
private PromiseSandbox that = this; | |
@Override | |
public void start() throws Exception { | |
System.out.println("start"); | |
this.openFile() | |
.then(new RepromiseHandler<AsyncFile, Buffer>() { | |
@Override | |
public Promise<Buffer> handle(AsyncFile file) { | |
System.out.println("read"); | |
BytesLoadedHandler closeFile = new BytesLoadedHandler(file); | |
that.getFirst10Bytes(file) | |
.then(closeFile); | |
return closeFile.getBufferPromise(); | |
} | |
}) | |
.then(new PromiseHandler<Buffer, Void>() { | |
@Override | |
public Void handle(Buffer value) { | |
System.out.println("loaded " + value); | |
return null; | |
} | |
}); | |
} | |
private Promise<AsyncFile> openFile() { | |
return Promise.defer(new Handler<Promise<AsyncFile>>() { | |
@Override | |
public void handle(final Promise<AsyncFile> promise) { | |
System.out.println("1. promise handle"); | |
getVertx().fileSystem().open("build.gradle", new AsyncResultHandler<AsyncFile>() { | |
@Override | |
public void handle(AsyncResult<AsyncFile> file) { | |
promise.fulfill(file.result); | |
} | |
}); | |
} | |
}); | |
} | |
private Promise<Buffer> getFirst10Bytes(AsyncFile file) { | |
final Promise<Buffer> result = Promise.defer(); | |
final Buffer buffer = new Buffer(20); | |
file.read(buffer, 0, 0, 20, new AsyncResultHandler<Buffer>() { | |
@Override | |
public void handle(AsyncResult<Buffer> read) { | |
System.out.println("First 20 Bytes read"); | |
buffer.appendBuffer(read.result); | |
result.fulfill(buffer); | |
} | |
}); | |
return result; | |
} | |
private class BytesLoadedHandler implements PromiseHandler<Buffer, Void> { | |
private AsyncFile file; | |
private Promise<Buffer> promise = Promise.defer(); | |
public BytesLoadedHandler(AsyncFile file) { | |
this.file = file; | |
} | |
@Override | |
public Void handle(Buffer buffer) { | |
System.out.println("Close"); | |
file.close(); | |
this.promise.fulfill(buffer); | |
return null; | |
} | |
public Promise<Buffer> getBufferPromise() { | |
return this.promise; | |
} | |
} | |
} |
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 org.edgeframework.promises.Promise; | |
import org.edgeframework.promises.PromiseHandler; | |
import org.edgeframework.promises.RepromiseHandler; | |
import org.vertx.java.core.AsyncResult; | |
import org.vertx.java.core.AsyncResultHandler; | |
import org.vertx.java.core.Handler; | |
import org.vertx.java.core.buffer.Buffer; | |
import org.vertx.java.core.file.AsyncFile; | |
import org.vertx.java.deploy.Verticle; | |
public class PromiseSandbox extends Verticle { | |
private PromiseSandbox that = this; | |
private class Pair<L, R> { | |
private L left; | |
private R right; | |
public Pair(L l, R r) { | |
this.left = l; | |
this.right = r; | |
} | |
public R getRight() { | |
return right; | |
} | |
public L getLeft() { | |
return left; | |
} | |
} | |
@Override | |
public void start() throws Exception { | |
System.out.println("start"); | |
this.openFile() | |
.then(new RepromiseHandler<AsyncFile, Pair<AsyncFile, Buffer>>() { | |
@Override | |
public Promise<Pair<AsyncFile, Buffer>> handle(AsyncFile file) { | |
System.out.println("read"); | |
return that.getFirst10Bytes(file); | |
} | |
}) | |
.then(new PromiseHandler<Pair<AsyncFile, Buffer>, Void>() { | |
@Override | |
public Void handle(Pair<AsyncFile, Buffer> value) { | |
value.getLeft().close(); | |
System.out.println("loaded " + value.getRight()); | |
return null; | |
} | |
}); | |
} | |
private Promise<AsyncFile> openFile() { | |
return Promise.defer(new Handler<Promise<AsyncFile>>() { | |
@Override | |
public void handle(final Promise<AsyncFile> promise) { | |
System.out.println("1. promise handle"); | |
getVertx().fileSystem().open("build.gradle", new AsyncResultHandler<AsyncFile>() { | |
@Override | |
public void handle(AsyncResult<AsyncFile> file) { | |
promise.fulfill(file.result); | |
} | |
}); | |
} | |
}); | |
} | |
private Promise<Pair<AsyncFile, Buffer>> getFirst10Bytes(final AsyncFile file) { | |
final Promise<Pair<AsyncFile, Buffer>> result = Promise.defer(); | |
final Buffer buffer = new Buffer(20); | |
file.read(buffer, 0, 0, 20, new AsyncResultHandler<Buffer>() { | |
@Override | |
public void handle(AsyncResult<Buffer> read) { | |
System.out.println("First 20 Bytes read"); | |
buffer.appendBuffer(read.result); | |
result.fulfill(new Pair<>(file, buffer)); | |
} | |
}); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment