2 * Copyright 2000-2016 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.intellij.openapi.util;
18 import com.intellij.openapi.diagnostic.Logger;
19 import com.intellij.util.Consumer;
20 import com.intellij.util.Function;
21 import com.intellij.util.PairConsumer;
22 import org.jetbrains.annotations.NotNull;
23 import org.jetbrains.annotations.Nullable;
25 public class AsyncResult<T> extends ActionCallback {
26 private static final Logger LOG = Logger.getInstance(AsyncResult.class);
30 public AsyncResult() {
33 AsyncResult(int countToDone, @Nullable T result) {
40 public AsyncResult<T> setDone(T result) {
47 public AsyncResult<T> setRejected(T result) {
54 public <DependentResult> AsyncResult<DependentResult> subResult(@NotNull Function<T, DependentResult> doneHandler) {
55 return subResult(new AsyncResult<DependentResult>(), doneHandler);
59 public <SubResult, SubAsyncResult extends AsyncResult<SubResult>> SubAsyncResult subResult(@NotNull SubAsyncResult subResult,
60 @NotNull Function<T, SubResult> doneHandler) {
61 doWhenDone(new SubResultDoneCallback<T, SubResult, SubAsyncResult>(subResult, doneHandler)).notifyWhenRejected(subResult);
66 * @deprecated Don't use AsyncResult - use Promise instead.
68 @SuppressWarnings("unused")
71 public ActionCallback subCallback(@NotNull Consumer<T> doneHandler) {
72 ActionCallback subCallback = new ActionCallback();
73 doWhenDone(new SubCallbackDoneCallback<T>(subCallback, doneHandler)).notifyWhenRejected(subCallback);
78 * @deprecated Use {@link #doWhenDone(com.intellij.util.Consumer)} (to remove in IDEA 16)
82 public AsyncResult<T> doWhenDone(@SuppressWarnings("deprecation") @NotNull final Handler<T> handler) {
83 doWhenDone(new Runnable() {
86 handler.run(myResult);
93 public AsyncResult<T> doWhenDone(@NotNull final Consumer<T> consumer) {
94 doWhenDone(new Runnable() {
97 consumer.consume(myResult);
104 public AsyncResult<T> doWhenRejected(@NotNull final PairConsumer<T, String> consumer) {
105 doWhenRejected(new Runnable() {
108 consumer.consume(myResult, myError);
116 public final AsyncResult<T> notify(@NotNull final ActionCallback child) {
121 public T getResult() {
125 public T getResultSync() {
126 return getResultSync(-1);
130 public T getResultSync(long msTimeout) {
136 public final ActionCallback doWhenProcessed(@NotNull final Consumer<T> consumer) {
137 doWhenDone(consumer);
138 doWhenRejected(new PairConsumer<T, String>() {
140 public void consume(T result, String error) {
141 consumer.consume(result);
148 * @deprecated Use {@link com.intellij.util.Consumer} (to remove in IDEA 16)
151 public interface Handler<T> {
156 * @deprecated Don't use AsyncResult - use Promise instead.
159 public static class Done<T> extends AsyncResult<T> {
160 public Done(T value) {
166 * @deprecated Don't use AsyncResult - use Promise instead.
169 public static class Rejected<T> extends AsyncResult<T> {
174 public Rejected(T value) {
180 * @deprecated Don't use AsyncResult - use Promise instead.
184 public static <R> AsyncResult<R> rejected() {
185 //noinspection unchecked,deprecation
186 return new Rejected();
190 * @deprecated Don't use AsyncResult - use Promise instead.
194 public static <R> AsyncResult<R> rejected(@NotNull String errorMessage) {
195 AsyncResult<R> result = new AsyncResult<R>();
196 result.reject(errorMessage);
201 public static <R> AsyncResult<R> done(@Nullable R result) {
202 return new AsyncResult<R>().setDone(result);
205 // we don't use inner class, avoid memory leak, we don't want to hold this result while dependent is computing
206 private static class SubResultDoneCallback<Result, SubResult, AsyncSubResult extends AsyncResult<SubResult>> implements Consumer<Result> {
207 private final AsyncSubResult subResult;
208 private final Function<Result, SubResult> doneHandler;
210 public SubResultDoneCallback(AsyncSubResult subResult, Function<Result, SubResult> doneHandler) {
211 this.subResult = subResult;
212 this.doneHandler = doneHandler;
216 public void consume(Result result) {
219 v = doneHandler.fun(result);
221 catch (Throwable e) {
222 subResult.reject(e.getMessage());
226 subResult.setDone(v);
230 private static class SubCallbackDoneCallback<Result> implements Consumer<Result> {
231 private final ActionCallback subResult;
232 private final Consumer<Result> doneHandler;
234 public SubCallbackDoneCallback(ActionCallback subResult, Consumer<Result> doneHandler) {
235 this.subResult = subResult;
236 this.doneHandler = doneHandler;
240 public void consume(Result result) {
242 doneHandler.consume(result);
244 catch (Throwable e) {
245 subResult.reject(e.getMessage());