|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectcom.marringtons.string.Format
This class provides a simple string formatting interface with more power than the C printf() and is much easier to use than the java.text.MessageFormat class. java.text.MessageFormat will rebuild the pattern if the static format() methods are called. While this limitation can be overcome by using a constructor with a pattern this removes the message text from the implementation - making the code harder to understand and maintain. Message provides a cache of formats to overcome these issues. Hence a typical message with be accessed and set in a single statement:
message = Format.pattern("0: {0}, 1: {1}").with("one").width("two").toString();
Often a message has only one argument. There is a format() override to make
this instance easier to read and use.
message = Format.pattern("1: {0}").with("one").toString();
Readability is also reduced by having to wrap primatives in their matching
class. There is a with() method for each primative. The toString() is rarely
needed since the method is typically used to send or save a message to a
class that takes an object.
message = Format.pattern("0: {0}").with(12).toString();
Messages.add(Format.pattern("{0} {1}").with(a).with(b));
The java.text.MessageFormat parsing with respect to quoting is a little
difficult to understand. This wrapper changes the rules. A quote is output as
a quote in the string while the curly braces will delimit an argument. To put
a single brace in the string, put 2 braces in the pattern. If you need a
format quote (possibly in a sub-format, use '';
message = Format.pattern("1: {{braced}} '{0}'").with("one");
message = Format.pattern("1: ''{12}'' '{0}'").with("one"); // "1: {12} 'one'"
All the
and,
options are available:
check("{0}", 1234, "1,234");
check("{0,number}", 1234, "1,234");
check("{0,number,integer}", 1234, "1,234");
check("{0,number,percent}", new Double("0.16"), "16%");
// @see java.text.DecimalFormat
check("{0,number,#,##0.00;(#,##0.00)}", -1234, "(1,234.00)");
Calendar calendar = new GregorianCalendar(2005, 2, 13, 20, 38, 55);
Date date = new Date(calendar.getTimeInMillis());
check("{0,date}", date, "13/03/2005");
check("{0,date, short}", date, "13/03/05");
check("{0,date, medium}", date, "13/03/2005");
check("{0,date, long}", date, "13 March 2005");
check("{0,date, full}", date, "Sunday, 13 March 2005");
// @see java.text.SimpleDateFormat
check("{0,date,yyyy.MM.dd G ''at'' HH:mm:ss z}", date,
"2005.03.13 AD at 20:38:55 EST");
check("{0,time}", date, "20:38:55");
check("{0,time, short}", date, "20:38");
check("{0,time, medium}", date, "20:38:55");
check("{0,time, long}", date, "20:38:55");
check("{0,time, full}", date, "08:38:55 PM EST");
String choice = // @see java.text.ChoiceFormat
"{0,choice,-1#negative ({0})|0#none|1#one|1<{0,number,integer}|101#greater than 100}";
check(choice, -23, "negative (-23)");
check(choice, 0, "none");
check(choice, 1, "one");
check(choice, 11, "11");
check(choice, 111, "greater than 100");
,
MessageFormat| Method Summary | |
boolean |
equals(Object obj)
|
static Format |
pattern(String pattern)
Retrieve or create a @see MessageFormat given a pattern. |
String |
toString()
Create a formatted output string from with() set arguments alone. |
Format |
with(boolean argument)
Used to build up a message from the parts. |
Format |
with(char argument)
Used to build up a message from the parts. |
Format |
with(int argument)
Used to build up a message from the parts. |
Format |
with(Object argument)
Used to build up a message from the parts. |
| Methods inherited from class java.lang.Object |
getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Method Detail |
public boolean equals(Object obj)
Object.equals(java.lang.Object)public static Format pattern(String pattern)
Message message = Message.pattern( "The rain in {0} rains mainly on the {1}");
pattern - to use to generate a message.
public String toString()
throw new ProgrammingErrorException(
Message.pattern( "{0} caused an error").with( userName).format();
public Format with(Object argument)
Message.pattern( "{0} foxed chased the {1}").with( 10).with( userName).format();
argument - to fill out pattern with.
public Format with(int argument)
Message.pattern( "{0} fox chased the {1}").with( 10).with( userName).format();
argument - to fill out pattern with.
public Format with(char argument)
Message.pattern( "{0} fox chased the {1}").with( 'A').with( userName).format();
argument - to fill out pattern with.
public Format with(boolean argument)
argument - to fill out pattern with.
directly.
Message.pattern( "The answer is {0}").with( true).format();
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||