KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > util > Context


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
25 import java.io.Serializable JavaDoc;
26 import java.util.*;
27
28 /**
29  * Encapsulates context of an action. There are two ways in which context may be defined:
30  * - list of files (f/b.txt, f/c.txt, f/e.txt)
31  * - list of roots (top folders) plus list of exclusions (f),(a.txt, d.txt)
32  *
33  * @author Maros Sandor
34  */

35 public class Context implements Serializable JavaDoc {
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 JavaDoc root = (File JavaDoc) i.next();
55             for (Iterator j = exclusions.iterator(); j.hasNext();) {
56                 File JavaDoc exclusion = (File JavaDoc) 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 JavaDoc file = (File JavaDoc) i.next();
73             for (Iterator j = newFiles.iterator(); j.hasNext();) {
74                 File JavaDoc includedFile = (File JavaDoc) 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 JavaDoc exclusion, File JavaDoc root) {
87         File JavaDoc [] exclusionChildren = exclusion.listFiles();
88         if (exclusionChildren == null) return;
89         for (int i = 0; i < exclusionChildren.length; i++) {
90             File JavaDoc child = exclusionChildren[i];
91             if (!Utils.isParentOrEqual(root, child)) {
92                 exclusions.add(child);
93             }
94         }
95     }
96
97     public Set<File JavaDoc> getExclusions() {
98         return exclusions;
99     }
100
101     /**
102      * Gets exact set of files to operate on, it is effectively defined as (rootFiles - exclusions). This set
103      * is NOT suitable for Update command because Update should operate on all rootFiles and just exclude some subfolders.
104      * Otherwise update misses new files and folders directly in rootFiles folders.
105      *
106      * @return files to operate on
107      */

108     public File JavaDoc [] getFiles() {
109         return (File JavaDoc[]) filteredFiles.toArray(new File JavaDoc[filteredFiles.size()]);
110     }
111
112     public File JavaDoc[] getRootFiles() {
113         return (File JavaDoc[]) rootFiles.toArray(new File JavaDoc[rootFiles.size()]);
114     }
115     
116     public boolean contains(File JavaDoc file) {
117         outter : for (Iterator i = rootFiles.iterator(); i.hasNext();) {
118             File JavaDoc root = (File JavaDoc) i.next();
119             if (Utils.isParentOrEqual(root, file)) {
120                 for (Iterator j = exclusions.iterator(); j.hasNext();) {
121                     File JavaDoc excluded = (File JavaDoc) j.next();
122                     if (Utils.isParentOrEqual(excluded, file)) {
123                         continue outter;
124                     }
125                 }
126                 return true;
127             }
128         }
129         return false;
130     }
131
132     /**
133      * Joins this context and the parameter context.
134      *
135      * @param ctx
136      * @return
137      */

138     public Context union(Context ctx) {
139         Set<File JavaDoc> newfilteredFiles = new HashSet<File JavaDoc>(filteredFiles);
140         Set<File JavaDoc> newrootFiles = new HashSet<File JavaDoc>(rootFiles);
141         Set<File JavaDoc> newexclusions = new HashSet<File JavaDoc>(exclusions);
142         
143         // TODO: postprocess filetered file list? it may be larger than necessary
144
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