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 * https://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<Configuration> builder = new BasicConfigurationBuilder<Configuration>(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 057 /** Stores the event listener registrations added to this object. */ 058 private final EventListenerList eventListeners; 059 060 /** 061 * Creates a new instance of {@code EventListenerParameters}. 062 */ 063 public EventListenerParameters() { 064 eventListeners = new EventListenerList(); 065 } 066 067 /** 068 * Adds the specified {@code EventListenerRegistrationData} instance to this object. 069 * 070 * @param registrationData the registration object to be added 071 * @param <T> the event type of the contained event listener 072 * @return a reference to this object for method chaining 073 */ 074 public <T extends Event> EventListenerParameters addEventListener(final EventListenerRegistrationData<T> registrationData) { 075 eventListeners.addEventListener(registrationData); 076 return this; 077 } 078 079 /** 080 * Adds an event listener of the specified event type to this object. 081 * 082 * @param eventType the event type object 083 * @param listener the event listener 084 * @param <T> the event type 085 * @return a reference to this object for method chaining 086 */ 087 public <T extends Event> EventListenerParameters addEventListener(final EventType<T> eventType, final EventListener<? super T> listener) { 088 eventListeners.addEventListener(eventType, listener); 089 return this; 090 } 091 092 @Override 093 public EventListenerList getListeners() { 094 return eventListeners; 095 } 096 097 /** 098 * {@inheritDoc} This implementation returns an empty map. 099 */ 100 @Override 101 public Map<String, Object> getParameters() { 102 return Collections.emptyMap(); 103 } 104}