    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 java.util.Collections;
    020import java.util.Map;
    021
    022import org.apache.commons.configuration2.event.Event;
    023import org.apache.commons.configuration2.event.EventListener;
    024import org.apache.commons.configuration2.event.EventListenerList;
    025import org.apache.commons.configuration2.event.EventListenerRegistrationData;
    026import org.apache.commons.configuration2.event.EventType;
    027
    028/**
    029 * <p>
    030 * A specialized parameters implementation for {@link BasicConfigurationBuilder}
    031 * which allows for a convenient event listener initialization.
    032 * </p>
    033 * <p>
    034 * This class offers a fluent interface for registering event listeners. A fully
    035 * initialized instance can be passed to the
    036 * {@link BasicConfigurationBuilder#configure(BuilderParameters...)} method. All
    037 * event listeners which have been registered at the instance are then copied
    038 * over to the configuration builder.
    039 * </p>
    040 * <p>
    041 * The code fragment below shows a typical usage scenario:
    042 * </p>
    043 *
    044 * <pre>
    045 * BasicConfigurationBuilder&lt;Configuration&gt; builder =
    046 *         new BasicConfigurationBuilder&lt;Configuration&gt;(
    047 *                 PropertiesConfiguration.class)
    048 *                 .configure(new EventListenerParameters().addEventListener(
    049 *                         ConfigurationEvent.ANY, myListener));
    050 * </pre>
    051 *
    052 * <p>
    053 * In order to support a configuration builder's {@code configure()} method,
    054 * this class implements the {@code BuilderParameters} interface. However, this
    055 * is just a dummy implementation; no parameters are propagated to the builder.
    056 * </p>
    057 *
    058 * @version $Id: EventListenerParameters.java 1842194 2018-09-27 22:24:23Z ggregory $
    059 * @since 2.0
    060 */
    061public class EventListenerParameters implements BuilderParameters,
    062        EventListenerProvider
    063{
    064    /** Stores the event listener registrations added to this object. */
    065    private final EventListenerList eventListeners;
    066
    067    /**
    068     * Creates a new instance of {@code EventListenerParameters}.
    069     */
    070    public EventListenerParameters()
    071    {
    072        eventListeners = new EventListenerList();
    073    }
    074
    075    /**
    076     * Adds an event listener of the specified event type to this object.
    077     *
    078     * @param eventType the event type object
    079     * @param listener the event listener
    080     * @param <T> the event type
    081     * @return a reference to this object for method chaining
    082     */
    083    public <T extends Event> EventListenerParameters addEventListener(
    084            final EventType<T> eventType, final EventListener<? super T> listener)
    085    {
    086        eventListeners.addEventListener(eventType, listener);
    087        return this;
    088    }
    089
    090    /**
    091     * Adds the specified {@code EventListenerRegistrationData} instance to this
    092     * object.
    093     *
    094     * @param registrationData the registration object to be added
    095     * @param <T> the event type of the contained event listener
    096     * @return a reference to this object for method chaining
    097     */
    098    public <T extends Event> EventListenerParameters addEventListener(
    099            final EventListenerRegistrationData<T> registrationData)
    100    {
    101        eventListeners.addEventListener(registrationData);
    102        return this;
    103    }
    104
    105    /**
    106     * {@inheritDoc} This implementation returns an empty map.
    107     */
    108    @Override
    109    public Map<String, Object> getParameters()
    110    {
    111        return Collections.emptyMap();
    112    }
    113
    114    @Override
    115    public EventListenerList getListeners()
    116    {
    117        return eventListeners;
    118    }
    119}