View Javadoc

1   /*
2    * Copyright (C) 2013 Atlassian
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * 	http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.atlassian.jira.rest.client.internal.async;
18  
19  import com.atlassian.jira.rest.client.api.RestClientException;
20  import com.atlassian.util.concurrent.Effect;
21  import com.atlassian.util.concurrent.Promise;
22  import com.google.common.base.Function;
23  import com.google.common.util.concurrent.FutureCallback;
24  
25  import java.util.concurrent.ExecutionException;
26  import java.util.concurrent.Executor;
27  import java.util.concurrent.TimeUnit;
28  import java.util.concurrent.TimeoutException;
29  
30  /**
31  * This class delegates all calls to given delegate Promise. Additionally it throws new RestClientException
32   * with original RestClientException given as a cause, which gives a more useful stack trace.
33  */
34  public class DelegatingPromise<T> implements Promise<T> {
35  
36  	private final Promise<T> delegate;
37  
38  	public DelegatingPromise(Promise<T> delegate) {
39  		this.delegate = delegate;
40  	}
41  
42  	@Override
43  	public T claim() {
44  		try {
45  			return delegate.claim();
46  		} catch (RestClientException e) {
47  			throw new RestClientException(e);
48  		}
49  	}
50  
51  	public Promise<T> done(Effect<T> e) {
52  		return delegate.done(e);
53  	}
54  
55  	@Override
56  	public Promise<T> fail(Effect<Throwable> e) {
57  		return delegate.fail(e);
58  	}
59  
60  	public Promise<T> then(FutureCallback<T> callback) {
61  		return delegate.then(callback);
62  	}
63  
64  	public <B> Promise<B> map(Function<? super T, ? extends B> function) {
65  		return delegate.map(function);
66  	}
67  
68  	public <B> Promise<B> flatMap(Function<? super T, Promise<B>> function) {
69  		return delegate.flatMap(function);
70  	}
71  
72  	public Promise<T> recover(Function<Throwable, ? extends T> handleThrowable) {
73  		return delegate.recover(handleThrowable);
74  	}
75  
76  	public <B> Promise<B> fold(Function<Throwable, ? extends B> handleThrowable, Function<? super T, ? extends B> function) {
77  		return delegate.fold(handleThrowable, function);
78  	}
79  
80  	@Override
81  	public void addListener(Runnable listener, Executor executor) {
82  		delegate.addListener(listener, executor);
83  	}
84  
85  	@Override
86  	public boolean cancel(boolean mayInterruptIfRunning) {
87  		return delegate.cancel(mayInterruptIfRunning);
88  	}
89  
90  	@Override
91  	public boolean isCancelled() {
92  		return delegate.isCancelled();
93  	}
94  
95  	@Override
96  	public boolean isDone() {
97  		return delegate.isDone();
98  	}
99  
100 	@Override
101 	public T get() throws InterruptedException, ExecutionException {
102 		return delegate.get();
103 	}
104 
105 	@Override
106 	public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
107 		return delegate.get(timeout, unit);
108 	}
109 }