Java 类com.intellij.util.TimedReference 实例源码

项目:tools-idea    文件:AllClassesSearchExecutor.java   
@NotNull
private static String[] getAllClassNames(@NotNull final Project project) {
  return ApplicationManager.getApplication().runReadAction(new Computable<String[]>() {
    @Override
    public String[] compute() {
      final long modCount = PsiManager.getInstance(project).getModificationTracker().getJavaStructureModificationCount();
      TimedReference<SoftReference<Pair<String[], Long>>> ref1 = project.getUserData(ALL_CLASS_NAMES_CACHE);
      SoftReference<Pair<String[], Long>> ref2 = ref1 == null ? null : ref1.get();
      Pair<String[], Long> pair = ref2 == null ? null : ref2.get();
      if (pair != null && pair.second.equals(modCount)) {
        return pair.first;
      }

      String[] names = PsiShortNamesCache.getInstance(project).getAllClassNames();
      ref1 = new TimedReference<SoftReference<Pair<String[], Long>>>(null);
      ref1.set(new SoftReference<Pair<String[], Long>>(Pair.create(names, modCount)));
      project.putUserData(ALL_CLASS_NAMES_CACHE, ref1);
      return names;
    }
  });
}