Skip to content

Commit

Permalink
Work on iluwatar#403, removed checkstyle violations
Browse files Browse the repository at this point in the history
  • Loading branch information
npathai committed Jul 22, 2016
1 parent 2b945ca commit eb560f5
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 98 deletions.
2 changes: 1 addition & 1 deletion promise/src/main/java/com/iluwatar/promise/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class App {
* Program entry point
* @param args arguments
* @throws InterruptedException if main thread is interruped.
* @throws ExecutionException
* @throws ExecutionException if an execution error occurs.
*/
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService executor = Executors.newSingleThreadExecutor();
Expand Down
95 changes: 0 additions & 95 deletions promise/src/main/java/com/iluwatar/promise/Promise.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
import java.util.function.Function;

Expand Down Expand Up @@ -147,96 +144,4 @@ public void run() {
}
}
}
}


/**
* A really simplified implementation of future that allows completing it successfully with a value
* or exceptionally with an exception.
*/
class PromiseSupport<T> implements Future<T> {

static final int RUNNING = 1;
static final int FAILED = 2;
static final int COMPLETED = 3;

final Object lock;

volatile int state = RUNNING;
T value;
Exception exception;

PromiseSupport() {
this.lock = new Object();
}

void fulfill(T value) {
this.value = value;
this.state = COMPLETED;
synchronized (lock) {
lock.notifyAll();
}
}

void fulfillExceptionally(Exception exception) {
this.exception = exception;
this.state = FAILED;
synchronized (lock) {
lock.notifyAll();
}
}

@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return false;
}

@Override
public boolean isCancelled() {
return false;
}

@Override
public boolean isDone() {
return state > RUNNING;
}

@Override
public T get() throws InterruptedException, ExecutionException {
if (state == COMPLETED) {
return value;
} else if (state == FAILED) {
throw new ExecutionException(exception);
} else {
synchronized (lock) {
lock.wait();
if (state == COMPLETED) {
return value;
} else {
throw new ExecutionException(exception);
}
}
}
}

@Override
public T get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
if (state == COMPLETED) {
return value;
} else if (state == FAILED) {
throw new ExecutionException(exception);
} else {
synchronized (lock) {
lock.wait(unit.toMillis(timeout));
if (state == COMPLETED) {
return value;
} else if (state == FAILED) {
throw new ExecutionException(exception);
} else {
throw new TimeoutException();
}
}
}
}
}
6 changes: 4 additions & 2 deletions promise/src/test/java/com/iluwatar/promise/PromiseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ private void testWaitingForeverForPromiseToBeFulfilled() throws InterruptedExcep
@Override
public Integer call() throws Exception {
throw new RuntimeException("Barf!");
}}, executor);
}
}, executor);

try {
promise.get();
Expand All @@ -85,7 +86,8 @@ private void testWaitingSomeTimeForPromiseToBeFulfilled()
@Override
public Integer call() throws Exception {
throw new RuntimeException("Barf!");
}}, executor);
}
}, executor);

try {
promise.get(1000, TimeUnit.SECONDS);
Expand Down

0 comments on commit eb560f5

Please sign in to comment.