Search in sources :

Example 1 with Formattable

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());
}
Also used : Formatter(java.util.Formatter) IOException(java.io.IOException) Formattable(java.util.Formattable)

Example 2 with Formattable

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");
}
Also used : Formattable(java.util.Formattable) Test(org.junit.Test)

Example 3 with Formattable

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);
            }
        }
    };
}
Also used : PrintStream(java.io.PrintStream) Formatter(java.util.Formatter) JavaMethod(jdk.vm.ci.meta.JavaMethod) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Formattable(java.util.Formattable)

Example 4 with Formattable

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());
        }
    }
}
Also used : Locale(java.util.Locale) FormatFlagsConversionMismatchException(java.util.FormatFlagsConversionMismatchException) Formatter(java.util.Formatter) Formattable(java.util.Formattable) BigInteger(java.math.BigInteger)

Aggregations

Formattable (java.util.Formattable)4 Formatter (java.util.Formatter)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 BigInteger (java.math.BigInteger)1 FormatFlagsConversionMismatchException (java.util.FormatFlagsConversionMismatchException)1 Locale (java.util.Locale)1 JavaMethod (jdk.vm.ci.meta.JavaMethod)1 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)1 Test (org.junit.Test)1