Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2009 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* If you wish your version of this file to be governed by only the CDDL |
25 |
* or only the GPL Version 2, indicate your decision by adding |
26 |
* "[Contributor] elects to include this software in this distribution |
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
28 |
* single choice of license, a recipient has the option to distribute |
29 |
* your version of this file under either the CDDL, the GPL Version 2 or |
30 |
* to extend the choice of license to its licensees as provided above. |
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
32 |
* Version 2 license, then the option applies only if the new code is |
33 |
* made subject to such option by the copyright holder. |
34 |
* |
35 |
* Contributor(s): |
36 |
* |
37 |
* Samuel Halliday |
38 |
* |
39 |
* Portions Copyrighted 2009 Sun Microsystems, Inc. |
40 |
*/ |
41 |
package org.netbeans.modules.editor.java; |
42 |
|
43 |
import java.util.ArrayList; |
44 |
import java.util.Collection; |
45 |
import java.util.concurrent.locks.ReadWriteLock; |
46 |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
47 |
|
48 |
/** |
49 |
* A whitelist/blacklist of excluded classes and packages for the Java completer. |
50 |
* Requested in RFE #125060. |
51 |
* |
52 |
* @author Samuel Halliday |
53 |
*/ |
54 |
final class ExcludeCompletion { |
55 |
|
56 |
private final Collection<String> exclude = new ArrayList<String>(); |
57 |
private final Collection<String> include = new ArrayList<String>(); |
58 |
private final ReadWriteLock lock = new ReentrantReadWriteLock(); |
59 |
|
60 |
/** */ |
61 |
public ExcludeCompletion() { |
62 |
} |
63 |
|
64 |
/** |
65 |
* @param fqn Fully Qualified Name |
66 |
* @return |
67 |
*/ |
68 |
public boolean isExcluded(final CharSequence fqn) { |
69 |
//#122334: do not propose imports from the default package |
70 |
if (fqn == null || fqn.length() == 0) { |
71 |
return true; |
72 |
} |
73 |
|
74 |
String s = fqn.toString(); |
75 |
lock.readLock().lock(); |
76 |
try { |
77 |
if (!include.isEmpty()) { |
78 |
for (String entry : include) { |
79 |
if (entry.length() > fqn.length()) { |
80 |
if (entry.startsWith(s)) { |
81 |
return false; |
82 |
} |
83 |
} else if (s.startsWith(entry)) { |
84 |
return false; |
85 |
} |
86 |
} |
87 |
} |
88 |
|
89 |
if (!exclude.isEmpty()) { |
90 |
for (String entry : exclude) { |
91 |
if (entry.endsWith(".") && fqn.equals(entry.substring(0, entry.length() - 1))) { // NOI18N |
92 |
// exclude packages names (no trailing fullstop) |
93 |
return true; |
94 |
} |
95 |
if (entry.length() > fqn.length()) { |
96 |
// fqn not long enough to filter yet |
97 |
continue; |
98 |
} |
99 |
if (s.startsWith(entry)) { |
100 |
return true; |
101 |
} |
102 |
} |
103 |
} |
104 |
|
105 |
return false; |
106 |
} finally { |
107 |
lock.readLock().unlock(); |
108 |
} |
109 |
} |
110 |
|
111 |
/** |
112 |
* @param blacklist comma separated list of fqns |
113 |
*/ |
114 |
public void updateBlacklist(String blacklist) { |
115 |
update(exclude, blacklist); |
116 |
} |
117 |
|
118 |
/** |
119 |
* @param whitelist comma separated list of fqns |
120 |
*/ |
121 |
public void updateWhitelist(String whitelist) { |
122 |
update(include, whitelist); |
123 |
} |
124 |
|
125 |
private void update(Collection<String> existing, String updated) { |
126 |
lock.writeLock().lock(); |
127 |
try { |
128 |
existing.clear(); |
129 |
if (updated == null || updated.length() == 0) { |
130 |
return; |
131 |
} |
132 |
String[] entries = updated.split(","); //NOI18N |
133 |
for (String entry : entries) { |
134 |
if (entry != null && entry.length() != 0) { |
135 |
existing.add(entry); |
136 |
} |
137 |
} |
138 |
} finally { |
139 |
lock.writeLock().unlock(); |
140 |
} |
141 |
} |
142 |
} |