1 19 20 package org.netbeans.modules.subversion.util; 21 22 import org.netbeans.modules.versioning.util.FlatFolder; 23 24 import java.io.File ; 25 import java.io.Serializable ; 26 import java.util.*; 27 28 35 public class Context implements Serializable { 36 37 public static final Context Empty = new Context( getEmptyList(), getEmptyList(), getEmptyList() ); 38 39 private static final long serialVersionUID = 1L; 40 41 private final List<File > filteredFiles; 42 private final List<File > rootFiles; 43 private final List<File > exclusions; 44 45 public Context(List<File > filteredFiles, List<File > rootFiles, List<File > exclusions) { 46 this.filteredFiles = filteredFiles; 47 this.rootFiles = rootFiles; 48 this.exclusions = exclusions; 49 while (normalize()); 50 } 51 52 public Context(File file) { 53 this(new File [] { file }); 54 } 55 56 public Context(File [] files) { 57 List<File > list = new ArrayList<File >(files.length); 58 list.addAll(Arrays.asList(files)); 59 removeDuplicates(list); 60 this.filteredFiles = list; 61 this.rootFiles = list; 62 this.exclusions = Collections.emptyList(); 63 } 64 65 private boolean normalize() { 66 for (Iterator<File > i = rootFiles.iterator(); i.hasNext();) { 67 File root = i.next(); 68 for (Iterator<File > j = exclusions.iterator(); j.hasNext();) { 69 File exclusion = j.next(); 70 if (SvnUtils.isParentOrEqual(exclusion, root)) { 71 j.remove(); 72 exclusionRemoved(exclusion, root); 73 return true; 74 } 75 } 76 } 77 removeDuplicates(rootFiles); 78 removeDuplicates(exclusions); 79 return false; 80 } 81 82 private void removeDuplicates(List<File > files) { 83 List<File > newFiles = new ArrayList<File >(); 84 outter: for (Iterator<File > i = files.iterator(); i.hasNext();) { 85 File file = i.next(); 86 for (Iterator<File > j = newFiles.iterator(); j.hasNext();) { 87 File includedFile = j.next(); 88 if (SvnUtils.isParentOrEqual(includedFile, file) && (file.isFile() || !(includedFile instanceof FlatFolder))) continue outter; 89 if (SvnUtils.isParentOrEqual(file, includedFile) && (includedFile.isFile() || !(file instanceof FlatFolder))) { 90 j.remove(); 91 } 92 } 93 newFiles.add(file); 94 } 95 files.clear(); 96 files.addAll(newFiles); 97 } 98 99 private void exclusionRemoved(File exclusion, File root) { 100 File [] exclusionChildren = exclusion.listFiles(); 101 if (exclusionChildren == null) return; 102 for (int i = 0; i < exclusionChildren.length; i++) { 103 File child = exclusionChildren[i]; 104 if (!SvnUtils.isParentOrEqual(root, child)) { 105 exclusions.add(child); 106 } 107 } 108 } 109 110 public List<File > getRoots() { 111 return rootFiles; 112 } 113 114 public List<File > getExclusions() { 115 return exclusions; 116 } 117 118 125 public File [] getFiles() { 126 return filteredFiles.toArray(new File [filteredFiles.size()]); 127 } 128 129 public File [] getRootFiles() { 130 return rootFiles.toArray(new File [rootFiles.size()]); 131 } 132 133 public boolean contains(File file) { 134 outter : for (Iterator i = rootFiles.iterator(); i.hasNext();) { 135 File root = (File ) i.next(); 136 if (SvnUtils.isParentOrEqual(root, file)) { 137 for (Iterator j = exclusions.iterator(); j.hasNext();) { 138 File excluded = (File ) j.next(); 139 if (SvnUtils.isParentOrEqual(excluded, file)) { 140 continue outter; 141 } 142 } 143 return true; 144 } 145 } 146 return false; 147 } 148 149 public static final List<File > getEmptyList() { 150 return Collections.emptyList(); 152 } 153 } 154 | Popular Tags |