net.scriptability.core.configuration.Configuration.java Source code

Java tutorial

Introduction

Here is the source code for net.scriptability.core.configuration.Configuration.java

Source

/*
 * Copyright 2013 Hayden Bakkum
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package net.scriptability.core.configuration;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.scriptability.core.event.Event;
import net.scriptability.core.script.Script;
import net.scriptability.core.template.Template;
import net.scriptability.core.visitor.Visited;
import net.scriptability.core.visitor.Visitor;
import net.scriptability.core.visitor.VisitorException;

import java.util.List;
import java.util.Map;

/**
 * Holds framework configuration. Exposes methods to add or retrieve individual configuration items and to
 * visit all configuration items with a {@link Visitor}
 *
 * @author Hayden Bakkum
 */
public class Configuration {

    /**
     * Map of events, keyed by event name
     */
    private final Map<String, Event> events = Maps.newHashMap();

    /**
     * Map of scripts, keyed by script name
     */
    private final Map<String, Script> scripts = Maps.newHashMap();

    /**
     * Map of templates, keyed by template name
     */
    private final Map<String, Template> templates = Maps.newHashMap();

    /**
     * Visits all configuration items with a visit.
     *
     * @param visitor the visitor
     * @throws VisitorException if an error occurs during a visit.
     */
    public void visit(final Visitor visitor) throws VisitorException {
        final List<Visited> allConfiguration = Lists.newLinkedList();
        allConfiguration.addAll(events.values());
        allConfiguration.addAll(scripts.values());
        allConfiguration.addAll(templates.values());

        for (Visited visited : allConfiguration) {
            visited.accept(visitor);
        }
    }

    /**
     * Adds a new event to configuration. If an event with this name already exists it will be overridden by the new event.
     *
     * @param event event to add
     * @throws ConfigurationException if an error occurs adding the event
     */
    public void add(final Event event) throws ConfigurationException {
        events.put(event.getName(), event);
    }

    /**
     * Adds a new script to configuration. If an script with this name already exists it will be overridden by the new script.
     *
     * @param script script to add
     * @throws ConfigurationException if an error occurs adding the script
     */
    public void add(final Script script) throws ConfigurationException {
        scripts.put(script.getName(), script);
    }

    /**
     * Adds a new template to configuration. If an template with this name already exists it will be overridden by the new template.
     *
     * @param template template to add
     * @throws ConfigurationException if an error occurs adding the template
     */
    public void add(final Template template) throws ConfigurationException {
        templates.put(template.getName(), template);
    }

    /**
     * Gets an event by name. If no event exists with the specified name, null will be returned.
     *
     * @param eventName name of the event
     * @return event with the specified name if it exists, null otherwise
     */
    public Event getEvent(final String eventName) {
        return events.get(eventName);
    }

    /**
     * Gets a script by name. If no script exists with the specified name, null will be returned.
     *
     * @param scriptName name of the script
     * @return script with the specified name if it exists, null otherwise
     */
    public Script getScript(final String scriptName) {
        return scripts.get(scriptName);
    }

    /**
     * Gets a template by name. If no template exists with the specified name, null will be returned.
     *
     * @param templateName name of the template
     * @return template with the specified name if it exists, null otherwise
     */
    public Template getTemplate(final String templateName) {
        return templates.get(templateName);
    }

}