return(
new AggregatedList()
{
AEMonitor timer_mon = new AEMonitor( "aggregatedList" );
Timer timer = new Timer( "AggregatedList" );
TimerEvent event;
List list = new ArrayList();
public void
add(
Object obj )
{
List dispatch_now = null;
try{
timer_mon.enter();
// if the list is full kick off a dispatch and reset the list
if ( max_queue_size > 0 &&
max_queue_size == list.size()){
dispatch_now = list;
list = new ArrayList();
}
list.add( obj );
// set up a timer to wakeup in required time period
long now = SystemTime.getCurrentTime();
if ( event != null ){
event.cancel();
}
event =
timer.addEvent(
now + idle_dispatch_time,
new TimerEventPerformer()
{
public void
perform(
TimerEvent event )
{
dispatch();
}
});
}finally{
timer_mon.exit();
}
if ( dispatch_now != null ){
dispatch( dispatch_now );
}
}
public Object
remove(
Object obj )
{
Object res = null;
try{
timer_mon.enter();
res = list.remove( obj )?obj:null;
if ( res != null ){
long now = SystemTime.getCurrentTime();
if ( event != null ){
event.cancel();
}
if ( list.size() == 0 ){
event = null;
}else{
event =
timer.addEvent(
now + idle_dispatch_time,
new TimerEventPerformer()
{
public void
perform(
TimerEvent event )
{
dispatch();
}
});
}
}
}finally{
timer_mon.exit();
}
return( res );
}
protected void
dispatch()
{
List dispatch_list;
try{
timer_mon.enter();
dispatch_list = list;
list = new ArrayList();
}finally{
timer_mon.exit();
}
dispatch( dispatch_list );
}
protected void
dispatch(
List l )
{
if ( l.size() > 0 ){
try{
acceptor.accept( l );
}catch( Throwable e ){
Debug.printStackTrace(e);
}
}
}
public void
destroy()
{
dispatch();
timer.destroy();
}
});
}