ref #10096 use longer form for TaxonNodeStatus labels
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / CdmAssignableTypeFilter.java
1 /**
2 * Copyright (C) 2014 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.cdm.model;
10
11 import org.springframework.core.type.filter.AbstractTypeHierarchyTraversingFilter;
12
13 /**
14 * Special implementation for the cdm which allows to also match
15 * abstract base types and interfaces in eu.etaxonomy.cdm.model.
16 *
17 *
18 * @author a.kohlbecker
19 * @since Jul 31, 2014
20 *
21 */
22 public class CdmAssignableTypeFilter extends AbstractTypeHierarchyTraversingFilter {
23
24
25 private final Class targetType;
26
27 /**
28 * @param targetType
29 */
30 public CdmAssignableTypeFilter(Class targetType, boolean considerInherited, boolean considerInterfaces) {
31 super(considerInherited, considerInterfaces);
32 this.targetType = targetType;
33 }
34
35 @Override
36 protected boolean matchClassName(String className) {
37 return this.targetType.getName().equals(className);
38 }
39
40 @Override
41 protected Boolean matchSuperClass(String superClassName) {
42 return matchTargetType(superClassName);
43 }
44
45 @Override
46 protected Boolean matchInterface(String interfaceName) {
47 return matchTargetType(interfaceName);
48 }
49
50
51 protected Boolean matchTargetType(String typeName) {
52 if (this.targetType.getName().equals(typeName)) {
53 return true;
54 }
55 else if (Object.class.getName().equals(typeName)) {
56 return Boolean.FALSE;
57 }
58 else if (typeName.startsWith("eu.etaxonomy.cdm.model.")) {
59 try {
60 Class clazz = getClass().getClassLoader().loadClass(typeName);
61 return Boolean.valueOf(this.targetType.isAssignableFrom(clazz));
62 }
63 catch (ClassNotFoundException ex) {
64 // Class not found - can't determine a match that way.
65 }
66 }
67 return null;
68 }
69
70 }