1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }