package com.mongodb.internal.connection;
import com.mongodb.MongoException;
import com.mongodb.MongoInternalException;
import com.mongodb.MongoInterruptedException;
import com.mongodb.connection.AsyncCompletionHandler;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
class FutureAsyncCompletionHandler<T> implements AsyncCompletionHandler<T> {
private final CountDownLatch latch = new CountDownLatch(1);
private volatile T result;
private volatile Throwable error;
@Override
public void completed(final T result) {
this.result = result;
latch.countDown();
}
@Override
public void failed(final Throwable t) {
this.error = t;
latch.countDown();
}
public void getOpen() throws IOException {
get("Opening");
}
public void getWrite() throws IOException {
get("Writing to");
}
public T getRead() throws IOException {
return get("Reading from");
}
private T get(final String prefix) throws IOException {
try {
latch.await();
} catch (InterruptedException e) {
throw new MongoInterruptedException(prefix + " the AsynchronousSocketChannelStream failed", e);
}
if (error != null) {
if (error instanceof IOException) {
throw (IOException) error;
} else if (error instanceof MongoException) {
throw (MongoException) error;
} else {
throw new MongoInternalException(prefix + " the AsynchronousSocketChannelStream failed", error);
}
}
return result;
}
}