2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util;
21 import java.io.IOException;
22 import java.util.concurrent.CancellationException;
23 import java.util.concurrent.CountDownLatch;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.Future;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.TimeoutException;
28 import java.util.concurrent.atomic.AtomicBoolean;
30 public class FuturePromise<C> implements Future<C>,Promise<C>
32 private static Throwable COMPLETED=new Throwable();
33 private final AtomicBoolean _done=new AtomicBoolean(false);
34 private final CountDownLatch _latch=new CountDownLatch(1);
35 private Throwable _cause;
38 public FuturePromise()
41 public FuturePromise(C result)
49 public FuturePromise(C ctx, Throwable failed)
58 public void succeeded(C result)
60 if (_done.compareAndSet(false,true))
69 public void failed(Throwable cause)
71 if (_done.compareAndSet(false,true))
79 public boolean cancel(boolean mayInterruptIfRunning)
81 if (_done.compareAndSet(false,true))
84 _cause=new CancellationException();
92 public boolean isCancelled()
100 catch (InterruptedException e)
102 throw new RuntimeException(e);
104 return _cause instanceof CancellationException;
110 public boolean isDone()
112 return _done.get() && _latch.getCount()==0;
116 public C get() throws InterruptedException, ExecutionException
119 if (_cause==COMPLETED)
121 if (_cause instanceof CancellationException)
122 throw (CancellationException) new CancellationException().initCause(_cause);
123 throw new ExecutionException(_cause);
127 public C get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
129 if (!_latch.await(timeout,unit))
130 throw new TimeoutException();
132 if (_cause==COMPLETED)
134 if (_cause instanceof TimeoutException)
135 throw (TimeoutException)_cause;
136 if (_cause instanceof CancellationException)
137 throw (CancellationException) new CancellationException().initCause(_cause);
138 throw new ExecutionException(_cause);
141 public static void rethrow(ExecutionException e) throws IOException
143 Throwable cause=e.getCause();
144 if (cause instanceof IOException)
145 throw (IOException)cause;
146 if (cause instanceof Error)
148 if (cause instanceof RuntimeException)
149 throw (RuntimeException)cause;
150 throw new RuntimeException(cause);
154 public String toString()
156 return String.format("FutureCallback@%x{%b,%b,%s}",hashCode(),_done,_cause==COMPLETED,_result);