net.sourceforge.stripes.action
Class SimpleMessage

java.lang.Object
  extended by net.sourceforge.stripes.action.SimpleMessage
All Implemented Interfaces:
Serializable, Message
Direct Known Subclasses:
LocalizableMessage, SimpleError

public class SimpleMessage
extends Object
implements Message

A simple non-error message that uses the String supplied to it as the message (i.e. it does not look up the message in a resource bundle).

Messages may contain one or more "replacement parameters". To use replacement parameters a message must contain the replacement token {#} where # is the numeric index of the replacement parameter.

For example, to construct a message with two replacement parameters you might supply a message like:

Welcome back {0}, your last login was on {1,date,short}.

At runtime this might get replaced out to result in a message for the user that looks like "Welcome back Johnnie, your last login was on 01/01/2006".

MessageFormat is used to merge the parameters in to the message and as a result the parameters can themselves receive formatting through the various java.text.* formatters.

Author:
Tim Fennell
See Also:
MessageFormat, Serialized Form

Constructor Summary
protected SimpleMessage(Object... parameters)
          Helper constructor to allow subclasses to provide and manipulate replacement parameters without having to supply a message String.
  SimpleMessage(String message, Object... parameters)
          Constructs a message with the supplied message string and zero or more parameters to be merged into the message.
 
Method Summary
 boolean equals(Object o)
          Checks equality by ensuring that the current instance and the 'other' instance are instances of the same class (though not necessarily SimpleMessage!) and that the message String and replacement parameters provided are the same.
 String getMessage()
          Returns the exact message that was supplied in the constructor.
 String getMessage(Locale locale)
          Uses the String message passed in as the message template and combines it with any replacement parameters provided to construct a message for display to the user.
protected  String getMessageTemplate(Locale locale)
          Simply returns the message passed in at Construction time.
 Object[] getReplacementParameters()
          Allows subclasses to access the replacement parameters for this message.
 int hashCode()
          Generated hash code method.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleMessage

public SimpleMessage(String message,
                     Object... parameters)
Constructs a message with the supplied message string and zero or more parameters to be merged into the message. When constructing a SimpleMessage a non-null message string must be supplied (though subclasses may return null if they do not rely upon it).

Parameters:
message - the String message to display to the user, optionally with placeholders for replacement parameters
parameters -

SimpleMessage

protected SimpleMessage(Object... parameters)
Helper constructor to allow subclasses to provide and manipulate replacement parameters without having to supply a message String.

Parameters:
parameters - zero or more parameters for replacement into the message
Method Detail

getMessage

public String getMessage(Locale locale)
Uses the String message passed in as the message template and combines it with any replacement parameters provided to construct a message for display to the user. Although SimpleMessage does not localize it's message string, any formatters invoked as a result of using replacement parameters will be in the correct locale.

Specified by:
getMessage in interface Message
Parameters:
locale - the locale of the current request
Returns:
String the message stored under the messageKey supplied

getMessageTemplate

protected String getMessageTemplate(Locale locale)
Simply returns the message passed in at Construction time. Designed to be overridden by subclasses to lookup messages from resource bundles.

Parameters:
locale - the Locale of the message template desired
Returns:
the message (potentially with TextFormat replacement tokens).

getMessage

public String getMessage()
Returns the exact message that was supplied in the constructor. This should not be called to render user output, but only when direct access to the String is needed for some reason.

Returns:
the exact message String passed in to the constructor

getReplacementParameters

public Object[] getReplacementParameters()
Allows subclasses to access the replacement parameters for this message.


equals

public boolean equals(Object o)
Checks equality by ensuring that the current instance and the 'other' instance are instances of the same class (though not necessarily SimpleMessage!) and that the message String and replacement parameters provided are the same.

Overrides:
equals in class Object
Parameters:
o - another object that is a SimpleMessage or subclass thereof
Returns:
true if the two objects will generate the same user message, false otherwise

hashCode

public int hashCode()
Generated hash code method.

Overrides:
hashCode in class Object


? Copyright 2005-2006, Stripes Development Team.