View Javadoc
1   /*
2    *  Copyright 2012 Chris Pheby
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.jadira.scanner.classpath.filter;
17  
18  import java.util.HashSet;
19  import java.util.Set;
20  
21  import org.jadira.scanner.classpath.api.JElementFilter;
22  import org.jadira.scanner.classpath.types.JElement;
23  import org.jadira.scanner.classpath.types.JPackage;
24  import org.jadira.scanner.core.filter.AbstractFilter;
25  
26  public class PackageFilter extends AbstractFilter<JElement> implements JElementFilter {
27  
28  	private Set<String> packageNames = new HashSet<String>();
29  	
30  	public PackageFilter() {
31  	}
32  	
33  	public PackageFilter(Package... packages) {
34  		for (Package next : packages) {
35  			packageNames.add(next.getName());
36  		}
37  	}
38  	
39  	public PackageFilter(JPackage... packages) {
40  		for (JPackage next : packages) {
41  			packageNames.add(next.getName());
42  		}
43  	}
44  	
45  	public PackageFilter(String... packageNames) {
46  		for (String next : packageNames) {
47  			this.packageNames.add(next);
48  		}
49  	}
50  	
51  	@Override
52  	public boolean accept(JElement element) {
53  		
54  		final boolean shouldAccept;
55  		if (JPackage.class.isAssignableFrom(element.getClass())) {
56  			if (packageNames.isEmpty()) {
57  				shouldAccept = true;
58  			} else {
59  				if (packageNames.contains(((JPackage)element).getName())) {
60  					shouldAccept = true;
61  				} else {
62  					shouldAccept = false;
63  				}
64  			}
65  		} else {
66  			shouldAccept = false;
67  		}
68  		return shouldAccept;
69  	}
70  }