1 19 20 package org.netbeans.modules.versioning.system.cvss.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(Collections.EMPTY_SET, Collections.EMPTY_SET, Collections.EMPTY_SET); 38 39 private static final long serialVersionUID = 1L; 40 41 private final Set filteredFiles; 42 private final Set rootFiles; 43 private final Set exclusions; 44 45 public Context(Set filteredFiles, Set rootFiles, Set exclusions) { 46 this.filteredFiles = filteredFiles; 47 this.rootFiles = rootFiles; 48 this.exclusions = exclusions; 49 while (normalize()); 50 } 51 52 private boolean normalize() { 53 for (Iterator i = rootFiles.iterator(); i.hasNext();) { 54 File root = (File ) i.next(); 55 for (Iterator j = exclusions.iterator(); j.hasNext();) { 56 File exclusion = (File ) j.next(); 57 if (Utils.isParentOrEqual(exclusion, root)) { 58 j.remove(); 59 exclusionRemoved(exclusion, root); 60 return true; 61 } 62 } 63 } 64 removeDuplicates(rootFiles); 65 removeDuplicates(exclusions); 66 return false; 67 } 68 69 private void removeDuplicates(Set files) { 70 List newFiles = new ArrayList(); 71 outter: for (Iterator i = files.iterator(); i.hasNext();) { 72 File file = (File ) i.next(); 73 for (Iterator j = newFiles.iterator(); j.hasNext();) { 74 File includedFile = (File ) j.next(); 75 if (Utils.isParentOrEqual(includedFile, file) && (file.isFile() || !(includedFile instanceof FlatFolder))) continue outter; 76 if (Utils.isParentOrEqual(file, includedFile) && (includedFile.isFile() || !(file instanceof FlatFolder))) { 77 j.remove(); 78 } 79 } 80 newFiles.add(file); 81 } 82 files.clear(); 83 files.addAll(newFiles); 84 } 85 86 private void exclusionRemoved(File exclusion, File root) { 87 File [] exclusionChildren = exclusion.listFiles(); 88 if (exclusionChildren == null) return; 89 for (int i = 0; i < exclusionChildren.length; i++) { 90 File child = exclusionChildren[i]; 91 if (!Utils.isParentOrEqual(root, child)) { 92 exclusions.add(child); 93 } 94 } 95 } 96 97 public Set<File > getExclusions() { 98 return exclusions; 99 } 100 101 108 public File [] getFiles() { 109 return (File []) filteredFiles.toArray(new File [filteredFiles.size()]); 110 } 111 112 public File [] getRootFiles() { 113 return (File []) rootFiles.toArray(new File [rootFiles.size()]); 114 } 115 116 public boolean contains(File file) { 117 outter : for (Iterator i = rootFiles.iterator(); i.hasNext();) { 118 File root = (File ) i.next(); 119 if (Utils.isParentOrEqual(root, file)) { 120 for (Iterator j = exclusions.iterator(); j.hasNext();) { 121 File excluded = (File ) j.next(); 122 if (Utils.isParentOrEqual(excluded, file)) { 123 continue outter; 124 } 125 } 126 return true; 127 } 128 } 129 return false; 130 } 131 132 138 public Context union(Context ctx) { 139 Set<File > newfilteredFiles = new HashSet<File >(filteredFiles); 140 Set<File > newrootFiles = new HashSet<File >(rootFiles); 141 Set<File > newexclusions = new HashSet<File >(exclusions); 142 143 newfilteredFiles.addAll(ctx.filteredFiles); 145 newrootFiles.addAll(ctx.rootFiles); 146 newexclusions.addAll(ctx.exclusions); 147 148 Context nc = new Context(newfilteredFiles, newrootFiles, newexclusions); 149 return nc; 150 } 151 } 152 | Popular Tags |