KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > util > FileCollection


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.versioning.util;
20
21 import java.io.File JavaDoc;
22 import java.util.*;
23 import java.util.prefs.Preferences JavaDoc;
24
25 /**
26  * Collection of Files that has special contracts for add, remove and contains methods, see below.
27  *
28  * @author Maros Sandor
29  */

30 public class FileCollection {
31     
32     private static final char FLAT_FOLDER_MARKER = '*';
33     
34     private final Set<File JavaDoc> storage = new HashSet<File JavaDoc>(1);
35     
36     public synchronized void load(Preferences JavaDoc prefs, String JavaDoc key) {
37         List<String JavaDoc> paths = Utils.getStringList(prefs, key);
38         storage.clear();
39         for (String JavaDoc 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 JavaDoc(path));
44             }
45         }
46     }
47
48     public synchronized void save(Preferences JavaDoc prefs, String JavaDoc key) {
49         List<String JavaDoc> paths = new ArrayList<String JavaDoc>(storage.size());
50         for (File JavaDoc 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     /**
61      * A file is contained in the collection either if it is in the colelction itself or there is any of its parents.
62      *
63      * @param file a file to query
64      * @return true if the file is contained in the collection, false otherwise
65      */

66     public synchronized boolean contains(File JavaDoc file) {
67         for (File JavaDoc element : storage) {
68             if (Utils.isParentOrEqual(element, file)) return true;
69         }
70         return false;
71     }
72
73     /**
74      * Adds a file to the collection. If any of its parent files is already in the collection, the file is NOT added.
75      * All children of the supplied file are removed from the collection.
76      *
77      * @param file a file to add
78      */

79     public synchronized void add(File JavaDoc file) {
80         for (Iterator<File JavaDoc> i = storage.iterator(); i.hasNext(); ) {
81             File JavaDoc 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     /**
91      * Removes a file from the collection. This method also removes all its parents and also all its children.
92      *
93      * @param file a file to remove
94      */

95     public synchronized void remove(File JavaDoc file) {
96         for (Iterator<File JavaDoc> i = storage.iterator(); i.hasNext(); ) {
97             File JavaDoc element = i.next();
98             if (Utils.isParentOrEqual(element, file) || Utils.isParentOrEqual(file, element)) {
99                 i.remove();
100             }
101         }
102     }
103 }
104
Popular Tags