1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.chain.impl;
17
18
19 import java.util.HashMap;
20 import java.util.Collections;
21 import java.util.Iterator;
22 import java.util.Map;
23 import org.apache.commons.chain.Catalog;
24 import org.apache.commons.chain.Command;
25
26
27 /***
28 * <p>Simple in-memory implementation of {@link Catalog}. This class can
29 * also be used as the basis for more advanced implementations.</p>
30 *
31 * <p>This implementation is thread-safe.</p>
32 *
33 * @author Craig R. McClanahan
34 * @author Matthew J. Sgarlata
35 * @version $Revision: 1.12 $ $Date: 2004/11/30 05:52:23 $
36 */
37
38 public class CatalogBase implements Catalog {
39
40
41
42
43
44 /***
45 * <p>The map of named {@link Command}s, keyed by name.
46 */
47 protected Map commands = Collections.synchronizedMap(new HashMap());
48
49
50
51
52
53
54 public void addCommand(String name, Command command) {
55
56 commands.put(name, command);
57
58 }
59
60
61 public Command getCommand(String name) {
62
63 return ((Command) commands.get(name));
64
65 }
66
67
68
69 public Iterator getNames() {
70
71 return (commands.keySet().iterator());
72
73 }
74
75 /***
76 * Converts this Catalog to a String. Useful for debugging purposes.
77 * @return a representation of this catalog as a String
78 */
79 public String toString() {
80
81 Iterator names = getNames();
82 StringBuffer str =
83 new StringBuffer("[" + this.getClass().getName() + ": ");
84
85 while (names.hasNext()) {
86 str.append(names.next());
87 if (names.hasNext()) {
88 str.append(", ");
89 }
90 }
91 str.append("]");
92
93 return str.toString();
94
95 }
96 }