001    /*
002     * Copyright (C) 2003-2009 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    package org.crsh.util;
020    
021    import java.util.concurrent.*;
022    import java.util.concurrent.locks.Lock;
023    import java.util.concurrent.locks.ReentrantLock;
024    
025    /**
026     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
027     */
028    public class LatchedFuture<V> implements Future<V> {
029    
030      /** . */
031      private V value = null;
032    
033      /** . */
034      private final CountDownLatch latch = new CountDownLatch(1);
035    
036      /** . */
037      private final Lock lock = new ReentrantLock();
038    
039      /** The optional listener. */
040      private FutureListener<V> listener;
041    
042      public LatchedFuture() {
043      }
044    
045      public LatchedFuture(V value) {
046        set(value);
047      }
048    
049      public boolean cancel(boolean b) {
050        return false;
051      }
052    
053      public boolean isCancelled() {
054        return false; 
055      }
056    
057      public boolean isDone() {
058        return latch.getCount() == 0;
059      }
060    
061      public V get() throws InterruptedException, ExecutionException {
062        latch.await();
063        return value;
064      }
065    
066      public V get(long l, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException {
067        latch.await(l, timeUnit);
068        return value;
069      }
070    
071      public void addListener(FutureListener<V> listener) {
072        if (listener == null) {
073          throw new NullPointerException();
074        }
075        lock.lock();
076        try {
077    
078          // Avoid double init
079          if (this.listener != null) {
080            throw new IllegalStateException();
081          }
082    
083          // Set state
084          this.listener = listener;
085    
086          //
087          if (latch.getCount() == 0) {
088            listener.completed(value);
089          }
090    
091          //
092        } finally {
093          lock.unlock();
094        }
095      }
096    
097      public void set(V value) {
098        lock.lock();
099        try {
100          if (latch.getCount() > 0) {
101            this.value = value;
102            latch.countDown();
103            if (listener != null) {
104              listener.completed(value);
105            }
106          } else {
107            throw new IllegalStateException();
108          }
109        } finally {
110          lock.unlock();
111        }
112      }
113    }