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