Java Code Examples for com.intellij.openapi.util.ThrowableComputable#compute()

The following examples show how to use com.intellij.openapi.util.ThrowableComputable#compute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EncodingRegistry.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static <E extends Throwable> VirtualFile doActionAndRestoreEncoding(@Nonnull VirtualFile fileBefore, @Nonnull ThrowableComputable<VirtualFile, E> action) throws E {
  EncodingRegistry registry = getInstance();
  Charset charsetBefore = registry.getEncoding(fileBefore, true);
  VirtualFile fileAfter = null;
  try {
    fileAfter = action.compute();
    return fileAfter;
  }
  finally {
    if (fileAfter != null) {
      Charset actual = registry.getEncoding(fileAfter, true);
      if (!Comparing.equal(actual, charsetBefore)) {
        registry.setEncoding(fileAfter, charsetBefore);
      }
    }
  }
}
 
Example 2
Source File: CoreProgressManager.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public <T, E extends Throwable> T computePrioritized(@Nonnull ThrowableComputable<T, E> computable) throws E {
  Thread thread = Thread.currentThread();

  if (!Registry.is("ide.prioritize.threads") || isPrioritizedThread(thread)) {
    return computable.compute();
  }

  synchronized (myPrioritizationLock) {
    if (myPrioritizedThreads.isEmpty()) {
      myPrioritizingStarted = System.nanoTime();
    }
    myPrioritizedThreads.add(thread);
    updateEffectivePrioritized();
  }
  try {
    return computable.compute();
  }
  finally {
    synchronized (myPrioritizationLock) {
      myPrioritizedThreads.remove(thread);
      updateEffectivePrioritized();
    }
  }
}
 
Example 3
Source File: ProgressIndicatorUtils.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void awaitWithCheckCanceled(@Nonnull ThrowableComputable<Boolean, ? extends Exception> waiter) {
  ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
  boolean success = false;
  while (!success) {
    checkCancelledEvenWithPCEDisabled(indicator);
    try {
      success = waiter.compute();
    }
    catch (Exception e) {
      //noinspection InstanceofCatchParameter
      if (!(e instanceof InterruptedException)) {
        LOG.warn(e);
      }
      throw new ProcessCanceledException(e);
    }
  }
}
 
Example 4
Source File: ProgressIndicatorUtils.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static <T, E extends Throwable> T computeWithLockAndCheckingCanceled(@Nonnull Lock lock, int timeout, @Nonnull TimeUnit timeUnit, @Nonnull ThrowableComputable<T, E> computable)
        throws E, ProcessCanceledException {
  awaitWithCheckCanceled(lock, timeout, timeUnit);

  try {
    return computable.compute();
  }
  finally {
    lock.unlock();
  }
}
 
Example 5
Source File: DebugUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static <T, E extends Throwable> T performPsiModification(String trace, @Nonnull ThrowableComputable<T, E> runnable) throws E {
  startPsiModification(trace);
  try {
    return runnable.compute();
  }
  finally {
    finishPsiModification();
  }
}
 
Example 6
Source File: IOUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static <T> T openCleanOrResetBroken(@Nonnull ThrowableComputable<T, ? extends IOException> factoryComputable, @Nonnull Runnable cleanupCallback) throws IOException {
  try {
    return factoryComputable.compute();
  }
  catch (IOException ex) {
    cleanupCallback.run();
  }

  return factoryComputable.compute();
}
 
Example 7
Source File: ConcurrencyUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static <T, E extends Throwable> T withLock(@Nonnull Lock lock, @Nonnull ThrowableComputable<T, E> runnable) throws E {
  lock.lock();
  try {
    return runnable.compute();
  }
  finally {
    lock.unlock();
  }
}
 
Example 8
Source File: ProjectLocator.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static <T, E extends Throwable> T computeWithPreferredProject(@Nonnull VirtualFile file, @Nonnull Project preferredProject, @Nonnull ThrowableComputable<T, E> action) throws E {
  Map<VirtualFile, Project> local = ourPreferredProjects.get();
  local.put(file, preferredProject);
  try {
    return action.compute();
  }
  finally {
    local.remove(file);
  }
}
 
Example 9
Source File: FSRecords.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static <T> T writeAndHandleErrors(@Nonnull ThrowableComputable<T, ?> action) {
  try {
    w.lock();
    return action.compute();
  }
  catch (Throwable e) {
    DbConnection.handleError(e);
    throw new RuntimeException(e);
  }
  finally {
    w.unlock();
  }
}
 
Example 10
Source File: TaskbarWrapper.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static <T> T call(ThrowableComputable<T, Throwable> computable, T defaultValue) {
  if (ourTaskbarClass == null) {
    return defaultValue;
  }

  try {
    return computable.compute();
  }
  catch (Throwable throwable) {
    throwable.printStackTrace();
    return defaultValue;
  }
}
 
Example 11
Source File: BaseApplication.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public <T, E extends Throwable> T runWriteActionNoIntentLock(@Nonnull ThrowableComputable<T, E> computation) throws E {
  Class<? extends ThrowableComputable> clazz = computation.getClass();

  startWrite(clazz);
  try {
    return computation.compute();
  }
  finally {
    endWrite(clazz);
  }
}
 
