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