1 /*
2 * Copyright (c) 2001, 2002 The XDoclet team
3 * All rights reserved.
4 */
5 package xdoclet.util.predicates;
6
7 import org.apache.commons.collections.Predicate;
8
9 import java.util.HashMap;
10 import java.util.Map;
11
12 /***
13 * Simple predicate factory. Should be subclassed to add domain-specific predicates.
14 *
15 * @see And
16 * @see Or
17 * @see Not
18 * @see True
19 * @see False
20 * @see HasClassTag
21 * @see IsA
22 *
23 * @author <a href="aslak.hellesoy at netcom.no">Aslak Hellesøy</a>
24 */
25 public class SimplePredicateFactory implements PredicateFactory {
26 private final Map _predicates = new HashMap(7);
27
28 /***
29 * Constructs a new SimplePredicateFactory.
30 */
31 public SimplePredicateFactory() {
32 // general predicates
33 register("and", And.class);
34 register("or", Or.class);
35 register("not", Not.class);
36 register("true", True.class);
37 register("false", False.class);
38
39 // these deal with classes
40 register("has-class-tag", HasClassTag.class);
41 register("is-a", IsA.class);
42 }
43
44 /***
45 * Creates a predicate instance. Valid values for name are <code>and</code>,
46 * <code>or</code>, <code>not</code>, <code>true</code>, <code>false</code>,
47 * <code>has-class-tag</code> and <code>is-a</code>.
48 *
49 * @param name the predicate name
50 * @return an instance of the requested predicate
51 * @throws PredicateException If the requested predicate is not registered.
52 */
53 public Predicate createPredicate(String name)
54 throws PredicateException {
55 if (name == null) {
56 throw new IllegalArgumentException("name cannot be null");
57 }
58
59 try {
60 // Get the class from the registered predicates
61 Class clazz = (Class) _predicates.get(name.toLowerCase());
62
63 if (clazz == null) {
64 throw new PredicateException("No known predicate named " + name);
65 }
66
67 // Instantiate the predicate and return it
68 return (Predicate) clazz.newInstance();
69 } catch (IllegalAccessException e) {
70 throw new PredicateException(e.getMessage(), e);
71 } catch (InstantiationException e) {
72 throw new PredicateException(e.getMessage(), e);
73 }
74 }
75
76 /***
77 * Registers the predicate in the map.
78 *
79 * @param name the predicate name
80 * @param predicateClass the class used for this predicate
81 */
82 private void register(String name, Class predicateClass) {
83 assert (name != null) && (predicateClass != null);
84 _predicates.put(name, predicateClass);
85 }
86 }
This page was automatically generated by Maven