/* $Id: ModuleLabels.java,v 1.9 2005/12/09 18:23:05 m31 Exp $
 *
 * This file is part of the project "Hilbert II" - http://www.qedeq.org
 *
 * Copyright 2000-2005,  Michael Meyling <mime@qedeq.org>.
 *
 * "Hilbert II" is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */

package org.qedeq.kernel.bo.control;

import java.util.HashMap;
import java.util.Map;

import org.qedeq.kernel.bo.module.NodeBo;


/**
 * Maps labels of an module to their elements.
 *
 * @version $Revision: 1.9 $
 * @author  Michael Meyling
 */
public final class ModuleLabels {

    /** Maps labels to business objects. */
    private final Map label2Bo;

    /** Maps labels to context of business objects. */
    private final Map label2Context;

    /**
     * Constructs a new empty module label list.
     */
    public ModuleLabels() {
        label2Bo = new HashMap();
        label2Context = new HashMap();
    }

    /**
     * Add node with certain id.
     *
     * @param   node    For this node.
     * @param   context With this context.
     * @throws  IllegalModuleDataException  The <code>id</code> already exists or is
     *          <code>null</code>.
     */
    public final void addNode(final Context context,  final NodeBo node)
            throws IllegalModuleDataException {
        if (null == node.getId()) {
            throw new IllegalModuleDataException(10001, "An id was not defined.", context, null,
                null);
        }
        if (label2Bo.containsKey(node.getId())) {
            throw new IllegalModuleDataException(10001, "Id \"" + node.getId()
                + "\" defined more than once.", context,
                (Context) label2Context.get(node.getId()), null);
        }
        label2Bo.put(node.getId(), node);
        // don't forget to use the copy constructor because the context could change!
        label2Context.put(node.getId(), new Context(context));
    }

    /**
     * Get node for given label.
     *
     * @param   label   Label to search node for.
     * @return  Node for given label. Maybe <code>null</code>.
     */
    public final NodeBo getNode(final String label) {
        return (NodeBo) label2Bo.get(label);
    }

}
