java.util.Formatter#out ( )源码实例Demo

下面列出了java.util.Formatter#out ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: JDA   文件: MiscUtil.java
/**
 * 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);
    }
}
 
源代码2 项目: JDA   文件: AbstractMessage.java
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);
    }
}
 
源代码3 项目: j2objc   文件: FormatterTest.java
/**
 * 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());
}