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 */
017package org.apache.commons.configuration2.builder;
018
019import org.apache.commons.configuration2.event.Event;
020import org.apache.commons.configuration2.event.EventType;
021
022/**
023 * <p>
024 * A base event class for events generated by a {@link ConfigurationBuilder}.
025 * </p>
026 * <p>
027 * Configuration builders can trigger a number of different events. All these
028 * events are derived from this class. This event base class does not define any
029 * additional properties. However, it defines that the event source must be a
030 * {@code ConfigurationBuilder}.
031 * </p>
032 *
033 * @since 2.0
034 */
035public class ConfigurationBuilderEvent extends Event
036{
037    /** The common super type for all events related to configuration builders. */
038    public static final EventType<ConfigurationBuilderEvent> ANY =
039            new EventType<>(Event.ANY, "BUILDER");
040
041    /**
042     * The specific event type for builder reset events. Events of this type are
043     * generated each time the builder's {@code resetResult()} method is called.
044     */
045    public static final EventType<ConfigurationBuilderEvent> RESET =
046            new EventType<>(ANY, "RESET");
047
048    /**
049     * The specific event type for configuration request events. Events of this
050     * type are generated each time the builder's {@code getConfiguration()}
051     * method is called (before the managed configuration is actually accessed
052     * and the lock is acquired). This gives listeners the opportunity to
053     * perform some checks which may invalidate the configuration, e.g. trigger
054     * a reload check. <strong>Note:</strong> A listener must not call the
055     * builder's {@code getConfiguration()} method - this will cause an
056     * infinite loop!
057     *
058     * @see ConfigurationBuilder#getConfiguration()
059     */
060    public static final EventType<ConfigurationBuilderEvent> CONFIGURATION_REQUEST =
061            new EventType<>(ANY,
062                    "CONFIGURATION_REQUEST");
063
064    /**
065     * Creates a new instance of {@code ConfigurationBuilderEvent} and sets
066     * basic properties.
067     *
068     * @param source the {@code ConfigurationBuilder} object which triggered
069     *        this event (must not be <b>null</b>)
070     * @param evType the type of this event (must not be <b>null</b>)
071     * @throws IllegalArgumentException if a required parameter is null
072     */
073    public ConfigurationBuilderEvent(final ConfigurationBuilder<?> source,
074            final EventType<? extends ConfigurationBuilderEvent> evType)
075    {
076        super(source, evType);
077    }
078
079    /**
080     * Returns the source of this event as a {@code ConfigurationBuilder}.
081     *
082     * @return the {@code ConfigurationBuilder} which generated this event
083     */
084    @Override
085    public ConfigurationBuilder<?> getSource()
086    {
087        return (ConfigurationBuilder<?>) super.getSource();
088    }
089}