org.objectweb.dream.queue.keyed
Class AbstractBufferKeyedAddKeyedRemoveImpl

java.lang.Object
  extended by org.objectweb.dream.AbstractComponent
      extended by org.objectweb.dream.queue.keyed.AbstractBufferKeyedAddKeyedRemoveImpl
All Implemented Interfaces:
ContextualBindingController, NeedAsyncStartController, PrepareStopLifeCycleController, Loggable, BufferAttributeController, KeyedBufferAdd, KeyedBufferRemove, AttributeController, BindingController, LifeCycleController
Direct Known Subclasses:
BufferAscendingSequenceNumberKeyedAddKeyedRemoveImpl, BufferKeyedAddKeyedRemoveImpl

public abstract class AbstractBufferKeyedAddKeyedRemoveImpl
extends AbstractComponent
implements KeyedBufferAdd, KeyedBufferRemove, BufferAttributeController

Abstract implementation of a keyed buffer. Keyed buffers allow mapping messages to keys. This class implements the BufferAttributeController, KeyedBufferAdd and KeyedBufferRemove interfaces. When a message is added to the buffer, an associated key is specified. This key can later be used to retrieve the message.

The add and remove methods are implemented as follows

Buffer developers can inherit this class. They must implement several methods:

Moreover developers must use the incrementAvailableMessagesCount and incrementStoredMessagesCount methods to increment indicators on stored messages, available messages and available space.

Note: the add and remove methods should not be overridden.


Field Summary
protected  int availableMessagesCount
          The count of available messages in the buffer.
protected static String[] CLIENT_INTERFACE_NAMES
          The names of client interfaces.
protected  Object lock
          The lock object.
protected  int maxCapacity
          The maximal capacity of the buffer.
protected  MessageManager messageManagerItf
          Can be used by subclasses to create aggregated messages in KeyedBufferRemove.removeAll().
protected  int storedMessagesCount
          The count of stored messages into the buffer.
 
Fields inherited from class org.objectweb.dream.AbstractComponent
bindingLogger, fcNeedAsyncStart, fcState, lifeCycleLogger, logger, weaveableC
 
Fields inherited from interface org.objectweb.dream.queue.keyed.KeyedBufferAdd
ITF_NAME
 
Fields inherited from interface org.objectweb.dream.queue.keyed.KeyedBufferRemove
ITF_NAME
 
Fields inherited from interface org.objectweb.fractal.api.control.LifeCycleController
STARTED, STOPPED
 
Constructor Summary
AbstractBufferKeyedAddKeyedRemoveImpl()
           
 
Method Summary
 void add(Message message, Object key)
          Adds the specified message to the buffer, associated with the specified key object.
protected  int availableSpace(Object key)
           
 void bindFc(String clientItfName, Object serverItf)
           
protected  boolean canAdd(Message message, Object key)
          Checks whether the given message can be added into the buffer.
protected abstract  void doAdd(Message message, Object key)
          Adds a message to the buffer.
protected abstract  Message doGet(Object key)
          Gets a message from the buffer.
protected abstract  Message doRemove(Object key)
          Removes a message from the buffer.
protected abstract  Message doRemoveAll()
          Removes all the messages stored into the buffer.
 Message get(Object key)
          Gets the message from the buffer that is associated with the specified key object.
 int getCurrentSize()
          Returns the current size of the queue.
 int getMaxCapacity()
          Returns the maximum capacity of the queue.
protected  boolean hasAvailableMessage(Object key)
          Checks whether there is a message available to get or remove.
protected  void incrementAvailableMessagesCount(int delta, Object key)
          Increments the count of available messages.
protected  void incrementStoredMessagesCount(int delta, Object key)
          Increments the count of stored messages.
 String[] listFc()
           
 Message remove(Object key)
          Removes the message from the buffer that is associated with the specified key object.
 Message removeAll()
          Removes all the available messages from the buffer.
 void setMaxCapacity(int maxCapacity)
          Sets the maximum capacity of the queue.
 boolean tryAdd(Message message, Object key)
          Adds the specified message to the buffer, associated with the specified key object.
 Message tryGet(Object key)
          Gets the message from the buffer that is associated with the specified key object.
 Message tryRemove(Object key)
          Removes the message from the buffer that is associated with the specified key object.
 
