Class Mailbox
java.lang.Object
madkit.kernel.Mailbox
This class represents the mailbox of an Agent
- Since:
- MaDKit 6.0
-
Method Summary
Modifier and TypeMethodDescriptionRetrieves and removes all the messages from the message box.<T extends Message>
TGets the last received message.<M extends Message>
MGets the last received message according to a filter.<M extends Message>
M/** Gets the next message which is a reply to the originalMessage.boolean
isEmpty()
Tells if there is a message in the mailbox<M extends Message>
Mnext()
Retrieves and removes the oldest received message contained in the mailbox.<M extends Message>
MRetrieves and removes the first oldest message of the mailbox that matches the filter.nextMatches
(Predicate<M> filter) Retrieves all the messages of the mailbox, without taking those that match the filter, Messages are listed in the order they were received.<M extends Message>
Mpurge()
Purges the mailbox and returns the most recent received message at that time.<M extends Message>
MPurges the mailbox and returns the most recent received message at that time that matches the filter.Retrieves and removes all the messages from the mailbox.int
size()
Returns the number of messages in the mailbox.toString()
waitAnswers
(Message origin, int howMany, Integer timeOutMilliSeconds) Returns the next howMany messages that are replies to the origin message, waiting for ever if necessary until howMany replies become available.waitMessages
(Integer timeOutMilliseconds, int howMany, Predicate<M> filter) Returns the next howMany messages that match the filter, waiting for ever if necessary until howMany matching messages become available.<M extends Message>
MwaitNext()
This method is the blocking version of nextMessage().<T extends Message>
TwaitNext
(long timeoutMilliSeconds) Wait next message.<M extends Message>
MThis method gets the next message of the mailbox or waits for a new incoming acceptable message up to a certain delay.<M extends Message>
MRetrieves and removes the next message that complies with the filter, waiting for ever if necessary until a matching message becomes available.
-
Method Details
-
toString
-
next
Retrieves and removes the oldest received message contained in the mailbox. Beware that if T is not the type of the next message, aClassCastException
will be thrown. For instance, if the next message is aStringMessage
and you call this method with T beingStringMessage
, it will work. But if you call it with T beingMessage
, aClassCastException
will be thrown.However, if you are seeking for a message of type T in the mailbox, you can use
next(Predicate)
with a specific Predicate such as inStringMessage sm = next(StringMessage.class::isInstance)
- Type Parameters:
M
- the expected type of the message to get- Returns:
- The next message or
null
if the message box is empty.
-
next
Retrieves and removes the first oldest message of the mailbox that matches the filter.- Type Parameters:
M
- the expected type of the message to get- Parameters:
filter
- the filter to use- Returns:
- The next acceptable message or
null
if such message has not been found.
-
nextMatches
Retrieves all the messages of the mailbox, without taking those that match the filter, Messages are listed in the order they were received.- Parameters:
filter
- ifnull
all the messages are returned and removed from the mailbox.- Returns:
- the ordered list of matching messages, or an empty list if none has been found.
-
removeAll
-
getAll
Retrieves and removes all the messages from the message box.- Type Parameters:
M
- the type of the message to get- Parameters:
filter
- the filter to use to select the messages to remove- Returns:
- the list of messages in the order they were received
-
getNewest
Gets the last received message. Beware that ifT
is not the type of the next message, aClassCastException
will be thrown. For instance, if the newest message is aStringMessage
and you call this method with T beingStringMessage
, it will work. But if you call it with T beingMessage
, aClassCastException
will be thrown.However, if you are seeking for a message of type T, you can use
getNewest(Predicate)
with a specific Predicate such as inStringMessage sm = getNewest(StringMessage.class::isInstance)
- Type Parameters:
T
- the expected type of the message- Returns:
- the last received message or
null
if the mailbox is empty.
-
getNewest
-
purge
Purges the mailbox and returns the most recent received message at that time. Beware that ifT
is not the type of the next message, aClassCastException
will be thrown. However, if you are seeking for a message of type T, you can usepurge(Predicate)
with a specific Predicate such as inStringMessage sm = purge(StringMessage.class::isInstance)
- Type Parameters:
M
- the expected type of the message- Returns:
- the most recent received message or
null
if the mailbox is already empty.
-
purge
Purges the mailbox and returns the most recent received message at that time that matches the filter.- Type Parameters:
M
- the expected type of the message- Parameters:
filter
- the filter to use- Returns:
- the m most recent received message that matches the filter or null if no such message has been found.
-
getReply
-
isEmpty
public boolean isEmpty()Tells if there is a message in the mailbox- Returns:
true
if there is no message in the mailbox.
-
waitNext
This method is the blocking version of nextMessage(). If there is no message in the mailbox, it suspends the agent life until a message is received.Beware that if T is not the type of the received message, a
ClassCastException
will be thrown. For instance, if the next message is aStringMessage
and you call this method with T beingStringMessage
, it will work. But if you call it with T beingMessage
, aClassCastException
will be thrown.- Type Parameters:
M
- the expected type of the message- Returns:
- the first received message
- See Also:
-
waitNext
Wait next message. This is equivalent to calling waitNext(timeoutMilliSeconds, TimeUnit.MILLISECONDS)- Type Parameters:
T
- the expected type- Parameters:
timeoutMilliSeconds
- the timeout in milliseconds- Returns:
- the t
-
waitNext
Retrieves and removes the next message that complies with the filter, waiting for ever if necessary until a matching message becomes available.- Type Parameters:
M
- the expected type of the message- Parameters:
filter
- the filter to use- Returns:
- the first received message that matches the filter
-
waitNext
This method gets the next message of the mailbox or waits for a new incoming acceptable message up to a certain delay.- Type Parameters:
M
- the expected type of the message- Parameters:
timeOutMilliseconds
- the maximum time to wait, in milliseconds.filter
- the filter to use- Returns:
- a message that matches or
null
otherwise.
-
waitMessages
public <M extends Message> List<M> waitMessages(Integer timeOutMilliseconds, int howMany, Predicate<M> filter) Returns the next howMany messages that match the filter, waiting for ever if necessary until howMany matching messages become available.- Type Parameters:
M
- the expected type of the message- Parameters:
timeOutMilliseconds
- the maximum time to wait, in milliseconds.howMany
- the number of messages to wait forfilter
- the filter to use- Returns:
- the list of messages that match the filter
-
waitAnswers
public <M extends Message> List<M> waitAnswers(Message origin, int howMany, Integer timeOutMilliSeconds) Returns the next howMany messages that are replies to the origin message, waiting for ever if necessary until howMany replies become available. Beware that if T is not the type of the answers, aClassCastException
will be thrown.- Type Parameters:
M
- the expected type of the message- Parameters:
origin
- the message to which the replies are searched.howMany
- the number of replies to wait fortimeOutMilliSeconds
- the maximum time to wait, in milliseconds.- Returns:
- the list of replies to the origin message
-
size
public int size()Returns the number of messages in the mailbox.- Returns:
- the number of messages in the mailbox.
-