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
018package org.apache.commons.chain.impl;
019
020import java.util.Iterator;
021import java.util.Map;
022import java.util.concurrent.ConcurrentHashMap;
023
024import org.apache.commons.chain.Catalog;
025import org.apache.commons.chain.CatalogFactory;
026import org.apache.commons.chain.Context;
027
028/**
029 * A simple implementation of {@link CatalogFactory}.
030 *
031 * @param <C> Type of the context associated with this command
032 *
033 * @author Sean Schofield
034 * @version $Revision$ $Date$
035 */
036public class CatalogFactoryBase<C extends Context> extends CatalogFactory<C> {
037
038    // ----------------------------------------------------------- Constructors
039
040    /**
041     * Construct an empty instance of {@link CatalogFactoryBase}. This
042     * constructor is intended solely for use by {@link CatalogFactory}.
043     */
044    public CatalogFactoryBase() {
045    }
046
047    // ----------------------------------------------------- Instance Variables
048
049    /**
050     * The default {@link Catalog} for this {@link CatalogFactory}.
051     */
052    private Catalog<C> catalog = null;
053
054    /**
055     * Map of named {@link Catalog}s, keyed by catalog name.
056     */
057    private final Map<String, Catalog<C>> catalogs = new ConcurrentHashMap<>();
058
059    // --------------------------------------------------------- Public Methods
060
061    /**
062     * Gets the default instance of Catalog associated with the factory
063     * (if any); otherwise, return {@code null}.
064     *
065     * @return the default Catalog instance
066     */
067    @Override
068    public Catalog<C> getCatalog() {
069        return catalog;
070    }
071
072    /**
073     * Sets the default instance of Catalog associated with the factory.
074     *
075     * @param catalog the default Catalog instance
076     */
077    @Override
078    public void setCatalog(Catalog<C> catalog) {
079        this.catalog = catalog;
080    }
081
082    /**
083     * Retrieves a Catalog instance by name (if any); otherwise
084     * return {@code null}.
085     *
086     * @param name the name of the Catalog to retrieve
087     *
088     * @return the specified Catalog
089     */
090    @Override
091    public Catalog<C> getCatalog(String name) {
092        return catalogs.get(name);
093    }
094
095    /**
096     * Adds a named instance of Catalog to the factory (for subsequent
097     * retrieval later).
098     *
099     * @param name the name of the Catalog to add
100     * @param catalog the Catalog to add
101     */
102    @Override
103    public void addCatalog(String name, Catalog<C> catalog) {
104        catalogs.put(name, catalog);
105    }
106
107    /**
108     * Return an {@code Iterator} over the set of named
109     * {@link Catalog}s known to this {@link CatalogFactory}.
110     * If there are no known catalogs, an empty Iterator is returned.
111     *
112     * @return An Iterator of the names of the Catalogs known by this factory.
113     */
114    @Override
115    public Iterator<String> getNames() {
116        return catalogs.keySet().iterator();
117    }
118}