1 19 package org.netbeans.modules.versioning.util; 20 21 import java.io.File ; 22 import java.util.*; 23 import java.util.prefs.Preferences ; 24 25 30 public class FileCollection { 31 32 private static final char FLAT_FOLDER_MARKER = '*'; 33 34 private final Set<File > storage = new HashSet<File >(1); 35 36 public synchronized void load(Preferences prefs, String key) { 37 List<String > paths = Utils.getStringList(prefs, key); 38 storage.clear(); 39 for (String path : paths) { 40 if (path.charAt(0) == FLAT_FOLDER_MARKER) { 41 storage.add(new FlatFolder(path.substring(1))); 42 } else { 43 storage.add(new File (path)); 44 } 45 } 46 } 47 48 public synchronized void save(Preferences prefs, String key) { 49 List<String > paths = new ArrayList<String >(storage.size()); 50 for (File file : storage) { 51 if (file instanceof FlatFolder) { 52 paths.add(FLAT_FOLDER_MARKER + file.getAbsolutePath()); 53 } else { 54 paths.add(file.getAbsolutePath()); 55 } 56 } 57 Utils.put(prefs, key, paths); 58 } 59 60 66 public synchronized boolean contains(File file) { 67 for (File element : storage) { 68 if (Utils.isParentOrEqual(element, file)) return true; 69 } 70 return false; 71 } 72 73 79 public synchronized void add(File file) { 80 for (Iterator<File > i = storage.iterator(); i.hasNext(); ) { 81 File element = i.next(); 82 if (Utils.isParentOrEqual(element, file)) return; 83 if (Utils.isParentOrEqual(file, element)) { 84 i.remove(); 85 } 86 } 87 storage.add(file); 88 } 89 90 95 public synchronized void remove(File file) { 96 for (Iterator<File > i = storage.iterator(); i.hasNext(); ) { 97 File element = i.next(); 98 if (Utils.isParentOrEqual(element, file) || Utils.isParentOrEqual(file, element)) { 99 i.remove(); 100 } 101 } 102 } 103 } 104 | Popular Tags |