org.miv.mbox
Class MBoxStandalone

java.lang.Object
  extended by org.miv.mbox.MBoxBase
      extended by org.miv.mbox.MBoxStandalone
All Implemented Interfaces:
MBox, MBoxListener

public class MBoxStandalone
extends MBoxBase

Implementation of a message box to be used without having to inherit it.

This message box is a fully fledged box that can work by itself, receive messages and dispatch them to a listener when invoked. It is the pending of the MBoxBase when it is not possible to inherit from it.

Since:
28102006
Author:
Antoine Dutot

Nested Class Summary
 
Nested classes/interfaces inherited from class org.miv.mbox.MBoxBase
MBoxBase.Message
 
Field Summary
protected  ArrayList<MBoxListener> listeners
          Listener for this message box.
protected  boolean notifyOnPost
          Notify every threads waiting on this mbox monitor at each post?.
 
Fields inherited from class org.miv.mbox.MBoxBase
messages, mtmp
 
Constructor Summary
MBoxStandalone()
          New empty message box.
MBoxStandalone(MBoxListener listener)
          New empty message box dispatching messages to a given listener.
 
Method Summary
 void addListener(MBoxListener listener)
          Add a listener for this message box.
 boolean notifyOnPost()
          Does the post(String,Object...) method must notify any thread waiting on the monitor of this message box?.
 void post(String from, Object... data)
          Post a message in the message box.
 void processMessage(String from, Object[] data)
          Method to override to process each incomming message.
 void removeListener(MBoxListener listener)
          Remove a listener.
 void setListener(MBoxListener listener)
          Deprecated. 
 void setNotifyOnPost(boolean on)
          Specify if each call to post(String, Object...) should notify threads waiting on the monitor of this message box.
 
Methods inherited from class org.miv.mbox.MBoxBase
getMessageCount, isMBoxEmpty, popPendingMessages, processMessages
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

listeners

protected ArrayList<MBoxListener> listeners
Listener for this message box. There can be only one listener per message box.


notifyOnPost

protected boolean notifyOnPost
Notify every threads waiting on this mbox monitor at each post?.

Constructor Detail

MBoxStandalone

public MBoxStandalone(MBoxListener listener)
New empty message box dispatching messages to a given listener.

Parameters:
listener - The message reader.

MBoxStandalone

public MBoxStandalone()
New empty message box. To use this, you must add a listener.

Method Detail

notifyOnPost

public boolean notifyOnPost()
Does the post(String,Object...) method must notify any thread waiting on the monitor of this message box?.


setNotifyOnPost

public void setNotifyOnPost(boolean on)
Specify if each call to post(String, Object...) should notify threads waiting on the monitor of this message box.


post

public void post(String from,
                 Object... data)
Description copied from interface: MBox
Post a message in the message box. This method can be called from another thread.

Specified by:
post in interface MBox
Overrides:
post in class MBoxBase
Parameters:
from - Identifier of the sending object.
data - Array of posted objects.

processMessage

public void processMessage(String from,
                           Object[] data)
Description copied from class: MBoxBase
Method to override to process each incomming message.

Specified by:
processMessage in interface MBoxListener
Specified by:
processMessage in class MBoxBase
Parameters:
from - The address of the sender.
data - The data sent by the sender.

setListener

@Deprecated
public void setListener(MBoxListener listener)
Deprecated. 

Set the listener of this message box. All messages will be sent to it when MBoxBase.processMessages() will be called.

Parameters:
listener - The message listener.

addListener

public void addListener(MBoxListener listener)
Add a listener for this message box. All message will be sent to it when MBoxBase.processMessages() is called. Note that all listeners receive all messages. That is if there is N listeners, and the box received one message, the message is sent N times, once for each listener.

Parameters:
listener - The listener to add.

removeListener

public void removeListener(MBoxListener listener)
Remove a listener.

Parameters:
listener - The listener to remove.


Copyright © 2011. All Rights Reserved.