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 import java.util.Collections;
26 import java.util.List;
28 public class AsyncResult<T> extends ActionCallback {
29 private static final Logger LOG = Logger.getInstance(AsyncResult.class);
33 public AsyncResult() {
36 AsyncResult(int countToDone, @Nullable T result) {
43 public AsyncResult<T> setDone(T result) {
50 public AsyncResult<T> setRejected(T result) {
57 public <DependentResult> AsyncResult<DependentResult> subResult(@NotNull Function<T, DependentResult> doneHandler) {
58 return subResult(new AsyncResult<DependentResult>(), doneHandler);
62 public <SubResult, SubAsyncResult extends AsyncResult<SubResult>> SubAsyncResult subResult(@NotNull SubAsyncResult subResult,
63 @NotNull Function<T, SubResult> doneHandler) {
64 doWhenDone(new SubResultDoneCallback<T, SubResult, SubAsyncResult>(subResult, doneHandler)).notifyWhenRejected(subResult);
69 * @deprecated Don't use AsyncResult - use Promise instead.
71 @SuppressWarnings("unused")
74 public ActionCallback subCallback(@NotNull Consumer<T> doneHandler) {
75 ActionCallback subCallback = new ActionCallback();
76 doWhenDone(new SubCallbackDoneCallback<T>(subCallback, doneHandler)).notifyWhenRejected(subCallback);
81 * @deprecated Use {@link #doWhenDone(com.intellij.util.Consumer)} (to remove in IDEA 16)
85 public AsyncResult<T> doWhenDone(@SuppressWarnings("deprecation") @NotNull final Handler<T> handler) {
86 doWhenDone(new Runnable() {
89 handler.run(myResult);
96 public AsyncResult<T> doWhenDone(@NotNull final Consumer<T> consumer) {
97 doWhenDone(new Runnable() {
100 consumer.consume(myResult);
107 public AsyncResult<T> doWhenRejected(@NotNull final PairConsumer<T, String> consumer) {
108 doWhenRejected(new Runnable() {
111 consumer.consume(myResult, myError);
119 public final AsyncResult<T> notify(@NotNull final ActionCallback child) {
124 public T getResult() {
128 public T getResultSync() {
129 return getResultSync(-1);
133 public T getResultSync(long msTimeout) {
139 public final ActionCallback doWhenProcessed(@NotNull final Consumer<T> consumer) {
140 doWhenDone(consumer);
141 doWhenRejected(new PairConsumer<T, String>() {
143 public void consume(T result, String error) {
144 consumer.consume(result);
151 * @deprecated Use {@link com.intellij.util.Consumer} (to remove in IDEA 16)
154 public interface Handler<T> {
159 * @deprecated Don't use AsyncResult - use Promise instead.
162 public static class Done<T> extends AsyncResult<T> {
163 public Done(T value) {
169 * @deprecated Don't use AsyncResult - use Promise instead.
172 public static class Rejected<T> extends AsyncResult<T> {
177 public Rejected(T value) {
183 * @deprecated Don't use AsyncResult - use Promise instead.
187 public static <R> AsyncResult<R> rejected() {
188 //noinspection unchecked,deprecation
189 return new Rejected();
193 * @deprecated Don't use AsyncResult - use Promise instead.
197 public static <R> AsyncResult<R> rejected(@NotNull String errorMessage) {
198 AsyncResult<R> result = new AsyncResult<R>();
199 result.reject(errorMessage);
204 public static <R> AsyncResult<R> done(@Nullable R result) {
205 return new AsyncResult<R>().setDone(result);
209 * @deprecated Don't use AsyncResult - use Promise instead.
213 public static <R extends List> AsyncResult<R> doneList() {
214 //noinspection unchecked
215 return done((R)Collections.emptyList());
218 // we don't use inner class, avoid memory leak, we don't want to hold this result while dependent is computing
219 private static class SubResultDoneCallback<Result, SubResult, AsyncSubResult extends AsyncResult<SubResult>> implements Consumer<Result> {
220 private final AsyncSubResult subResult;
221 private final Function<Result, SubResult> doneHandler;
223 public SubResultDoneCallback(AsyncSubResult subResult, Function<Result, SubResult> doneHandler) {
224 this.subResult = subResult;
225 this.doneHandler = doneHandler;
229 public void consume(Result result) {
232 v = doneHandler.fun(result);
234 catch (Throwable e) {
235 subResult.reject(e.getMessage());
239 subResult.setDone(v);
243 private static class SubCallbackDoneCallback<Result> implements Consumer<Result> {
244 private final ActionCallback subResult;
245 private final Consumer<Result> doneHandler;
247 public SubCallbackDoneCallback(ActionCallback subResult, Consumer<Result> doneHandler) {
248 this.subResult = subResult;
249 this.doneHandler = doneHandler;
253 public void consume(Result result) {
255 doneHandler.consume(result);
257 catch (Throwable e) {
258 subResult.reject(e.getMessage());