View Javadoc

1   package liquibase.servicelocator;
2   
3   import java.util.HashSet;
4   import java.util.Set;
5   
6   /**
7    * Package scan filter for testing if a given class is assignable to another class.
8    */
9   public class AssignableToPackageScanFilter implements PackageScanFilter {
10      private final Set<Class<?>> parents = new HashSet<Class<?>>();
11  
12      public AssignableToPackageScanFilter() {
13      }
14  
15      public AssignableToPackageScanFilter(Class<?> parentType) {
16          parents.add(parentType);
17      }
18  
19      public AssignableToPackageScanFilter(Set<Class<?>> parents) {
20          this.parents.addAll(parents);
21      }
22  
23      public void addParentType(Class<?> parentType) {
24          parents.add(parentType);
25      }
26  
27      public boolean matches(Class<?> type) {
28          if (parents != null && parents.size() > 0) {
29              for (Class<?> parent : parents) {
30                  if (parent.isAssignableFrom(type)) {
31                      return true;
32                  }
33              }
34          }
35          return false;
36      }
37  
38      @Override
39      public String toString() {
40          StringBuilder sb = new StringBuilder();
41          for (Class<?> parent : parents) {
42              sb.append(parent.getSimpleName()).append(", ");
43          }
44          sb.setLength(sb.length() > 0 ? sb.length() - 2 : 0);
45          return "is assignable to " + sb;
46      }
47  }