1 19 20 package org.netbeans.modules.queries; 21 22 import java.io.File ; 23 import java.util.Arrays ; 24 import java.util.HashSet ; 25 import java.util.Set ; 26 import org.netbeans.spi.queries.CollocationQueryImplementation; 27 28 35 public class AlwaysRelativeCollocationQuery implements CollocationQueryImplementation { 36 37 private File [] roots; 38 39 40 public AlwaysRelativeCollocationQuery() { 41 } 42 43 public File findRoot(File file) { 44 final File [] roots = getFileSystemRoots (); 45 if (roots.length == 0) { 46 assert false : "Cannot find filesystem roots"; 47 return null; 48 } 49 else if (roots.length == 1) { 50 return roots[0]; 52 } 53 else { 54 final Set <File > rootsSet = new HashSet <File >(Arrays.asList(this.roots != null ? this.roots : roots)); 55 return getRoot (file, rootsSet); 56 } 57 } 58 59 public boolean areCollocated(File file1, File file2) { 60 File root1 = findRoot (file1); 61 File root2 = findRoot (file2); 62 return root1 != null && root1.equals(root2); 63 } 64 65 67 private File [] getFileSystemRoots () { 68 if (this.roots != null) { 69 return this.roots; 70 } 71 else { 72 return File.listRoots(); 73 } 74 } 75 76 private File getRoot(File f, final Set <File > roots) { 77 while (f != null && !roots.contains(f)) { 82 f = f.getParentFile(); 83 } 84 return f; 85 } 86 87 final void setFileSystemRoots (File [] roots) { 88 this.roots = roots; 89 } 90 91 } 92 | Popular Tags |