use of java.util.Formattable in project robovm by robovm.
the class OldFormatterTest method test_Formattable.
public void test_Formattable() {
Formattable ones = new Formattable() {
public void formatTo(Formatter formatter, int flags, int width, int precision) throws IllegalFormatException {
try {
formatter.out().append("111");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
Formattable twos = new Formattable() {
public void formatTo(Formatter formatter, int flags, int width, int precision) throws IllegalFormatException {
try {
formatter.out().append("222");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
assertEquals("aaa 111?", new Formatter().format("aaa %s?", ones).toString());
assertEquals("aaa 111 bbb 222?", new Formatter().format("aaa %s bbb %s?", ones, twos).toString());
}
use of java.util.Formattable in project commons-text by apache.
the class FormattableUtilsTest method testSimplestFormat.
@Test
public void testSimplestFormat() {
final Formattable formattable = new SimplestFormattable("foo");
assertThat(FormattableUtils.toString(formattable)).isEqualTo("foo");
}
use of java.util.Formattable in project graal by oracle.
the class HotSpotGraalCompiler method fmt.
/**
* Wraps {@code obj} in a {@link Formatter} that standardizes formatting for certain objects.
*/
static Formattable fmt(Object obj) {
return new Formattable() {
@Override
public void formatTo(Formatter buf, int flags, int width, int precision) {
if (obj instanceof Throwable) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
((Throwable) obj).printStackTrace(new PrintStream(baos));
buf.format("%s", baos.toString());
} else if (obj instanceof StackTraceElement[]) {
for (StackTraceElement e : (StackTraceElement[]) obj) {
buf.format("\t%s%n", e);
}
} else if (obj instanceof JavaMethod) {
buf.format("%s", str((JavaMethod) obj));
} else {
buf.format("%s", obj);
}
}
};
}
use of java.util.Formattable in project j2objc by google.
the class FormatterTest method test_formatLjava_lang_String$Ljava_lang_Object_GeneralConversionOther.
/**
* java.util.Formatter#format(String, Object...) for general
* conversion other cases
*/
public void test_formatLjava_lang_String$Ljava_lang_Object_GeneralConversionOther() {
/*
* In Turkish locale, the upper case of '\u0069' is '\u0130'. The
* following test indicate that '\u0069' is coverted to upper case
* without using the turkish locale.
*/
Formatter f = new Formatter(new Locale("tr"));
f.format("%S", "\u0069");
// assertEquals("\u0049", f.toString()); J2ObjC changed.
assertEquals("\u0130", f.toString());
final Object[] input = { Boolean.FALSE, Boolean.TRUE, new Character('c'), new Byte((byte) 0x01), new Short((short) 0x0001), new Integer(1), new Float(1.1f), new Double(1.1d), "", "string content", new MockFormattable(), (Object) null };
f = new Formatter(Locale.GERMAN);
for (int i = 0; i < input.length; i++) {
if (!(input[i] instanceof Formattable)) {
try {
f.format("%#s", input[i]);
/*
* fail on RI, spec says if the '#' flag is present and the
* argument is not a Formattable , then a
* FormatFlagsConversionMismatchException will be thrown.
*/
fail("should throw FormatFlagsConversionMismatchException");
} catch (FormatFlagsConversionMismatchException e) {
// expected
}
} else {
f.format("%#s%<-#8s", input[i]);
assertEquals("customized format function width: -1 precision: -1customized format function width: 8 precision: -1", f.toString());
}
}
}
Aggregations