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      @Override
52      public Promise<T> done(Effect<? super T> e) {
53          return delegate.done(e);
54      }
55  
56      @Override
57      public Promise<T> fail(Effect<Throwable> e) {
58          return delegate.fail(e);
59      }
60  
61      @Override
62      public Promise<T> then(FutureCallback<? super T> callback) {
63          return delegate.then(callback);
64      }
65  
66      @Override
67      public <B> Promise<B> map(Function<? super T, ? extends B> function) {
68          return delegate.map(function);
69      }
70  
71      @Override
72      public <B> Promise<B> flatMap(Function<? super T, ? extends Promise<? extends B>> function) {
73          return delegate.flatMap(function);
74      }
75  
76      @Override
77      public Promise<T> recover(Function<Throwable, ? extends T> handleThrowable) {
78          return delegate.recover(handleThrowable);
79      }
80  
81      @Override
82      public <B> Promise<B> fold(Function<Throwable, ? extends B> handleThrowable, Function<? super T, ? extends B> function) {
83          return delegate.fold(handleThrowable, function);
84      }
85  
86      @Override
87      public void addListener(Runnable listener, Executor executor) {
88          delegate.addListener(listener, executor);
89      }
90  
91      @Override
92      public boolean cancel(boolean mayInterruptIfRunning) {
93          return delegate.cancel(mayInterruptIfRunning);
94      }
95  
96      @Override
97      public boolean isCancelled() {
98          return delegate.isCancelled();
99      }
100 
101     @Override
102     public boolean isDone() {
103         return delegate.isDone();
104     }
105 
106     @Override
107     public T get() throws InterruptedException, ExecutionException {
108         return delegate.get();
109     }
110 
111     @Override
112     public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
113         return delegate.get(timeout, unit);
114     }
115 }