org.jmonit.events.DefaultMonitoringEventBus Maven / Gradle / Ivy
Show all versions of jmonit Show documentation
/*
~ Copyright 2006-2007 Nicolas De Loof.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
*/
package org.jmonit.events;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* A synchronous implementation of the MonitoringEventBus.
*
* "Synchronous" means the event is dispatched to and consumed by all listeners
* before the emitter can resume processing.
*
* @author Nicolas De Loof
*/
public class DefaultMonitoringEventBus
implements MonitoringEventBus
{
/**
* Registered listeners
*/
private List listeners =
new CopyOnWriteArrayList();
/**
* {@inheritDoc}
*
* @see org.jmonit.events.MonitoringEventBus#addListener(org.jmonit.events.MonitoringEventListener)
*/
public void addListener( final MonitoringEventListener listener )
{
if ( listener != null )
{
listeners.add( listener );
}
}
/**
* {@inheritDoc}
*
* @see org.jmonit.events.MonitoringEventBus#removeListener(MonitoringEventListener)
*/
public void removeListener( final MonitoringEventListener listener )
{
listeners.remove( listener );
}
/**
* {@inheritDoc}
*
* @see org.jmonit.events.MonitoringEventBus#hasListener(MonitoringEventListener)
*/
public boolean hasListener( final MonitoringEventListener listener )
{
return listeners.contains( listener );
}
/**
* {@inheritDoc}
*
* @see org.jmonit.events.MonitoringEventBus#fireMonitoringEvent(org.jmonit.events.MonitoringEvent)
*/
public void fireMonitoringEvent( final MonitoringEvent event )
{
for ( MonitoringEventListener listener : listeners )
{
listener.onMonitoringEvent( event );
}
}
}