View Javadoc

1   package org.apache.mina.common;
2   
3   import java.util.concurrent.atomic.AtomicInteger;
4   
5   /**
6    * An {@link IoFuture} of {@link IoFuture}s.  It is useful when you want to
7    * get notified when all {@link IoFuture}s are complete.  It is not recommended
8    * to use {@link CompositeIoFuture} if you just want to wait for all futures.
9    * In that case, please use {@link IoUtil#await(Iterable)} instead
10   * for better performance.
11   * 
12   * @author The Apache MINA Project (dev@mina.apache.org)
13   * @version $Rev: 589474 $, $Date: 2007-10-28 21:03:14 -0600 (Sun, 28 Oct 2007) $
14   *
15   * @param <E> the type of the child futures.
16   */
17  public class CompositeIoFuture<E extends IoFuture> extends DefaultIoFuture {
18      
19      private final NotifyingListener listener = new NotifyingListener();
20      private final AtomicInteger unnotified = new AtomicInteger();
21      private volatile boolean constructionFinished;
22      
23      public CompositeIoFuture(Iterable<E> children) {
24          super(null);
25          
26          for (E f: children) {
27              f.addListener(listener);
28              unnotified.incrementAndGet();
29          }
30          
31          constructionFinished = true;
32          if (unnotified.get() == 0) {
33              setValue(true);
34          }
35      }
36      
37      private class NotifyingListener implements IoFutureListener<IoFuture> {
38          public void operationComplete(IoFuture future) {
39              if (unnotified.decrementAndGet() == 0 && constructionFinished) {
40                  setValue(true);
41              }
42          }
43      }
44  }