Skip to content

Commit 3f42040

Browse files
author
Stephane Landelle
committed
Add adapter from AHC ListenableFuture to Guava one, close AsyncHttpClient#240, close AsyncHttpClient#98
1 parent f0fd001 commit 3f42040

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@
9090
<version>1.6.2</version>
9191
</dependency>
9292

93+
<dependency>
94+
<groupId>com.google.guava</groupId>
95+
<artifactId>guava</artifactId>
96+
<version>11.0.2</version>
97+
<optional>true</optional>
98+
</dependency>
99+
93100
<!-- Test dependencies -->
94101
<dependency>
95102
<groupId>ch.qos.logback</groupId>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)