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