View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.chain.impl;
18  
19  
20  import java.util.HashMap;
21  import java.util.Collections;
22  import java.util.Iterator;
23  import java.util.Map;
24  import org.apache.commons.chain.Catalog;
25  import org.apache.commons.chain.Command;
26  
27  
28  /**
29   * <p>Simple in-memory implementation of {@link Catalog}.  This class can
30   * also be used as the basis for more advanced implementations.</p>
31   *
32   * <p>This implementation is thread-safe.</p>
33   *
34   * @author Craig R. McClanahan
35   * @author Matthew J. Sgarlata
36   * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $
37   */
38  
39  public class CatalogBase implements Catalog {
40  
41  
42      // ----------------------------------------------------- Instance Variables
43  
44  
45      /**
46       * <p>The map of named {@link Command}s, keyed by name.
47       */
48      protected Map commands = Collections.synchronizedMap(new HashMap());
49  
50  
51      // --------------------------------------------------------- Constructors
52  
53      /**
54       * Create an empty catalog.
55       */
56      public CatalogBase() { }
57  
58      /**
59       * <p>Create a catalog whose commands are those specified in the given <code>Map</code>.
60       * All Map keys should be <code>String</code> and all values should be <code>Command</code>.</p>
61       *
62       * @param commands Map of Commands.
63       *
64       * @since Chain 1.1
65       */
66      public CatalogBase( Map commands ) {
67          this.commands = Collections.synchronizedMap(commands);
68      }
69  
70      // --------------------------------------------------------- Public Methods
71  
72  
73      /**
74       * <p>Add a new name and associated {@link Command}
75       * to the set of named commands known to this {@link Catalog},
76       * replacing any previous command for that name.
77       *
78       * @param name Name of the new command
79       * @param command {@link Command} to be returned
80       *  for later lookups on this name
81       */
82      public void addCommand(String name, Command command) {
83  
84          commands.put(name, command);
85  
86      }
87  
88      /**
89       * <p>Return the {@link Command} associated with the
90       * specified name, if any; otherwise, return <code>null</code>.</p>
91       *
92       * @param name Name for which a {@link Command}
93       *  should be retrieved
94       * @return The Command associated with the specified name.
95       */
96      public Command getCommand(String name) {
97  
98          return ((Command) commands.get(name));
99  
100     }
101 
102 
103     /**
104      * <p>Return an <code>Iterator</code> over the set of named commands
105      * known to this {@link Catalog}.  If there are no known commands,
106      * an empty Iterator is returned.</p>
107      * @return An iterator of the names in this Catalog.
108      */
109     public Iterator getNames() {
110 
111         return (commands.keySet().iterator());
112 
113     }
114 
115     /**
116      * Converts this Catalog to a String.  Useful for debugging purposes.
117      * @return a representation of this catalog as a String
118      */
119     public String toString() {
120 
121         Iterator names = getNames();
122         StringBuffer str =
123             new StringBuffer("[" + this.getClass().getName() + ": ");
124 
125         while (names.hasNext()) {
126             str.append(names.next());
127             if (names.hasNext()) {
128             str.append(", ");
129             }
130         }
131         str.append("]");
132 
133         return str.toString();
134 
135     }
136 }