/** * Format a <tt>float</tt>. * @param flt float to format * @return formatted float */ public FormatStringBuffer format(float flt) { return format((double) flt); }
/** * Reset this <tt>FormatStringBuffer</tt>. * * @param format the format string. * @return format reset */ public FormatStringBuffer reset(String format) { reset(); this.format = format; return this; }
FormatStringBuffer fsb = new FormatStringBuffer("[%s]"); fsb.reset("[%s]").format("test"); test("[test]", fsb.toString()); fsb.reset("[%5s]").format("test"); test("[ test]", fsb.toString()); fsb.reset("[%-5s]").format("test"); test("[test ]", fsb.toString()); fsb.reset("[%5.2s]").format("test"); test("[ te]", fsb.toString()); fsb.reset("[%-5.2s]").format("test"); test("[te ]", fsb.toString()); fsb.reset("[%c]").format('A'); test("[A]", fsb.toString()); fsb.reset("[%2c]").format('A'); test("[ A]", fsb.toString()); fsb.reset("[%-2c]").format('A'); test("[A ]", fsb.toString()); fsb.reset("[%f]").format(3.1415); test("[3.1415]", fsb.toString());
/** * Format a <tt>char</tt>. * @param ch char to format * @return formatted char */ public FormatStringBuffer format(char ch) { Format fmt = getFormat(); if (fmt.type != CHAR) throw new IllegalArgumentException("Expected a char format"); if ((fmt.flags & LEFT) != LEFT) while (--fmt.fieldWidth > 0) buffer.append(' '); buffer.append(ch); while (--fmt.fieldWidth > 0) buffer.append(' '); return this; }
fmt.fieldWidth = skipDigits(); throw new IllegalArgumentException("Malformed format"); fmt.precision = skipDigits(); if (fmt.precision < 0) { fmt.precision = 0;
/** * Format a <tt>String</tt>. * @param str string to format * @return formatted string */ public FormatStringBuffer format(String str) { if (str == null) str = "<NULL>"; Format fmt = getFormat(); if (fmt.type != STRING) throw new IllegalArgumentException("Expected a String format"); int len = str.length(); if (fmt.precision != -1 && len > fmt.precision) len = fmt.precision; if ((fmt.flags & LEFT) != LEFT) while (len < fmt.fieldWidth--) buffer.append(' '); for (int i = 0; i < len; ++i) buffer.append(str.charAt(i)); while (len < fmt.fieldWidth--) buffer.append(' '); return this; }
Format fmt = getFormat();
/** * Create a new <tt>FormatStringBuffer</tt>. * * @param format the format string. */ public FormatStringBuffer(String format) { reset(format); }
/** * Format a <tt>float</tt>. * @param i int to format * @return formatted int */ public FormatStringBuffer format(int i) { return format((long) i); }
Format fmt = getFormat();
/** * Get the result of the formatting. <tt>reset()</tt> is automatically * called from this method. */ public String toString() { if (index < format.length()) buffer.append(format.substring(index)); String str = buffer.toString(); this.reset(); return str; }