package com.intellij.openapi.vcs.changes;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.peer.PeerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author max
 */
public class UnversionedFilesHolder {
  private List<VirtualFile> myFiles = new ArrayList<VirtualFile>();
  private Project myProject;

  public UnversionedFilesHolder(Project project) {
    myProject = project;
  }

  public synchronized void cleanScope(final VcsDirtyScope scope) {
    ApplicationManager.getApplication().runReadAction(new Runnable() {
      public void run() {
        if (myProject.isDisposed()) return;
        final List<VirtualFile> currentFiles = new ArrayList<VirtualFile>(myFiles);
        for (VirtualFile file : currentFiles) {
          if (fileDropped(file) || scope.belongsTo(PeerFactory.getInstance().getVcsContextFactory().createFilePathOn(file))) {
            myFiles.remove(file);
          }
        }
      }
    });
  }

  private boolean fileDropped(final VirtualFile file) {
    return !file.isValid() || !ProjectRootManager.getInstance(myProject).getFileIndex().isInContent(file);
  }

  public synchronized void addFile(VirtualFile file) {
    myFiles.add(file);
  }

  public synchronized List<VirtualFile> getFiles() {
    return Collections.unmodifiableList(myFiles);
  }

  public synchronized UnversionedFilesHolder copy() {
    final UnversionedFilesHolder copyHolder = new UnversionedFilesHolder(myProject);
    copyHolder.myFiles.addAll(myFiles);
    return copyHolder;
  }

  public synchronized boolean containsFile(final VirtualFile file) {
    return myFiles.contains(file);
  }
}

@@ -324,27 +324,23 @@
 va.util.
-Collections
+HashSet
 ;%0Aimport
@@ -355,16 +355,38 @@
 il.List;
+%0Aimport java.util.Set;
 %0A%0A/**%0A *
@@ -450,19 +450,18 @@
 private 
-Lis
+Se
 t%3CVirtua
@@ -477,32 +477,30 @@
 Files = new 
-ArrayLis
+HashSe
 t%3CVirtualFil
@@ -1479,36 +1479,34 @@
 urn 
-Collections.unmodifiableList
+new ArrayList%3CVirtualFile%3E
 (myF

store unversioned files in a set, not in a list (IDEADEV-7148)