/*
 * Copyright 2000-2012 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.openapi.vfs.impl.win32;

import com.intellij.openapi.util.io.win32.FileInfo;
import com.intellij.openapi.util.io.win32.IdeaWin32;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.ArrayUtil;
import gnu.trove.THashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Map;

/**
 * @author Dmitry Avdeev
 */
class Win32FsCache {
  private final IdeaWin32 myKernel = IdeaWin32.getInstance();
  private final Map<String, FileInfo> myCache = new THashMap<String, FileInfo>();

  void clearCache() {
    myCache.clear();
  }

  @NotNull
  String[] list(@NotNull String absolutePath) {
    FileInfo[] fileInfo = myKernel.listChildren(absolutePath.replace('/', '\\'));
    if (fileInfo == null) {
      return ArrayUtil.EMPTY_STRING_ARRAY;
    }

    ArrayList<String> names = new ArrayList<String>(fileInfo.length);
    for (FileInfo info : fileInfo) {
      if (info.name.equals(".")) {
        myCache.put(absolutePath, info);
        continue;
      }
      if (info.name.equals("..")) {
        continue;
      }
      myCache.put(absolutePath + "/" + info.name, info);
      names.add(info.name);
    }

    return ArrayUtil.toStringArray(names);
  }

  @Nullable
  FileInfo getInfo(@NotNull VirtualFile file) {
    String path = file.getPath();
    FileInfo info = myCache.get(path);
    if (info == null) {
      info = myKernel.getInfo(path.replace('/', '\\'));
      if (info == null) {
        return null;
      }
      myCache.put(path, info);
    }
    return info;
  }
}

@@ -951,26 +951,117 @@
 rt java.
+lang.ref.Reference;%0Aimport java.lang.ref.SoftReference;%0Aimport java.util.ArrayList;%0Aimport java.
 util.
-Array
 List;%0Aim
@@ -1207,22 +1207,26 @@
 private 
-final 
+Reference%3C
 Map%3CStri
@@ -1238,24 +1238,25 @@
 ileInfo%3E
+%3E
  myCache
  = new T
@@ -1251,87 +1251,386 @@
 ache
- = new THashMap%3CString, FileInfo%3E();%0A%0A  void clearCache() %7B%0A    myCache.clear()
+;%0A%0A  void clearCache() %7B%0A    myCache = null;%0A  %7D%0A%0A  @NotNull%0A  private Map%3CString, FileInfo%3E getMap() %7B%0A    Reference%3CMap%3CString, FileInfo%3E%3E cache = myCache;%0A    Map%3CString, FileInfo%3E map = cache == null ? null : cache.get();%0A    if (map == null) %7B%0A      map = new THashMap%3CString, FileInfo%3E();%0A      myCache = new SoftReference%3CMap%3CString, FileInfo%3E%3E(map);%0A    %7D%0A    return map
 ;%0A  
@@ -1855,21 +1855,16 @@
  %7D%0A%0A    
-Array
 List%3CStr
@@ -1912,24 +1912,66 @@
 fo.length);%0A
+    Map%3CString, FileInfo%3E map = getMap();%0A
     for (Fil
@@ -2035,30 +2035,26 @@
  %7B%0A        m
-yCache
+ap
 .put(absolut
@@ -2158,30 +2158,26 @@
    %7D%0A      m
-yCache
+ap
 .put(absolut
@@ -2381,24 +2381,66 @@
 .getPath();%0A
+    Map%3CString, FileInfo%3E map = getMap();%0A
     FileInfo
@@ -2448,22 +2448,18 @@
 info = m
-yCache
+ap
 .get(pat
@@ -2604,22 +2604,18 @@
 %0A      m
-yCache
+ap
 .put(pat

make cache soft-reachable, to reclaim memory if necessary