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;
    }

}

你可能感兴趣的:(消息队列)