public static Closeable sequential(final Object lock, final Closeable... closeables) {
if (closeables.length == 0) {
return new Closeable() {
public CloseFuture close(boolean immediately) {
final CloseFuture future = new DefaultCloseFuture(lock);
future.setClosed();
return future;
}
};
} else if (closeables.length == 1) {
return closeables[0];
} else {
return new Closeable() {
public CloseFuture close(final boolean immediately) {
final DefaultCloseFuture future = new DefaultCloseFuture(lock);
final Iterator<Closeable> iterator = Arrays.asList(closeables).iterator();
SshFutureListener<CloseFuture> listener = new SshFutureListener<CloseFuture>() {
public void operationComplete(CloseFuture previousFuture) {
if (iterator.hasNext()) {
Closeable c = iterator.next();
CloseFuture nextFuture = c.close(immediately);
nextFuture.addListener(this);
} else {
future.setClosed();
}
}
};