📄 format.java
字号:
o = "mondrian";
} else {
o = d;
}
testFormat(pw, null, o, fe.token, null);
}
pw.println();
pw.println("Some tricky dates.");
// must not throw exception
testFormat(pw, null, date2, "mm/##/yy", "09/##/10");
// must recognize lowercase "dd"
testFormat(pw, null, date2, "mm/dd/yy", "09/07/10");
// must print '7' not '07'
testFormat(pw, null, date2, "mm/d/yy", "09/7/10");
// must not decrement month by one (cuz java.util.Calendar is 0-based)
testFormat(pw, null, date2, "mm/dd/yy", "09/07/10");
// must recognize "mmm"
testFormat(pw, null, date2, "mmm/dd/yyyy", "Sep/07/2010");
// "mm" means minute, not month, when following "hh"
testFormat(pw, null, date2, "hh/mm/ss", "06/05/04");
// must recognize "Long Date" etc.
testFormat(pw, null, date2, "Long Date", "Tuesday, September 07, 2010");
// international currency symbol
testFormat(pw, null, new Double(1.2), "" + intlCurrencySymbol + "#", "$1");
pw.println();
pw.println("End of test.");
pw.flush();
}
static private void testFormat(
PrintWriter pw, FormatLocale locale, Object o, String f, String result)
{
pw.print(
"format(" + o + "," +
(f == null ? "null" : "'" + f + "'") +
")");
pw.flush();
Format format = new Format(f, locale);
String s = format.format(o);
pw.print(" --> '" + s + "'");
if (result == null) {
pw.println();
} else if (s.equals(result)) {
pw.println(" CORRECT");
} else {
pw.println(" INCORRECT - should be '" + result + "'");
}
pw.flush();
}
/**
* Formats an object using a format string, according to a given locale. If
* you need to format many objects using the same format string, use {@link
* #Format(Object,String,Locale)}.
**/
static String format(Object o, String formatString, Locale locale)
{
Format format = new Format(formatString, locale);
return format.format(o);
}
private static class Token {
int code;
int flags;
String token;
Token(int code, int flags, String token)
{
this.code = code;
this.flags = flags;
this.token = token;
}
boolean isSpecial()
{
return (flags & SPECIAL) == SPECIAL;
}
boolean isNumeric()
{
return (flags & NUMERIC) == NUMERIC;
}
boolean isDate()
{
return (flags & DATE) == DATE;
}
boolean isString()
{
return (flags & STRING) == STRING;
}
BasicFormat makeFormat(FormatLocale locale)
{
if (isDate()) {
return new DateFormat(code, token, locale, false);
} else if (isNumeric()) {
return new LiteralFormat(code, token);
} else if (isString()) {
throw new Error();
} else {
return new LiteralFormat(token);
}
}
};
/**
* BasicFormat is the interface implemented by the classes which do all
* the work. Whereas {@link Format} has only one method for formatting,
* {@link Format#format(Object)}, this class provides methods for several
* primitive types. To make it easy to combine formatting objects, all
* methods write to a {@link PrintWriter}.
*
* The base implementation of most of these methods throws an error, there
* is no requirement that a derived class implements all of these methods.
* It is up to {@link Format#parseFormatString} to ensure that, for example,
* the {@link #format(double,PrintWriter)} method is never called for
* {@link DateFormat}.
*/
static class BasicFormat {
int code;
BasicFormat() {
this(0);
}
BasicFormat(int code) {
this.code = code;
}
boolean isNumeric() {
return false;
}
boolean isDate() {
return false;
}
boolean isString() {
return false;
}
void format(Object o, PrintWriter pw) {
if (o == null) {
formatNull(pw);
} else if (o instanceof Double) {
format(((Double) o).doubleValue(), pw);
} else if (o instanceof Float) {
format(((Float) o).floatValue(), pw);
} else if (o instanceof Integer) {
format(((Integer) o).intValue(), pw);
} else if (o instanceof Long) {
format(((Long) o).longValue(), pw);
} else if (o instanceof Short) {
format(((Short) o).shortValue(), pw);
} else if (o instanceof Byte) {
format(((Byte) o).byteValue(), pw);
} else if (o instanceof BigDecimal) {
format(((BigDecimal) o).doubleValue(), pw);
} else if (o instanceof BigInteger) {
format(((BigInteger) o).longValue(), pw);
} else if (o instanceof String) {
format((String) o, pw);
} else if (o instanceof java.util.Date) {
// includes java.sql.Date, java.sql.Time and java.sql.Timestamp
format((Date) o, pw);
} else {
pw.print(o.toString());
}
}
void formatNull(PrintWriter pw) {
pw.print("(null)");
}
void format(double d, PrintWriter pw) {
throw new Error();
}
void format(long n, PrintWriter pw) {
throw new Error();
}
void format(String s, PrintWriter pw) {
throw new Error();
}
void format(Date date, PrintWriter pw) {
Calendar calendar = Calendar.getInstance(); // todo: use locale
calendar.setTime(date);
format(calendar, pw);
}
void format(Calendar calendar, PrintWriter pw) {
throw new Error();
}
};
/**
* AlternateFormat is an implementation of {@link BasicFormat} which
* allows a different format to be used for different kinds of values. If
* there are 4 formats, purposes are as follows:<ol>
* <li>positive numbers</li>
* <li>negative numbers</li>
* <li>zero</li>
* <li>null values</li>
* </ol>
*
* If there are fewer than 4 formats, the first is used as a fall-back.
* See the <a href="http://www.apostate.com/programming/vb-format.html">the
* visual basic format specification</a> for more details.
*/
static class AlternateFormat extends BasicFormat {
BasicFormat[] formats;
AlternateFormat(BasicFormat[] formats)
{
this.formats = formats;
}
void formatNull(PrintWriter pw) {
if (formats.length >= 4) {
formats[3].format(0, pw);
} else {
super.formatNull(pw);
}
}
void format(double n, PrintWriter pw) {
if (formats.length == 0) {
pw.print(n);
} else {
int i;
if (n == 0 &&
formats.length >= 3 &&
formats[2] != null) {
i = 2;
} else if (n < 0 &&
formats.length >= 2 &&
formats[1] != null) {
n = -n;
i = 1;
} else {
i = 0;
}
formats[i].format(n, pw);
}
}
void format(long n, PrintWriter pw) {
if (formats.length == 0) {
pw.print(n);
} else {
int i;
if (n == 0 &&
formats.length >= 3 &&
formats[2] != null) {
i = 2;
} else if (n < 0 &&
formats.length >= 2 &&
formats[1] != null) {
n = -n;
i = 1;
} else {
i = 0;
}
formats[i].format(n, pw);
}
}
void format(String s, PrintWriter pw) {
formats[0].format(s, pw);
}
void format(Date date, PrintWriter pw) {
formats[0].format(date, pw);
}
void format(Calendar calendar, PrintWriter pw) {
formats[0].format(calendar, pw);
}
};
/**
* LiteralFormat is an implementation of {@link BasicFormat} which prints
* a constant value, regardless of the value to be formatted.
*
* @see CompoundFormat
*/
static class LiteralFormat extends BasicFormat
{
String s;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -