View Javadoc

1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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      // ----------------------------------------------------- Instance Variables
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      // --------------------------------------------------------- Public Methods
51  
52  
53      // Documented in Catalog interface
54      public void addCommand(String name, Command command) {
55  
56          commands.put(name, command);
57  
58      }
59  
60      // Documented in Catalog interface
61      public Command getCommand(String name) {
62  
63          return ((Command) commands.get(name));
64  
65      }
66  
67  
68      // Documented in Catalog interface
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  }