Addresses my concerns quite nicely and should be easy to refactor code against.
On 5/5/06, Don Brown <[EMAIL PROTECTED]> wrote:
I like it, Level should extend Comparable, and Global works for me.
Don
Bob Lee wrote:
> - The attached version supports arbitrary levels. I used an interface
> instead of an enum so the user can define additional levels if they
> wish. Should Level extend Comparable?
>
> - It has built in support for INFO, WARN, and ERROR along with
> respective convenience methods.
>
> - It provides a Map of field messages. It's not necessary for Messages
> itself to implement both Map and List. Delegating to separate objects
> is less confusing.
>
> - Adding messages and checking for the presence of messages
> (hasErrors()) should be dead simple. Getting the messages doesn't have
> to be as convenient (at least not through the published API).
>
> - "Request-scoped" is the wrong word. We're really talking about "not
> associated with a field." Page-scoped? Form-scoped? Global?
>
> Thanks,
> Bob
>
>
> ------------------------------------------------------------------------
>
> package org.apache.struts.action2;
>
> import java.util.List;
> import java.util.Map;
> import java.util.HashMap;
> import java.util.Set;
> import java.io.Serializable;
>
> /**
> * Request and field-scoped messages. Uses keys instead of actual messages to
decouple code from messages. Messages
> * may come from multiple actions and interceptors.
> *
> * @author [EMAIL PROTECTED] (Bob Lee)
> */
> public interface Messages {
>
> /**
> * Message level.
> */
> public interface Level {
>
> /**
> * Informational message level.
> */
> public static final Level INFO = new LevelImpl("info");
>
> /**
> * Warning message level.
> */
> public static final Level WARN = new LevelImpl("warn");
>
> /**
> * Error message level.
> */
> public static final Level ERROR = new LevelImpl("error");
> }
>
> /**
> * Adds request-scoped informational message.
> *
> * @param key message key
> * @see Level.INFO
> */
> void info(String key);
>
> /**
> * Adds request-scoped informational message.
> *
> * @param key message key
> * @param arguments message arguments
> * @see Level.INFO
> */
> void info(String key, Object... arguments);
>
> /**
> * Adds field-scoped informational message.
> *
> * @param fieldName name of field to attach message to
> * @param key message key
> * @see Level.INFO
> */
> void info(String fieldName, String key);
>
> /**
> * Adds field-scoped informational message.
> *
> * @param fieldName name of field to attach message to
> * @param key message key
> * @param arguments message arguments
> * @see Level.INFO
> */
> void info(String fieldName, String key, Object... arguments);
>
> /**
> * Adds request-scoped warning message.
> *
> * @param key message key
> * @see Level.WARN
> */
> void warn(String key);
>
> /**
> * Adds request-scoped warning message.
> *
> * @param key message key
> * @param arguments message arguments
> * @see Level.WARN
> */
> void warn(String key, Object... arguments);
>
> /**
> * Adds field-scoped warning message.
> *
> * @param fieldName name of field to attach message to
> * @param key message key
> * @see Level.WARN
> */
> void warn(String fieldName, String key);
>
> /**
> * Adds field-scoped warning message.
> *
> * @param fieldName name of field to attach message to
> * @param key message key
> * @param arguments message arguments
> * @see Level.WARN
> */
> void warn(String fieldName, String key, Object... arguments);
>
> /**
> * Adds request-scoped error message.
> *
> * @param key message key
> * @see Level.ERROR
> */
> void error(String key);
>
> /**
> * Adds request-scoped error message.
> *
> * @param key message key
> * @param arguments message arguments
> * @see Level.ERROR
> */
> void error(String key, Object... arguments);
>
> /**
> * Adds field-scoped error message.
> *
> * @param fieldName name of field to attach message to
> * @param key message key
> * @see Level.ERROR
> */
> void error(String fieldName, String key);
>
> /**
> * Adds field-scoped error message.
> *
> * @param fieldName name of field to attach message to
> * @param key message key
> * @param arguments message arguments
> * @see Level.ERROR
> */
> void error(String fieldName, String key, Object... arguments);
>
> /**
> * Adds request-scoped message.
> *
> * @param level message level
> * @param key message key
> */
> void add(Level level, String key);
>
> /**
> * Adds request-scoped message.
> *
> * @param level message level
> * @param key message key
> * @param arguments message arguments
> */
> void add(Level level, String key, Object... arguments);
>
> /**
> * Adds field-scoped message.
> *
> * @param level message level
> * @param fieldName name of field to attach message to
> * @param key message key
> */
> void add(Level level, String fieldName, String key);
>
> /**
> * Adds field-scoped message.
> *
> * @param level message level
> * @param fieldName name of field to attach message to
> * @param key message key
> * @param arguments message arguments
> */
> void add(Level level, String fieldName, String key, Object... arguments);
>
> /**
> * Gets request-scoped messages.
> *
> * @param level message level
> * @return unmodifiable list of messages for this request.
> */
> List<String> forRequest(Level level);
>
> /**
> * Gets field-scoped messages.
> *
> * @param level message level
> * @return unmodifiable map of field names to message lists
> */
> Map<String, List<String>> forFields(Level level);
>
> /**
> * Returns set of levels for which we have messages.
> *
> * @return unmodifiable set of levels
> */
> Set<Level> levels();
>
> /**
> * Returns true if we have request or field-scoped error messages.
> *
> * @see Level.ERROR
> */
> boolean hasErrors();
>
> /**
> * Returns true if we have request or field-scoped warning messages.
> *
> * @see Level.WARN
> */
> boolean hasWarnings();
>
> /**
> * Returns true if we have request or field-scoped informational messages.
> *
> * @see Level.INFO
> */
> boolean hasInformation();
>
> /**
> * Returns true if no request or field-scoped messages have been added.
> */
> boolean isEmpty();
>
> /**
> * Returns true if no request or field-scoped messages have been added
for the given level.
> *
> * @param level message level
> */
> boolean isEmpty(Level level);
>
> static class LevelImpl implements Messages.Level, Serializable {
>
> private static final long serialVersionUID = 0;
>
> static Map<String, Level> levels = new HashMap<String, Level>();
>
> String name;
>
> public LevelImpl(String name) {
> this.name = name;
> levels.put(name, this);
> }
>
> Object readResolve() {
> return forName(name);
> }
>
> public String toString() {
> return "Level(" + name + ")";
> }
>
> static Level forName(String name) {
> Level level = levels.get(name);
> if (level == null)
> throw new NullPointerException("Invalid level: " + name);
> return level;
> }
> }
> }
>
>
>
>
>
>
>
>
>
>
>
>
> ------------------------------------------------------------------------
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]