下面列出了怎么用java.util.FormatFlagsConversionMismatchException的API类实例代码及写法,或者点击链接到github查看源代码。
private static void assertFormatFlagsConversionMismatchException(String format, Object arg) {
try {
log(format, arg);
fail("expected FormatFlagsConversionMismatchException");
} catch (FormatFlagsConversionMismatchException expected) {
}
}
private void assertFormatFlagsConversionMismatchException(Formatter f, String str) {
try {
f.format(str);
fail("should throw FormatFlagsConversionMismatchException: "
+ str);
/*
* error on RI, throw IllegalFormatFlagsException specification
* says FormatFlagsConversionMismatchException should be thrown
*/
} catch (FormatFlagsConversionMismatchException e) {
// expected
}
}
FormatSpecifier(String source, String[] sa)
throws FormatFlagsConversionMismatchException,
FormatterNumberFormatException {
int idx = 0;
this.source = source;
index(sa[idx++]);
flags(sa[idx++]);
width(sa[idx++]);
precision(sa[idx++]);
if (sa[idx] != null) {
dt = true;
if (sa[idx].equals("T"))
f.add(Flags.UPPERCASE);
}
conversion(sa[++idx]);
if (dt)
checkDateTime();
else if (Conversion.isGeneral(c))
checkGeneral();
else if (Conversion.isCharacter(c))
checkCharacter();
else if (Conversion.isInteger(c))
checkInteger();
else if (Conversion.isFloat(c))
checkFloat();
else if (Conversion.isText(c))
checkText();
else
throw new UnknownFormatConversionException(String.valueOf(c));
}
private void checkGeneral() throws FormatFlagsConversionMismatchException {
if ((c == Conversion.BOOLEAN || c == Conversion.HASHCODE)
&& f.contains(Flags.ALTERNATE))
failMismatch(Flags.ALTERNATE, c);
// '-' requires a width
if (width == -1 && f.contains(Flags.LEFT_JUSTIFY))
throw new MissingFormatWidthException(toString());
checkBadFlags(Flags.PLUS, Flags.LEADING_SPACE, Flags.ZERO_PAD,
Flags.GROUP, Flags.PARENTHESES);
}
private void checkDateTime() throws FormatFlagsConversionMismatchException {
if (precision != -1)
throw new IllegalFormatPrecisionException(precision);
if (!DateTime.isValid(c))
throw new UnknownFormatConversionException("t" + c);
checkBadFlags(Flags.ALTERNATE, Flags.PLUS, Flags.LEADING_SPACE,
Flags.ZERO_PAD, Flags.GROUP, Flags.PARENTHESES);
// '-' requires a width
if (width == -1 && f.contains(Flags.LEFT_JUSTIFY))
throw new MissingFormatWidthException(toString());
}
private void checkCharacter() throws FormatFlagsConversionMismatchException {
if (precision != -1)
throw new IllegalFormatPrecisionException(precision);
checkBadFlags(Flags.ALTERNATE, Flags.PLUS, Flags.LEADING_SPACE,
Flags.ZERO_PAD, Flags.GROUP, Flags.PARENTHESES);
// '-' requires a width
if (width == -1 && f.contains(Flags.LEFT_JUSTIFY))
throw new MissingFormatWidthException(toString());
}
private void checkInteger() throws FormatFlagsConversionMismatchException {
checkNumeric();
if (precision != -1)
throw new IllegalFormatPrecisionException(precision);
if (c == Conversion.DECIMAL_INTEGER)
checkBadFlags(Flags.ALTERNATE);
else
checkBadFlags(Flags.GROUP);
}
private void checkFloat() throws FormatFlagsConversionMismatchException {
checkNumeric();
if (c == Conversion.DECIMAL_FLOAT) {
} else if (c == Conversion.HEXADECIMAL_FLOAT) {
checkBadFlags(Flags.PARENTHESES, Flags.GROUP);
} else if (c == Conversion.SCIENTIFIC) {
checkBadFlags(Flags.GROUP);
} else if (c == Conversion.GENERAL) {
checkBadFlags(Flags.ALTERNATE);
}
}
private void printInteger(String arg)
throws IllegalFormatConversionException,
FormatFlagsConversionMismatchException {
if (mightBeUnknown(arg))
return;
if (matchSig(arg, Byte.class, Short.class, Integer.class, Long.class))
printLong();
else if (matchSig(arg, BigInteger.class)) {
} else
failConversion(arg);
}
private void printLong() throws FormatFlagsConversionMismatchException {
if (c == Conversion.OCTAL_INTEGER) {
checkBadFlags(Flags.PARENTHESES, Flags.LEADING_SPACE, Flags.PLUS);
} else if (c == Conversion.HEXADECIMAL_INTEGER) {
checkBadFlags(Flags.PARENTHESES, Flags.LEADING_SPACE, Flags.PLUS);
}
}
private static List<FormatSpecifier> parse(String s)
throws FormatFlagsConversionMismatchException,
FormatterNumberFormatException {
ArrayList<FormatSpecifier> al = new ArrayList<FormatSpecifier>();
Matcher m = fsPattern.matcher(s);
int i = 0;
while (i < s.length()) {
if (m.find(i)) {
// Anything between the start of the string and the beginning
// of the format specifier is either fixed text or contains
// an invalid format string.
if (m.start() != i) {
// Make sure we didn't miss any invalid format specifiers
checkText(s.substring(i, m.start()));
}
// Expect 6 groups in regular expression
String[] sa = new String[6];
for (int j = 0; j < m.groupCount(); j++) {
sa[j] = m.group(j + 1);
}
al.add(new FormatSpecifier(m.group(0), sa));
i = m.end();
} else {
// No more valid format specifiers. Check for possible invalid
// format specifiers.
checkText(s.substring(i));
// The rest of the string is fixed text
break;
}
}
return al;
}
private void checkBadFlags(Flags... badFlags)
throws FormatFlagsConversionMismatchException {
for (int i = 0; i < badFlags.length; i++)
if (f.contains(badFlags[i]))
failMismatch(badFlags[i], c);
}
public void print(String arg, int argIndex)
throws IllegalFormatConversionException,
FormatFlagsConversionMismatchException {
try {
if (arg.charAt(0) == '[')
failConversion(arg);
if (dt) {
printDateTime(arg);
return;
}
switch (c) {
case Conversion.DECIMAL_INTEGER:
case Conversion.OCTAL_INTEGER:
case Conversion.HEXADECIMAL_INTEGER:
printInteger(arg);
break;
case Conversion.SCIENTIFIC:
case Conversion.GENERAL:
case Conversion.DECIMAL_FLOAT:
case Conversion.HEXADECIMAL_FLOAT:
printFloat(arg);
break;
case Conversion.CHARACTER:
case Conversion.CHARACTER_UPPER:
printCharacter(arg);
break;
case Conversion.BOOLEAN:
printBoolean(arg);
break;
case Conversion.STRING:
case Conversion.HASHCODE:
case Conversion.LINE_SEPARATOR:
case Conversion.PERCENT_SIGN:
break;
default:
throw new UnknownFormatConversionException(String.valueOf(c));
}
} catch (IllegalFormatConversionException e) {
e.setArgIndex(argIndex);
throw e;
}
}
private void failMismatch(Flags f, char c)
throws FormatFlagsConversionMismatchException {
String fs = f.toString();
throw new FormatFlagsConversionMismatchException(fs, c);
}
public static void check(String format, String... args)
throws ExtraFormatArgumentsException,
IllegalFormatConversionException, IllegalFormatException,
FormatFlagsConversionMismatchException,
MissingFormatArgumentException, FormatterNumberFormatException {
// index of last argument referenced
int last = -1;
// last ordinary index
int lasto = -1;
// last index used
int maxIndex = -1;
for (FormatSpecifier fs : parse(format)) {
int index = fs.index();
switch (index) {
case -2:
// ignore it
break;
case -1: // relative index
if (last < 0 || last > args.length - 1)
throw new MissingFormatArgumentException(last,
fs.toString());
fs.print(args[last], last);
break;
case 0: // ordinary index
lasto++;
last = lasto;
if (lasto > args.length - 1)
throw new MissingFormatArgumentException(lasto,
fs.toString());
maxIndex = Math.max(maxIndex, lasto);
fs.print(args[lasto], lasto);
break;
default: // explicit index
last = index - 1;
if (last > args.length - 1)
throw new MissingFormatArgumentException(last,
fs.toString());
maxIndex = Math.max(maxIndex, last);
fs.print(args[last], last);
break;
}
}
if (maxIndex < args.length - 1) {
throw new ExtraFormatArgumentsException(args.length, maxIndex + 1);
}
}