/*
 * Copyright 2000-2015 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.javaee;

import com.intellij.codeInsight.daemon.impl.quickfix.FetchExtResourceAction;
import com.intellij.openapi.util.NotNullLazyValue;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.indexing.IndexableSetContributor;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.Set;

/**
 * @author Dmitry Avdeev
 */
public class ExternalResourcesRootsProvider extends IndexableSetContributor {
  private final NotNullLazyValue<Set<VirtualFile>> myStandardResources = new NotNullLazyValue<Set<VirtualFile>>() {
    @NotNull
    @Override
    protected Set<VirtualFile> compute() {
      ExternalResourceManagerExImpl manager = (ExternalResourceManagerExImpl)ExternalResourceManager.getInstance();
      Set<ExternalResourceManagerExImpl.Resource> dirs = new THashSet<>();
      Set<VirtualFile> set = new THashSet<>();
      for (Map<String, ExternalResourceManagerExImpl.Resource> map : manager.getStandardResources()) {
        for (ExternalResourceManagerExImpl.Resource resource : map.values()) {
          ExternalResourceManagerExImpl.Resource dir = new ExternalResourceManagerExImpl.Resource(
            resource.directoryName(), resource);

          if (dirs.add(dir)) {
            String url = resource.getResourceUrl();
            if (url != null) {
              ContainerUtil.addIfNotNull(set, VfsUtilCore.findRelativeFile(url.substring(0, url.lastIndexOf('/') + 1), null));
            }
          }
        }
      }
      return set;
    }
  };

  @NotNull
  @Override
  public Set<VirtualFile> getAdditionalRootsToIndex() {
    Set<VirtualFile> roots = new THashSet<>(myStandardResources.getValue());

    String path = FetchExtResourceAction.getExternalResourcesPath();
    VirtualFile extResources = LocalFileSystem.getInstance().findFileByPath(path);
    ContainerUtil.addIfNotNull(roots, extResources);

    return roots;
  }
}

@@ -705,59 +705,8 @@
 on;%0A
-import com.intellij.openapi.util.NotNullLazyValue;%0A
 impo
@@ -840,16 +840,202 @@
 alFile;%0A
+import com.intellij.openapi.vfs.VirtualFileManager;%0Aimport com.intellij.psi.util.CachedValue;%0Aimport com.intellij.psi.util.CachedValueProvider;%0Aimport com.intellij.util.CachedValueImpl;%0A
 import c
@@ -1378,27 +1378,22 @@
 e final 
-NotNullLazy
+Cached
 Value%3CSe
@@ -1438,123 +1438,38 @@
 new 
-NotNullLazyValue%3CSet%3CVirtualFile%3E
+CachedValueImpl%3C
 %3E(
+(
 ) 
-%7B%0A    @NotNull%0A    @Override%0A    protected Set%3CVirtualFile%3E compute()
+-%3E
  %7B%0A    
- 
- 
 Exte
@@ -1574,26 +1574,24 @@
 ance();%0A    
-  
 Set%3CExternal
@@ -1651,18 +1651,16 @@
 ();%0A    
- 
- 
 Set%3CVirt
@@ -1688,26 +1688,24 @@
 ashSet%3C%3E();%0A
-  
     for (Map
@@ -1799,18 +1799,16 @@
 %7B%0A      
- 
- 
 for (Ext
@@ -1866,26 +1866,24 @@
 values()) %7B%0A
-  
         Exte
@@ -1977,18 +1977,16 @@
         
- 
- 
 resource
@@ -2019,26 +2019,24 @@
 );%0A%0A        
-  
 if (dirs.add
@@ -2054,18 +2054,16 @@
         
- 
- 
 String u
@@ -2100,26 +2100,24 @@
 ;%0A          
-  
 if (url != n
@@ -2119,26 +2119,24 @@
  != null) %7B%0A
-  
             
@@ -2252,22 +2252,8 @@
 ));%0A
-            %7D%0A
     
@@ -2276,24 +2276,28 @@
 %7D%0A      %7D%0A  
+  %7D%0A
     return s
@@ -2299,22 +2299,100 @@
 urn 
-set;%0A    %7D
+CachedValueProvider.Result.create(set, VirtualFileManager.VFS_STRUCTURE_MODIFICATIONS);
 %0A  %7D
+)
 ;%0A%0A 

drop ExternalResourcesRootsProvider cache on VFS structure change (EA-138666 - assert: IndexableSetContributor.filterOutNulls)