diff options
Diffstat (limited to 'platform/core-api/src/com/intellij/concurrency')
5 files changed, 54 insertions, 101 deletions
diff --git a/platform/core-api/src/com/intellij/concurrency/AsyncUtil.java b/platform/core-api/src/com/intellij/concurrency/AsyncUtil.java index 93c7573c0381..72c0a85aac72 100644 --- a/platform/core-api/src/com/intellij/concurrency/AsyncUtil.java +++ b/platform/core-api/src/com/intellij/concurrency/AsyncUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -18,12 +18,17 @@ package com.intellij.concurrency; import org.jetbrains.annotations.NotNull; import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; /** * Author: dmitrylomov */ public class AsyncUtil { + private static final AsyncFuture<Boolean> TRUE = createConst(true); + private static final AsyncFuture<Boolean> FALSE = createConst(false); + public static <V> V get(@NotNull Future<V> result) { try { return result.get(); @@ -41,4 +46,43 @@ public class AsyncUtil { } } } + + private static AsyncFuture<Boolean> createConst(final boolean result) { + return new AsyncFuture<Boolean>() { + @Override + public void addConsumer(@NotNull Executor executor, @NotNull ResultConsumer<Boolean> consumer) { + consumer.onSuccess(result); + } + + @Override + public boolean cancel(boolean mayInterruptIfRunning) { + return false; + } + + @Override + public boolean isCancelled() { + return false; + } + + @Override + public boolean isDone() { + return true; + } + + @Override + public Boolean get() { + return result; + } + + @Override + public Boolean get(long timeout, @NotNull TimeUnit unit) { + return result; + } + }; + } + + @NotNull + public static AsyncFuture<Boolean> wrapBoolean(boolean result) { + return result ? TRUE : FALSE; + } } diff --git a/platform/core-api/src/com/intellij/concurrency/DefaultResultConsumer.java b/platform/core-api/src/com/intellij/concurrency/DefaultResultConsumer.java index 7230cd469e13..0e54993fe84b 100644 --- a/platform/core-api/src/com/intellij/concurrency/DefaultResultConsumer.java +++ b/platform/core-api/src/com/intellij/concurrency/DefaultResultConsumer.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -15,13 +15,15 @@ */ package com.intellij.concurrency; +import org.jetbrains.annotations.NotNull; + /** * Author: dmitrylomov */ public class DefaultResultConsumer<V> implements ResultConsumer<V> { private final AsyncFutureResult<V> myResult; - public DefaultResultConsumer(AsyncFutureResult<V> result) { + public DefaultResultConsumer(@NotNull AsyncFutureResult<V> result) { myResult = result; } @@ -31,7 +33,7 @@ public class DefaultResultConsumer<V> implements ResultConsumer<V> { } @Override - public void onFailure(Throwable t) { + public void onFailure(@NotNull Throwable t) { myResult.setException(t); } } diff --git a/platform/core-api/src/com/intellij/concurrency/DoWhile.java b/platform/core-api/src/com/intellij/concurrency/DoWhile.java deleted file mode 100644 index b77e3258c984..000000000000 --- a/platform/core-api/src/com/intellij/concurrency/DoWhile.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2000-2013 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 com.intellij.concurrency; - -import org.jetbrains.annotations.NotNull; - -/** - * Author: dmitrylomov - */ -public abstract class DoWhile { - private final AsyncFutureResult<Boolean> myResult = AsyncFutureFactory.getInstance().createAsyncFutureResult(); - private final SameThreadExecutorWithTrampoline myExecutor = new SameThreadExecutorWithTrampoline(); - - @NotNull - public AsyncFutureResult<Boolean> getResult() { - body().addConsumer(myExecutor, new MyConsumer()); - return myResult; - } - - @NotNull - protected abstract AsyncFuture<Boolean> body(); - protected abstract boolean condition(); - - private class MyConsumer extends DefaultResultConsumer<Boolean> { - public MyConsumer() { - super(DoWhile.this.myResult); - } - - @Override - public void onSuccess(Boolean value) { - if (!value.booleanValue()) { - myResult.set(false); - } - else { - if(!condition()) { - myResult.set(true); - } - else { - body().addConsumer(myExecutor, this); - } - } - } - } - - -} diff --git a/platform/core-api/src/com/intellij/concurrency/Iterate.java b/platform/core-api/src/com/intellij/concurrency/Iterate.java deleted file mode 100644 index e158999f4da5..000000000000 --- a/platform/core-api/src/com/intellij/concurrency/Iterate.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.intellij.concurrency; - -import org.jetbrains.annotations.NotNull; - -import java.util.Iterator; - -/** - * Author: dmitrylomov - */ -public abstract class Iterate<T> extends DoWhile { - private final Iterator<T> myIterator; - private boolean myIsDone; - - public Iterate(@NotNull Iterable<T> iterable) { - myIterator = iterable.iterator(); - myIsDone = false; - } - - @NotNull - @Override - protected final AsyncFuture<Boolean> body() { - if (!myIterator.hasNext()) { - myIsDone = true; - return AsyncFutureFactory.wrap(true); - } - return process(myIterator.next()); - } - - @NotNull - protected abstract AsyncFuture<Boolean> process(T t); - - @Override - protected boolean condition() { - return !myIsDone; - } -} diff --git a/platform/core-api/src/com/intellij/concurrency/ResultConsumer.java b/platform/core-api/src/com/intellij/concurrency/ResultConsumer.java index 80618f0b9c79..409d5a9439a4 100644 --- a/platform/core-api/src/com/intellij/concurrency/ResultConsumer.java +++ b/platform/core-api/src/com/intellij/concurrency/ResultConsumer.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -15,10 +15,12 @@ */ package com.intellij.concurrency; +import org.jetbrains.annotations.NotNull; + /** * Author: dmitrylomov */ public interface ResultConsumer<V> { void onSuccess(V value); - void onFailure(Throwable t); + void onFailure(@NotNull Throwable t); } |