EventListenerParameters.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.configuration2.builder;

  18. import java.util.Collections;
  19. import java.util.Map;

  20. import org.apache.commons.configuration2.event.Event;
  21. import org.apache.commons.configuration2.event.EventListener;
  22. import org.apache.commons.configuration2.event.EventListenerList;
  23. import org.apache.commons.configuration2.event.EventListenerRegistrationData;
  24. import org.apache.commons.configuration2.event.EventType;

  25. /**
  26.  * <p>
  27.  * A specialized parameters implementation for {@link BasicConfigurationBuilder} which allows for a convenient event
  28.  * listener initialization.
  29.  * </p>
  30.  * <p>
  31.  * This class offers a fluent interface for registering event listeners. A fully initialized instance can be passed to
  32.  * the {@link BasicConfigurationBuilder#configure(BuilderParameters...)} method. All event listeners which have been
  33.  * registered at the instance are then copied over to the configuration builder.
  34.  * </p>
  35.  * <p>
  36.  * The code fragment below shows a typical usage scenario:
  37.  * </p>
  38.  *
  39.  * <pre>
  40.  * BasicConfigurationBuilder&lt;Configuration&gt; builder = new BasicConfigurationBuilder&lt;Configuration&gt;(PropertiesConfiguration.class)
  41.  *     .configure(new EventListenerParameters().addEventListener(ConfigurationEvent.ANY, myListener));
  42.  * </pre>
  43.  *
  44.  * <p>
  45.  * In order to support a configuration builder's {@code configure()} method, this class implements the
  46.  * {@code BuilderParameters} interface. However, this is just a dummy implementation; no parameters are propagated to
  47.  * the builder.
  48.  * </p>
  49.  *
  50.  * @since 2.0
  51.  */
  52. public class EventListenerParameters implements BuilderParameters, EventListenerProvider {
  53.     /** Stores the event listener registrations added to this object. */
  54.     private final EventListenerList eventListeners;

  55.     /**
  56.      * Creates a new instance of {@code EventListenerParameters}.
  57.      */
  58.     public EventListenerParameters() {
  59.         eventListeners = new EventListenerList();
  60.     }

  61.     /**
  62.      * Adds the specified {@code EventListenerRegistrationData} instance to this object.
  63.      *
  64.      * @param registrationData the registration object to be added
  65.      * @param <T> the event type of the contained event listener
  66.      * @return a reference to this object for method chaining
  67.      */
  68.     public <T extends Event> EventListenerParameters addEventListener(final EventListenerRegistrationData<T> registrationData) {
  69.         eventListeners.addEventListener(registrationData);
  70.         return this;
  71.     }

  72.     /**
  73.      * Adds an event listener of the specified event type to this object.
  74.      *
  75.      * @param eventType the event type object
  76.      * @param listener the event listener
  77.      * @param <T> the event type
  78.      * @return a reference to this object for method chaining
  79.      */
  80.     public <T extends Event> EventListenerParameters addEventListener(final EventType<T> eventType, final EventListener<? super T> listener) {
  81.         eventListeners.addEventListener(eventType, listener);
  82.         return this;
  83.     }

  84.     @Override
  85.     public EventListenerList getListeners() {
  86.         return eventListeners;
  87.     }

  88.     /**
  89.      * {@inheritDoc} This implementation returns an empty map.
  90.      */
  91.     @Override
  92.     public Map<String, Object> getParameters() {
  93.         return Collections.emptyMap();
  94.     }
  95. }