Example 12
Source File: DesktopApplicationImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private <T, E extends Throwable> T runWriteActionWithClass(@Nonnull Class<?> clazz, @Nonnull ThrowableComputable<T, E> computable) throws E {
  boolean needUnlock = startWriteUI(clazz);
  try {
    return computable.compute();
  }
  finally {
    endWrite(clazz);
    if(needUnlock) {
      releaseWriteIntentLock();
    }
  }
}
 
Example 13
Source File: IndexAccessValidator.java    From consulo with Apache License 2.0 5 votes vote down vote up
public <T, E extends Throwable> T validate(@Nonnull ID<?, ?> indexKey, @Nonnull ThrowableComputable<T, E> runnable) throws E {
  checkAccessingIndexDuringOtherIndexProcessing(indexKey);
  ourAlreadyProcessingIndices.set(indexKey);
  try {
    return runnable.compute();
  }
  finally {
    ourAlreadyProcessingIndices.set(null);
  }
}
 
Example 14
Source File: AstLoadingFilter.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static <T, E extends Throwable> T forceAllowTreeLoading(@Nonnull VirtualFile virtualFile, @Nonnull ThrowableComputable<? extends T, E> computable) throws E {
  Set<VirtualFile> enabledFiles = myForcedAllowedFiles.get();
  if (enabledFiles.add(virtualFile)) {
    try {
      return computable.compute();
    }
    finally {
      enabledFiles.remove(virtualFile);
    }
  }
  else {
    return computable.compute();
  }
}
 
Example 15
Source File: AstLoadingFilter.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static <T, E extends Throwable> T disallowTreeLoading(@Nonnull ThrowableComputable<? extends T, E> computable, @Nonnull Supplier<String> debugInfo) throws E {
  if (myDisallowedInfo.get() != null) {
    return computable.compute();
  }
  else {
    try {
      myDisallowedInfo.set(debugInfo);
      return computable.compute();
    }
    finally {
      myDisallowedInfo.set(null);
    }
  }
}
 
Example 16
Source File: DataInputOutputUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Reads an element from the stream, using the given function to read it when a not-null element is expected, or returns null otherwise.
 * Should be coupled with {@link #writeNullable}
 */
@Nullable
public static <T> T readNullable(@Nonnull DataInput in, @Nonnull ThrowableComputable<T, IOException> readValue) throws IOException {
  return in.readBoolean() ? readValue.compute() : null;
}
 
Example 17
Source File: MockApplication.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public <T, E extends Throwable> T runReadAction(@Nonnull ThrowableComputable<T, E> computation) throws E {
  return computation.compute();
}
 
Example 18
Source File: MockApplication.java    From consulo with Apache License 2.0 4 votes vote down vote up
@RequiredUIAccess
@Override
public <T, E extends Throwable> T runWriteAction(@Nonnull ThrowableComputable<T, E> computation) throws E {
  return computation.compute();
}
 
Example 19
Source File: AstLoadingFilter.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static <T, E extends Throwable> T forceAllowTreeLoading(@Nullable PsiFile psiFile, @Nonnull ThrowableComputable<? extends T, E> computable) throws E {
  VirtualFile virtualFile = psiFile == null ? null : psiFile.getVirtualFile();
  return virtualFile == null ? computable.compute() : forceAllowTreeLoading(virtualFile, computable);
}
 
Example 20
Source File: VcsAnnotationCachedProxy.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * @param currentRevision - just a hint for optimization
 */
private FileAnnotation annotate(VirtualFile file, final VcsRevisionNumber revisionNumber, final boolean currentRevision,
                                final ThrowableComputable<FileAnnotation, VcsException> delegate) throws VcsException {
  final AnnotationProvider annotationProvider = myAnnotationProvider;

  final FilePath filePath = VcsUtil.getFilePath(file);

  final VcsCacheableAnnotationProvider cacheableAnnotationProvider = (VcsCacheableAnnotationProvider)annotationProvider;

  VcsAnnotation vcsAnnotation = null;
  if (revisionNumber != null) {
    Object cachedData = myCache.get(filePath, myVcs.getKeyInstanceMethod(), revisionNumber);
    vcsAnnotation = ObjectUtils.tryCast(cachedData, VcsAnnotation.class);
  }

  if (vcsAnnotation != null) {
    final VcsHistoryProvider historyProvider = myVcs.getVcsHistoryProvider();
    final VcsAbstractHistorySession history = getHistory(revisionNumber, filePath, historyProvider, vcsAnnotation.getFirstRevision());
    if (history == null) return null;
    // question is whether we need "not moved" path here?
    final ContentRevision fileContent = myVcs.getDiffProvider().createFileContent(revisionNumber, file);
    final FileAnnotation restored = cacheableAnnotationProvider.
            restore(vcsAnnotation, history, fileContent.getContent(), currentRevision,
                    revisionNumber);
    if (restored != null) {
      return restored;
    }
  }

  final FileAnnotation fileAnnotation = delegate.compute();
  vcsAnnotation = cacheableAnnotationProvider.createCacheable(fileAnnotation);
  if (vcsAnnotation == null) return fileAnnotation;

  if (revisionNumber != null) {
    myCache.put(filePath, myVcs.getKeyInstanceMethod(), revisionNumber, vcsAnnotation);
  }

  if (myVcs.getVcsHistoryProvider() instanceof VcsCacheableHistorySessionFactory) {
    loadHistoryInBackgroundToCache(revisionNumber, filePath, vcsAnnotation);
  }
  return fileAnnotation;
}