001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.chain.impl;
017:
018: import java.util.HashMap;
019: import java.util.Collections;
020: import java.util.Iterator;
021: import java.util.Map;
022: import org.apache.commons.chain.Catalog;
023: import org.apache.commons.chain.Command;
024:
025: /**
026: * <p>Simple in-memory implementation of {@link Catalog}. This class can
027: * also be used as the basis for more advanced implementations.</p>
028: *
029: * <p>This implementation is thread-safe.</p>
030: *
031: * @author Craig R. McClanahan
032: * @author Matthew J. Sgarlata
033: * @version $Revision: 411876 $ $Date: 2006-06-05 19:02:19 +0100 (Mon, 05 Jun 2006) $
034: */
035:
036: public class CatalogBase implements Catalog {
037:
038: // ----------------------------------------------------- Instance Variables
039:
040: /**
041: * <p>The map of named {@link Command}s, keyed by name.
042: */
043: protected Map commands = Collections.synchronizedMap(new HashMap());
044:
045: // --------------------------------------------------------- Constructors
046:
047: /**
048: * Create an empty catalog.
049: */
050: public CatalogBase() {
051: }
052:
053: /**
054: * <p>Create a catalog whose commands are those specified in the given <code>Map</code>.
055: * All Map keys should be <code>String</code> and all values should be <code>Command</code>.</p>
056: *
057: * @param commands Map of Commands.
058: *
059: * @since Chain 1.1
060: */
061: public CatalogBase(Map commands) {
062: this .commands = Collections.synchronizedMap(commands);
063: }
064:
065: // --------------------------------------------------------- Public Methods
066:
067: /**
068: * <p>Add a new name and associated {@link Command}
069: * to the set of named commands known to this {@link Catalog},
070: * replacing any previous command for that name.
071: *
072: * @param name Name of the new command
073: * @param command {@link Command} to be returned
074: * for later lookups on this name
075: */
076: public void addCommand(String name, Command command) {
077:
078: commands.put(name, command);
079:
080: }
081:
082: /**
083: * <p>Return the {@link Command} associated with the
084: * specified name, if any; otherwise, return <code>null</code>.</p>
085: *
086: * @param name Name for which a {@link Command}
087: * should be retrieved
088: * @return The Command associated with the specified name.
089: */
090: public Command getCommand(String name) {
091:
092: return ((Command) commands.get(name));
093:
094: }
095:
096: /**
097: * <p>Return an <code>Iterator</code> over the set of named commands
098: * known to this {@link Catalog}. If there are no known commands,
099: * an empty Iterator is returned.</p>
100: * @return An iterator of the names in this Catalog.
101: */
102: public Iterator getNames() {
103:
104: return (commands.keySet().iterator());
105:
106: }
107:
108: /**
109: * Converts this Catalog to a String. Useful for debugging purposes.
110: * @return a representation of this catalog as a String
111: */
112: public String toString() {
113:
114: Iterator names = getNames();
115: StringBuffer str = new StringBuffer("["
116: + this .getClass().getName() + ": ");
117:
118: while (names.hasNext()) {
119: str.append(names.next());
120: if (names.hasNext()) {
121: str.append(", ");
122: }
123: }
124: str.append("]");
125:
126: return str.toString();
127:
128: }
129: }
|