|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2012 Sonatype, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * This program is licensed to you under the Apache License Version 2.0, |
| 5 | + * and you may not use this file except in compliance with the Apache License Version 2.0. |
| 6 | + * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, |
| 9 | + * software distributed under the Apache License Version 2.0 is distributed on an |
| 10 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. |
| 12 | + */ |
| 13 | +package com.ning.http.client.extra; |
| 14 | + |
| 15 | +import java.util.concurrent.ExecutionException; |
| 16 | +import java.util.concurrent.Executor; |
| 17 | +import java.util.concurrent.TimeUnit; |
| 18 | +import java.util.concurrent.TimeoutException; |
| 19 | + |
| 20 | +import com.ning.http.client.ListenableFuture; |
| 21 | + |
| 22 | +public final class ListenableFutureAdapter { |
| 23 | + |
| 24 | + /** |
| 25 | + * @param future an AHC ListenableFuture |
| 26 | + * @return a Guava ListenableFuture |
| 27 | + */ |
| 28 | + public static <V> com.google.common.util.concurrent.ListenableFuture<V> asGuavaFuture(final ListenableFuture<V> future) { |
| 29 | + |
| 30 | + return new com.google.common.util.concurrent.ListenableFuture<V>() { |
| 31 | + |
| 32 | + public boolean cancel(boolean mayInterruptIfRunning) { |
| 33 | + return future.cancel(mayInterruptIfRunning); |
| 34 | + } |
| 35 | + |
| 36 | + public V get() throws InterruptedException, ExecutionException { |
| 37 | + return future.get(); |
| 38 | + } |
| 39 | + |
| 40 | + public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
| 41 | + return future.get(timeout, unit); |
| 42 | + } |
| 43 | + |
| 44 | + public boolean isCancelled() { |
| 45 | + return future.isCancelled(); |
| 46 | + } |
| 47 | + |
| 48 | + public boolean isDone() { |
| 49 | + return future.isDone(); |
| 50 | + } |
| 51 | + |
| 52 | + public void addListener(final Runnable runnable, final Executor executor) { |
| 53 | + future.addListener(runnable, executor); |
| 54 | + } |
| 55 | + }; |
| 56 | + } |
| 57 | +} |
0 commit comments