package org.glassfish.grizzly.impl;
import org.glassfish.grizzly.Cacheable;
import org.glassfish.grizzly.ThreadCache;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.glassfish.grizzly.CompletionHandler;
public final class ReadyFutureImpl<R> implements FutureImpl<R> {
private static final ThreadCache.CachedTypeIndex<ReadyFutureImpl> CACHE_IDX =
ThreadCache.obtainIndex(ReadyFutureImpl.class, 4);
public static <R> ReadyFutureImpl<R> create() {
final ReadyFutureImpl<R> future = takeFromCache();
if (future != null) {
future.isCancelled = true;
return future;
}
return new ReadyFutureImpl<R>();
}
public static <R> ReadyFutureImpl<R> create(R result) {
final ReadyFutureImpl<R> future = takeFromCache();
if (future != null) {
future.result = result;
return future;
}
return new ReadyFutureImpl<R>(result);
}
public static <R> ReadyFutureImpl<R> create(Throwable failure) {
final ReadyFutureImpl<R> future = takeFromCache();
if (future != null) {
future.failure = failure;
return future;
}
return new ReadyFutureImpl<R>(failure);
}
@SuppressWarnings("unchecked")
private static <R> ReadyFutureImpl<R> takeFromCache() {
return ThreadCache.takeFromCache(CACHE_IDX);
}
protected R result;
private Throwable failure;
private boolean isCancelled;
private ReadyFutureImpl() {
this(null, null, true);
}
private ReadyFutureImpl(R result) {
this(result, null, false);
}
private ReadyFutureImpl(Throwable failure) {
this(null, failure, false);
}
private ReadyFutureImpl(R result, Throwable failure, boolean isCancelled) {
this.result = result;
this.failure = failure;
this.isCancelled = isCancelled;
}
@Override
public void addCompletionHandler(final CompletionHandler<R> completionHandler) {
if (isCancelled) {
completionHandler.cancelled();
} else if (failure != null) {
completionHandler.failed(failure);
} else {
completionHandler.completed(result);
}
}
@Override
public R getResult() {
return result;
}
public void setResult(R result) {
throw new IllegalStateException("Can not be reset on ReadyFutureImpl");
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return isCancelled;
}
@Override
public boolean isCancelled() {
return isCancelled;
}
@Override
public boolean isDone() {
return true;
}
@Override
public R get() throws InterruptedException, ExecutionException {
if (isCancelled) {
throw new CancellationException();
} else if (failure != null) {
throw new ExecutionException(failure);
}
return result;
}
@Override
public R get(long timeout, TimeUnit unit) throws
InterruptedException, ExecutionException, TimeoutException {
if (isCancelled) {
throw new CancellationException();
} else if (failure != null) {
throw new ExecutionException(failure);
} else if (result != null) {
return result;
} else {
throw new TimeoutException();
}
}
@Override
public void failure(Throwable failure) {
throw new IllegalStateException("Can not be reset on ReadyFutureImpl");
}
@Override
public void result(R result) {
throw new IllegalStateException("Can not be reset on ReadyFutureImpl");
}
private void reset() {
result = null;
failure = null;
isCancelled = false;
}
@Override
public void markForRecycle(boolean recycleResult) {
recycle(recycleResult);
}
@Override
public void recycle() {
recycle(false);
}
@Override
public void recycle(boolean recycleResult) {
if (recycleResult && result != null && result instanceof Cacheable) {
((Cacheable) result).recycle();
}
reset();
ThreadCache.putToCache(CACHE_IDX, this);
}
}