package org.glassfish.grizzly.utils;
import org.glassfish.grizzly.CompletionHandler;
import org.glassfish.grizzly.impl.FutureImpl;
public class CompletionHandlerAdapter<A, B>
implements CompletionHandler<B> {
private final static GenericAdapter DIRECT_ADAPTER = new GenericAdapter() {
@Override
public Object adapt(final Object result) {
return result;
}
};
private final GenericAdapter<B, A> adapter;
private final FutureImpl<A> future;
private final CompletionHandler<A> completionHandler;
public CompletionHandlerAdapter(FutureImpl<A> future) {
this(future, null);
}
public CompletionHandlerAdapter(FutureImpl<A> future,
CompletionHandler<A> completionHandler) {
this(future, completionHandler, null);
}
public CompletionHandlerAdapter(FutureImpl<A> future,
CompletionHandler<A> completionHandler,
GenericAdapter<B, A> adapter) {
this.future = future;
this.completionHandler = completionHandler;
if (adapter != null) {
this.adapter = adapter;
} else {
this.adapter = getDirectAdapter();
}
}
@Override
public void cancelled() {
if (completionHandler != null) {
completionHandler.cancelled();
}
if (future != null) {
future.cancel(false);
}
}
@Override
public void failed(Throwable throwable) {
if (completionHandler != null) {
completionHandler.failed(throwable);
}
if (future != null) {
future.failure(throwable);
}
}
@Override
public void completed(B result) {
final A adaptedResult = adapt(result);
if (completionHandler != null) {
completionHandler.completed(adaptedResult);
}
if (future != null) {
future.result(adaptedResult);
}
}
@Override
public void updated(B result) {
final A adaptedResult = adapt(result);
if (completionHandler != null) {
completionHandler.updated(adaptedResult);
}
}
protected A adapt(B result) {
return adapter.adapt(result);
}
@SuppressWarnings("unchecked")
private static <K, V> GenericAdapter<K, V> getDirectAdapter() {
return DIRECT_ADAPTER;
}
}