/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 This library is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as
 published by the Free Software Foundation; either version 2.1 of the
 License, or (at your option) any later version.

 This library 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
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.project;

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

/**
 * Singleton mediator class for managing setting and getting of all project data
 * to and from the respective handlers. All instances of
 * ProjectDataHandler must register with this class at program
 * start.
 * 
 * @author Markus Kraetzig 
 */
public final class ProjectDataManager {
    private static final ProjectDataManager INSTANCE = new ProjectDataManager();

    private final Map handlerMap = new HashMap();

    private ProjectDataManager() {
        // not to be invoked
    }

    /**
     * Gets the singleton instance of this class.
     * 
     * @return project data manager
     */
    public static ProjectDataManager getInstance() {
        return INSTANCE;
    }

    /**
     * Sets the project data to the respective handler.
     * 
     * @param data
     *            the project data to set
     * @throws IllegalArgumentException
     *             if (data == null)
     */
    public void setProjectData(ProjectData data) {
        if (data == null)
            throw new IllegalArgumentException("Argument was null.");
        ProjectDataHandler handler = handlerMap.get(data.getProjectDataType());
        handler.setProjectData(data);
    }

    /**
     * Gets an array with all project data from the data handlers that have been
     * registered with this manager.
     * 
     * @return project data array
     */
    public ProjectData[] getProjectData() {
        ProjectData[] rtn = new ProjectData[handlerMap.size()];
        int i = 0;
        for (Iterator iter = handlerMap.values().iterator(); iter.hasNext();) {
            ProjectDataHandler element = (ProjectDataHandler) iter.next();
            rtn[i++] = element.getProjectData();
        }
        return rtn;
    }

    /**
     * Registers a project data handler with this manager class. Only one
     * handler is allowed for each instance of ProjectDataTypes.
     * 
     * @param handler
     *            data handler object that should take part in the project
     *            management system
     * @throws IllegalArgumentException
     *             if (handler == null)
     * @throws IllegalStateException
     *             if another listener has already been registered for the same
     *             project data type
     */
    public void register(ProjectDataHandler handler) {
        if (handler == null)
            throw new IllegalArgumentException("Argument was null.");

        if (handlerMap.containsKey(handler.getProjectDataType()))
            throw new IllegalStateException(
                    "An instance of ProjectDataHandler for the type \""
                            + handler.getProjectDataType()
                            + "\" has already been registered.");
        handlerMap.put(handler.getProjectDataType(), handler);

    }
}

 

Ads help maintain this website.