Methods inherited from class org.objectweb.dream.AbstractComponent
beforeFirstStart, bindFc, getFcNeedAsyncStart, getFcState, initComponent, lookupFc, prepareStopFc, setLogger, startFc, stopFc, unbindFc
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLIENT_INTERFACE_NAMES

protected static final String[] CLIENT_INTERFACE_NAMES
The names of client interfaces.


messageManagerItf

protected MessageManager messageManagerItf
Can be used by subclasses to create aggregated messages in KeyedBufferRemove.removeAll().


maxCapacity

protected int maxCapacity
The maximal capacity of the buffer.


availableMessagesCount

protected int availableMessagesCount
The count of available messages in the buffer. This attribute should only be updated using the incrementAvailableMessagesCount(int, Object) method.


storedMessagesCount

protected int storedMessagesCount
The count of stored messages into the buffer. This attribute should only be updated using the incrementStoredMessagesCount(int, Object) method.


lock

protected final Object lock
The lock object.

Constructor Detail

AbstractBufferKeyedAddKeyedRemoveImpl

public AbstractBufferKeyedAddKeyedRemoveImpl()
Method Detail

add

public void add(Message message,
                Object key)
         throws InterruptedException
Description copied from interface: KeyedBufferAdd
Adds the specified message to the buffer, associated with the specified key object. This method blocks while there is not enough space available, and while this buffer contains a message already associated with the specified key.

Buffers may place limitations on what elements may be added to this buffer. In particular, some buffers require that messages contain particular chunks.

Specified by:
add in interface KeyedBufferAdd
Parameters:
message - the message to be added; never null.
key - the key that is associated with the message to be added; never null.
Throws:
InterruptedException - if it is interrupted while waiting for a message to be removed.
See Also:
KeyedBufferAdd.add(Message, Object)

tryAdd

public boolean tryAdd(Message message,
                      Object key)
Description copied from interface: KeyedBufferAdd
Adds the specified message to the buffer, associated with the specified key object. If the message cannot be added (due to space limitation, or if a message is already associated with the specified key), the message is not added.

Buffers may place limitations on what elements may be added to this buffer. In particular, some buffers require that messages contain particular chunks.

Specified by:
tryAdd in interface KeyedBufferAdd
Parameters:
message - the message to be added; never null.
key - the key that is associated with the message to be added; never null.
Returns:
true if the message has been added.
See Also:
KeyedBufferAdd.tryAdd(Message, Object)

remove

public Message remove(Object key)
               throws InterruptedException
Description copied from interface: KeyedBufferRemove
Removes the message from the buffer that is associated with the specified key object. This method blocks while no message associated with the specified key is available.

Specified by:
remove in interface KeyedBufferRemove
Parameters:
key - the key that is associated with the message to be removed.
Returns:
the removed message that was associated with the specified key; never null.
Throws:
InterruptedException - if it is interrupted while waiting for a message to be added.
See Also:
KeyedBufferRemove.remove(Object)

tryRemove

public Message tryRemove(Object key)
Description copied from interface: KeyedBufferRemove
Removes the message from the buffer that is associated with the specified key object. This method does not block.

Specified by:
tryRemove in interface KeyedBufferRemove
Parameters:
key - the key that is associated with the message to be removed.
Returns:
the removed message that was associated with the specified key if there was one, null otherwise.
See Also:
KeyedBufferRemove.tryRemove(Object)

get

public Message get(Object key)
            throws InterruptedException
Description copied from interface: KeyedBufferRemove
Gets the message from the buffer that is associated with the specified key object. This method blocks while no message associated with the specified key is available.

Specified by:
get in interface KeyedBufferRemove
Parameters:
key - the key that is associated with the message to get.
Returns:
the message that is associated with the specified key; never null.
Throws:
InterruptedException - if it is interrupted while waiting for a message to be added.
See Also:
KeyedBufferRemove.get(Object)

tryGet

public Message tryGet(Object key)
Description copied from interface: KeyedBufferRemove
Gets the message from the buffer that is associated with the specified key object. This method does not block.

