Java Code Examples for java.util.Formatter#out()

The following examples show how to use java.util.Formatter#out() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MiscUtil.java    From JDA with Apache License 2.0 6 votes vote down vote up
/**
 * Can be used to append a String to a formatter.
 *
 * @param formatter
 *        The {@link java.util.Formatter Formatter}
 * @param width
 *        Minimum width to meet, filled with space if needed
 * @param precision
 *        Maximum amount of characters to append
 * @param leftJustified
 *        Whether or not to left-justify the value
 * @param out
 *        The String to append
 */
public static void appendTo(Formatter formatter, int width, int precision, boolean leftJustified, String out)
{
    try
    {
        Appendable appendable = formatter.out();
        if (precision > -1 && out.length() > precision)
        {
            appendable.append(Helpers.truncate(out, precision));
            return;
        }

        if (leftJustified)
            appendable.append(Helpers.rightPad(out, width));
        else
            appendable.append(Helpers.leftPad(out, width));
    }
    catch (IOException e)
    {
        throw new UncheckedIOException(e);
    }
}
 
Example 2
Source File: AbstractMessage.java    From JDA with Apache License 2.0 6 votes vote down vote up
protected void appendFormat(Formatter formatter, int width, int precision, boolean leftJustified, String out)
{
    try
    {
        Appendable appendable = formatter.out();
        if (precision > -1 && out.length() > precision)
        {
            appendable.append(Helpers.truncate(out, precision - 3)).append("...");
            return;
        }

        if (leftJustified)
            appendable.append(Helpers.rightPad(out, width));
        else
            appendable.append(Helpers.leftPad(out, width));
    }
    catch (IOException e)
    {
        throw new UncheckedIOException(e);
    }
}
 
Example 3
Source File: FormatterTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * java.util.Formatter#Formatter(Appendable)
 */
public void test_ConstructorLjava_lang_Appendable() {
    MockAppendable ma = new MockAppendable();
    Formatter f1 = new Formatter(ma);
    assertEquals(ma, f1.out());
    assertEquals(f1.locale(), Locale.getDefault());
    assertNotNull(f1.toString());

    Formatter f2 = new Formatter((Appendable) null);
    /*
     * If a(the input param) is null then a StringBuilder will be created
     * and the output can be attained by invoking the out() method. But RI
     * raises an error of FormatterClosedException when invoking out() or
     * toString().
     */
    Appendable sb = f2.out();
    assertTrue(sb instanceof StringBuilder);
    assertNotNull(f2.toString());
}