All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.objectweb.dream.queue.buffer.BufferFIFO Maven / Gradle / Ivy

/**
 * Dream
 * Copyright (C) 2003-2004 INRIA Rhone-Alpes
 *
 * 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 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
 *
 * Contact: [email protected]
 *
 * Initial developer(s): Romain Lenglet
 * Contributor(s):
 */

package org.objectweb.dream.queue.buffer;

import java.util.Iterator;
import java.util.LinkedList;

import org.objectweb.dream.message.Message;
import org.objectweb.dream.dreamannotation.DreamComponent;

/**
 * A buffer in which stored messages are available to {@link #get() get} or
 * {@link #remove() remove} in the same order as they are {@link #add(Message)
 * added} (First In First Out).
 */
@DreamComponent(controllerDesc = "dreamUnstoppablePrimitive")
public class BufferFIFO extends AbstractBuffer
{

  // ------------------------------------------------------------------------
  // Fields
  // ------------------------------------------------------------------------

  /**
   * The messages stored in this buffer, in adding order (FIFO).
   */
  protected LinkedList messages = new LinkedList();

  // ---------------------------------------------------------------------------
  // Implementation of the AbstractBuffer methods.
  // ---------------------------------------------------------------------------

  protected void doAdd(Message message)
  {
    messages.addLast(message);
    incrementAvailableMessagesCount(1);
    incrementStoredMessagesCount(1);
  }

  protected Message doGet()
  {
    return messages.getFirst();
  }

  protected Message doRemove()
  {
    Message message = messages.removeFirst();
    incrementAvailableMessagesCount(-1);
    incrementStoredMessagesCount(-1);
    return message;
  }

  protected Message doRemoveAll()
  {

    int size = messages.size();

    if (size == 0)
    {
      return null;
    }

    Message aggregatedMessage = messageManagerItf.createMessage();

    Iterator messagesIterator = messages.iterator();
    while (messagesIterator.hasNext())
    {
      Message message = (Message) messagesIterator.next();
      messageManagerItf.addSubMessage(aggregatedMessage, message);
    }

    messages.clear();
    incrementAvailableMessagesCount(-size);
    incrementStoredMessagesCount(-size);

    return aggregatedMessage;

  }
  
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy