/* * ALMA - Atacama Large Millimiter Array (c) Associated Universities Inc., 2002 * (c) National Radio Astronomy Observatory, 2009 Copyright by NRAO (in the framework of * the ALMA collaboration), All rights reserved * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ package alma.acs.nc; import java.util.Vector; import org.omg.CosNotification.StructuredEvent; /** * Circular Queue that can buffer events while the Notify Service is down. *
* Currently we store the event data twice: Both the StructuredEvent with Corba Anys inside (incl. the user data),
* and also the user data separately. This design relieves us from transforming the user data back
* from Corba Any to IDL-generated struct.
* If this becomes a memory issue, we may have to change it.
*
* @see NCPublisher#publishCORBAEvent(StructuredEvent, T)
* @author Jorge Avarias null
if all went well,
* or otherwise the data for another event that had to be pushed out of the queue
* if the queue was full.
*/
public Data push(StructuredEvent e, T userData) {
Data ret = null;
queue.add(new Data(e, userData));
length++;
if(length > queue.capacity()-1) {
ret = queue.remove(0);
length--;
}
return ret;
}
/**
* Remove all the elements of the queue
*/
public void clear(){
queue.clear();
length = 0;
}
/**
* Remove the first element in the queue and return it. If there is no
* elements in the queue returns null
*
* @return the first element in the queue
*/
public Data pop(){
Data ret = null;
try{
ret = queue.remove(0);
length--;
}catch (IndexOutOfBoundsException ex)
{}
return ret;
}
/**
* Set the capacity of the queue. It only changes the capacity when the
* given size is greater than the current one.
* @return true when the capacity of the queue has been changed. Otherwise returns false.
*/
public boolean setMaxSize(int size) {
if(size > queue.capacity()) {
queue.ensureCapacity(size+1);
return true;
}
return false;
}
/**
* @return the current capacity of the queue
*/
public int getMaxSize() {
return queue.capacity();
}
}