jade的消息队列

链表实现

package jade.core;

import jade.util.leap.Iterator;
import jade.util.leap.LinkedList;
import jade.util.leap.EnumIterator;

import java.util.Vector;

import jade.lang.acl.ACLMessage;

/**
@author Giovanni Rimassa - Universita` di Parma
@version $Date: 2003/11/11 13:08:30 $ $Revision: 2.6 $
*/
class MessageQueue {

//#MIDP_EXCLUDE_BEGIN
// In MIDP we use Vector instead of jade.util.leap.LinkedList as the latter has been implemented in terms of the first
private LinkedList list;
//#MIDP_EXCLUDE_END
/*#MIDP_INCLUDE_BEGIN
private Vector list;
#MIDP_INCLUDE_END*/

private int maxSize;

public MessageQueue(int size) {
maxSize = size;
//#MIDP_EXCLUDE_BEGIN
list = new LinkedList();
//#MIDP_EXCLUDE_END
/*#MIDP_INCLUDE_BEGIN
list = new Vector();
#MIDP_INCLUDE_END*/
}

public MessageQueue() {
this(0);
}

public boolean isEmpty() {
return list.isEmpty();
}

public void setMaxSize(int newSize) throws IllegalArgumentException {
if(newSize < 0)
throw new IllegalArgumentException("Invalid MsgQueue size");
maxSize = newSize;
}

public int getMaxSize() {
return maxSize;
}

/**
* @return the number of messages
* currently in the queue
**/
public int size() {
return list.size();
}

public void addFirst(ACLMessage msg) {
if((maxSize != 0) && (list.size() >= maxSize)) {
//#MIDP_EXCLUDE_BEGIN
list.removeFirst(); // FIFO replacement policy
}
list.addFirst(msg);
//#MIDP_EXCLUDE_END
/*#MIDP_INCLUDE_BEGIN
list.setElementAt(msg,0);
} else
list.insertElementAt(msg,0);
#MIDP_INCLUDE_END*/
}

public void addLast(ACLMessage msg) {
if((maxSize != 0) && (list.size() >= maxSize)){
//#MIDP_EXCLUDE_BEGIN
list.removeFirst(); // FIFO replacement policy
System.err.println("WARNING: a message has been lost by an agent because of the FIFO replacement policy of its message queue.\n Notice that, under some circumstances, this might not be the proper expected behaviour and the size of the queue needs to be increased. Check the method Agent.setQueueSize()");
}
list.addLast(msg);
//#MIDP_EXCLUDE_END
/*#MIDP_INCLUDE_BEGIN
list.removeElementAt(0);
}
list.addElement(msg);
#MIDP_INCLUDE_END*/
}

public ACLMessage removeFirst() {
//#MIDP_EXCLUDE_BEGIN
return (ACLMessage)list.removeFirst();
//#MIDP_EXCLUDE_END
/*#MIDP_INCLUDE_BEGIN
ACLMessage msg = (ACLMessage)list.firstElement();
list.removeElementAt(0);
return msg;
#MIDP_INCLUDE_END*/
}

public boolean remove(ACLMessage item) {
//#MIDP_EXCLUDE_BEGIN
return list.remove(item);
//#MIDP_EXCLUDE_END
/*#MIDP_INCLUDE_BEGIN
return list.removeElement(item);
#MIDP_INCLUDE_END*/
}

public Iterator iterator() {
//#MIDP_EXCLUDE_BEGIN
return list.iterator();
//#MIDP_EXCLUDE_END
/*#MIDP_INCLUDE_BEGIN
return new EnumIterator(list.elements());
#MIDP_INCLUDE_END*/
}

//#MIDP_EXCLUDE_BEGIN

// For persistence service
private void setMessages(java.util.List l) {
// FIXME: To be implemented
System.out.println(">>> MessageQueue::setMessages() <<<");
}

// For persistence service
private java.util.List getMessages() {
// FIXME: To be implemented
System.out.println(">>> MessageQueue::getMessages() <<<");
return null;
}

//#MIDP_EXCLUDE_END


// For persistence service
private Long persistentID;

// For persistence service
private Long getPersistentID() {
return persistentID;
}

// For persistence service
private void setPersistentID(Long l) {
persistentID = l;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值