001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.versioning.util;
042:
043: import org.netbeans.modules.versioning.spi.VersioningSupport;
044:
045: import java.io.File;
046: import java.util.*;
047: import java.util.prefs.Preferences;
048:
049: /**
050: * Collection of Files that has special contracts for add, remove and contains methods, see below.
051: *
052: * @author Maros Sandor
053: */
054: public class FileCollection {
055:
056: private static final char FLAT_FOLDER_MARKER = '*';
057:
058: private final Set<File> storage = new HashSet<File>(1);
059:
060: public synchronized void load(Preferences prefs, String key) {
061: List<String> paths = Utils.getStringList(prefs, key);
062: storage.clear();
063: for (String path : paths) {
064: if (path.charAt(0) == FLAT_FOLDER_MARKER) {
065: storage.add(VersioningSupport
066: .getFlat(path.substring(1)));
067: } else {
068: storage.add(new File(path));
069: }
070: }
071: }
072:
073: public synchronized void save(Preferences prefs, String key) {
074: List<String> paths = new ArrayList<String>(storage.size());
075: for (File file : storage) {
076: if (VersioningSupport.isFlat(file)) {
077: paths.add(FLAT_FOLDER_MARKER + file.getAbsolutePath());
078: } else {
079: paths.add(file.getAbsolutePath());
080: }
081: }
082: Utils.put(prefs, key, paths);
083: }
084:
085: /**
086: * A file is contained in the collection either if it is in the colelction itself or there is any of its parents.
087: *
088: * @param file a file to query
089: * @return true if the file is contained in the collection, false otherwise
090: */
091: public synchronized boolean contains(File file) {
092: for (File element : storage) {
093: if (Utils.isAncestorOrEqual(element, file))
094: return true;
095: }
096: return false;
097: }
098:
099: /**
100: * Adds a file to the collection. If any of its parent files is already in the collection, the file is NOT added.
101: * All children of the supplied file are removed from the collection.
102: *
103: * @param file a file to add
104: */
105: public synchronized void add(File file) {
106: for (Iterator<File> i = storage.iterator(); i.hasNext();) {
107: File element = i.next();
108: if (Utils.isAncestorOrEqual(element, file))
109: return;
110: if (Utils.isAncestorOrEqual(file, element)) {
111: i.remove();
112: }
113: }
114: storage.add(file);
115: }
116:
117: /**
118: * Removes a file from the collection. This method also removes all its parents and also all its children.
119: *
120: * @param file a file to remove
121: */
122: public synchronized void remove(File file) {
123: for (Iterator<File> i = storage.iterator(); i.hasNext();) {
124: File element = i.next();
125: if (Utils.isAncestorOrEqual(element, file)
126: || Utils.isAncestorOrEqual(file, element)) {
127: i.remove();
128: }
129: }
130: }
131: }
|