Specified by:
tryGet in interface KeyedBufferRemove
Parameters:
key - the key that is associated with the message to get.
Returns:
the message that is associated with the specified key if there is one, null otherwise.
See Also:
KeyedBufferRemove.tryGet(Object)

removeAll

public Message removeAll()
Description copied from interface: KeyedBufferRemove
Removes all the available messages from the buffer. Messages are returned in an aggregated message. If no message has been removed, this method returns null. This method does not block.

Specified by:
removeAll in interface KeyedBufferRemove
Returns:
an aggregated message containing all the messages available from the buffer, or null if no message was available.
See Also:
KeyedBufferRemove.removeAll()

doAdd

protected abstract void doAdd(Message message,
                              Object key)
Adds a message to the buffer. This method should not check if there is enough available space provided it has already been done in the add(org.objectweb.dream.message.Message, java.lang.Object)method. The lockhas been acquired when this method is called.

Parameters:
message - the message to be added.

doRemove

protected abstract Message doRemove(Object key)
Removes a message from the buffer. This method should not check if there is a message available provided it has already been done in the remove(java.lang.Object)method. The lockhas been acquired when this method is called.

Returns:
a message.

doRemoveAll

protected abstract Message doRemoveAll()
Removes all the messages stored into the buffer. The lockhas been acquired when this method is called.

Returns:
an aggregated message.

doGet

protected abstract Message doGet(Object key)
Gets a message from the buffer. This method should not check if there is a message available provided it has already been done in the get(java.lang.Object) method. The lockhas been acquired when this method is called.

Returns:
a message.

canAdd

protected boolean canAdd(Message message,
                         Object key)
Checks whether the given message can be added into the buffer. The lockhas been acquired when this method is called.

Parameters:
message - the message to be tested.
Returns:
true if the message can be added.

hasAvailableMessage

protected boolean hasAvailableMessage(Object key)
Checks whether there is a message available to get or remove.

Returns:
true if there is an available message.

availableSpace

protected int availableSpace(Object key)

incrementAvailableMessagesCount

protected void incrementAvailableMessagesCount(int delta,
                                               Object key)
Increments the count of available messages. When calling this method, the lockmust have already been acquired.

Parameters:
delta - the value that must be added to the previous count.

incrementStoredMessagesCount

protected void incrementStoredMessagesCount(int delta,
                                            Object key)
Increments the count of stored messages. When calling this method, the lockmust have already been acquired.

Parameters:
delta - the value that must be added to previous indicator.

getMaxCapacity

public int getMaxCapacity()
Description copied from interface: BufferAttributeController
Returns the maximum capacity of the queue.

Specified by:
getMaxCapacity in interface BufferAttributeController
Returns:
the maximum capacity of the queue.
See Also:
BufferAttributeController.getMaxCapacity()

setMaxCapacity

public void setMaxCapacity(int maxCapacity)
Description copied from interface: BufferAttributeController
Sets the maximum capacity of the queue.

Specified by:
setMaxCapacity in interface BufferAttributeController
Parameters:
maxCapacity - the maximum capacity of the queue.
See Also:
BufferAttributeController.setMaxCapacity(int)

getCurrentSize

public int getCurrentSize()
Description copied from interface: BufferAttributeController
Returns the current size of the queue.

Specified by:
getCurrentSize in interface BufferAttributeController
Returns:
the current size of the queue.
See Also:
BufferAttributeController.getCurrentSize()

bindFc

public void bindFc(String clientItfName,
                   Object serverItf)
            throws NoSuchInterfaceException,
                   IllegalBindingException,
                   IllegalLifeCycleException
Specified by:
bindFc in interface BindingController
Overrides:
bindFc in class AbstractComponent
Throws:
NoSuchInterfaceException
IllegalBindingException
IllegalLifeCycleException
See Also:
BindingController.bindFc(String, Object)

listFc

public String[] listFc()
Specified by:
listFc in interface BindingController
See Also:
BindingController.listFc()


Copyright © 2003, 2005 - INRIA Rhone-Alpes - All Rights Reserved.