+++ /dev/null
-/*
- * Copyright 2000-2015 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.jetbrains.concurrency;
-
-import com.intellij.util.Consumer;
-
-public abstract class ConsumerRunnable implements Consumer<Void>, Runnable {
- @Override
- public final void consume(Void aVoid) {
- run();
- }
-
- @Override
- public abstract void run();
-}
import com.intellij.util.Consumer;
import com.intellij.util.Function;
import com.intellij.util.ThreeState;
-import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
-import java.util.Collection;
-
public abstract class Promise<T> {
public static final Promise<Void> DONE = new DonePromise<Void>(null);
public static final Promise<Void> REJECTED = new RejectedPromise<Void>(createError("rejected"));
}
}
- public static <T> Promise<T> any(@NotNull final Collection<Promise<T>> promises, @NotNull final String totalError) {
- if (promises.isEmpty()) {
- //noinspection unchecked
- return (Promise<T>)DONE;
- }
- else if (promises.size() == 1) {
- return ContainerUtil.getFirstItem(promises);
- }
-
- final AsyncPromise<T> totalPromise = new AsyncPromise<T>();
- Consumer<T> done = new Consumer<T>() {
- @Override
- public void consume(T result) {
- totalPromise.setResult(result);
- }
- };
- Consumer<Throwable> rejected = new Consumer<Throwable>() {
- private volatile int toConsume = promises.size();
-
- @Override
- public void consume(Throwable throwable) {
- if (--toConsume <= 0) {
- totalPromise.setError(totalError);
- }
- }
- };
-
- for (Promise<? extends T> promise : promises) {
- promise.done(done);
- promise.rejected(rejected);
- }
- return totalPromise;
- }
-
@NotNull
public static Promise<Void> wrapAsVoid(@NotNull ActionCallback asyncResult) {
final AsyncPromise<Void> promise = new AsyncPromise<Void>();
promise.setResult(totalResult)
}
}
+}
+
+fun <T> any(promises: Collection<Promise<T>>, totalError: String): Promise<T> {
+ if (promises.isEmpty()) {
+ return resolvedPromise(null)
+ }
+ else if (promises.size == 1) {
+ return promises.first()
+ }
+
+ val totalPromise = AsyncPromise<T>()
+ val done = Consumer<T> { result -> totalPromise.setResult(result) }
+ val rejected = object : Consumer<Throwable> {
+ @Volatile private var toConsume = promises.size
+
+ override fun consume(throwable: Throwable) {
+ if (--toConsume <= 0) {
+ totalPromise.setError(totalError)
+ }
+ }
+ }
+
+ for (promise in promises) {
+ promise.done(done)
+ promise.rejected(rejected)
+ }
+ return totalPromise
}
\ No newline at end of file