001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.chain.impl;
018    
019    
020    import java.util.HashMap;
021    import java.util.Collections;
022    import java.util.Iterator;
023    import java.util.Map;
024    import org.apache.commons.chain.Catalog;
025    import org.apache.commons.chain.Command;
026    
027    
028    /**
029     * <p>Simple in-memory implementation of {@link Catalog}.  This class can
030     * also be used as the basis for more advanced implementations.</p>
031     *
032     * <p>This implementation is thread-safe.</p>
033     *
034     * @author Craig R. McClanahan
035     * @author Matthew J. Sgarlata
036     * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $
037     */
038    
039    public class CatalogBase implements Catalog {
040    
041    
042        // ----------------------------------------------------- Instance Variables
043    
044    
045        /**
046         * <p>The map of named {@link Command}s, keyed by name.
047         */
048        protected Map commands = Collections.synchronizedMap(new HashMap());
049    
050    
051        // --------------------------------------------------------- Constructors
052    
053        /**
054         * Create an empty catalog.
055         */
056        public CatalogBase() { }
057    
058        /**
059         * <p>Create a catalog whose commands are those specified in the given <code>Map</code>.
060         * All Map keys should be <code>String</code> and all values should be <code>Command</code>.</p>
061         *
062         * @param commands Map of Commands.
063         *
064         * @since Chain 1.1
065         */
066        public CatalogBase( Map commands ) {
067            this.commands = Collections.synchronizedMap(commands);
068        }
069    
070        // --------------------------------------------------------- Public Methods
071    
072    
073        /**
074         * <p>Add a new name and associated {@link Command}
075         * to the set of named commands known to this {@link Catalog},
076         * replacing any previous command for that name.
077         *
078         * @param name Name of the new command
079         * @param command {@link Command} to be returned
080         *  for later lookups on this name
081         */
082        public void addCommand(String name, Command command) {
083    
084            commands.put(name, command);
085    
086        }
087    
088        /**
089         * <p>Return the {@link Command} associated with the
090         * specified name, if any; otherwise, return <code>null</code>.</p>
091         *
092         * @param name Name for which a {@link Command}
093         *  should be retrieved
094         * @return The Command associated with the specified name.
095         */
096        public Command getCommand(String name) {
097    
098            return ((Command) commands.get(name));
099    
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    }