-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inspection IO is now async and poll checks for cancellation (#192).
- Loading branch information
Showing
5 changed files
with
76 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/org/infernus/idea/checkstyle/util/Async.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.infernus.idea.checkstyle.util; | ||
|
||
import com.intellij.openapi.application.ApplicationManager; | ||
import com.intellij.openapi.progress.ProgressManager; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.Future; | ||
|
||
public class Async { | ||
private static final int FIFTY_MS = 50; | ||
|
||
@Nullable | ||
public static <T> T asyncResultOf(@NotNull final Callable<T> callable, | ||
@Nullable final T defaultValue) { | ||
try { | ||
return whenFinished(executeOnPooledThread(callable)).get(); | ||
|
||
} catch (Exception e) { | ||
return defaultValue; | ||
} | ||
} | ||
|
||
private static <T> Future<T> executeOnPooledThread(final Callable<T> callable) { | ||
return ApplicationManager.getApplication().executeOnPooledThread(callable); | ||
} | ||
|
||
private static <T> Future<T> whenFinished(final Future<T> future) { | ||
while (!future.isDone() && !future.isCancelled()) { | ||
ProgressManager.checkCanceled(); | ||
waitFor(FIFTY_MS); | ||
} | ||
return future; | ||
} | ||
|
||
private static void waitFor(final int millis) { | ||
try { | ||
Thread.sleep(millis); | ||
} catch (InterruptedException ignored) { | ||
// ignored | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters