1 *java.util.Formattable* *Formattable* The Formattable interface must be implemen
3 public interface interface Formattable
6 |java.util.Formattable_Description|
7 |java.util.Formattable_Fields|
8 |java.util.Formattable_Constructors|
9 |java.util.Formattable_Methods|
11 ================================================================================
13 *java.util.Formattable_Methods*
14 |java.util.Formattable.formatTo(Formatter,int,int,int)|Formats the object using
16 *java.util.Formattable_Description*
18 The Formattable interface must be implemented by any class that needs to
19 perform custom formatting using the 's' conversion specifier of
20 (|java.util.Formatter|) . This interface allows basic control for formatting
23 For example, the following class prints out different representations of a
24 stock's name depending on the flags and length constraints:
28 import java.nio.CharBuffer; import java.util.Formatter; import
29 java.util.Formattable; import java.util.Locale; import static
30 java.util.FormattableFlags.*;
34 public class StockName implements Formattable { private String symbol,
35 companyName, frenchCompanyName; public StockName(String symbol, String
36 companyName, String frenchCompanyName) { ... }
40 public void formatTo(Formatter fmt, int f, int width, int precision) {
41 StringBuilder sb = new StringBuilder();
43 // decide form of name String name = companyName; if
44 (fmt.locale().equals(Locale.FRANCE)) name = frenchCompanyName; boolean
45 alternate = (f & ALTERNATE) == ALTERNATE; boolean usesymbol = alternate ||
46 (precision != -1 precision
48 When used in conjunction with the (|java.util.Formatter|) , the above class
49 produces the following output for various format strings.
53 Formatter fmt = new Formatter(); StockName sn = new StockName("HUGE", "Huge
54 Fruit, Inc.", "Fruit Titanesque, Inc."); fmt.format("%s", sn); // -> "Huge
55 Fruit, Inc." fmt.format("%s", sn.toString()); // -> "HUGE - Huge Fruit, Inc."
56 fmt.format("%#s", sn); // -> "HUGE" fmt.format("%-10.8s", sn); // -> "HUGE "
57 fmt.format("%.12s", sn); // -> "Huge Fruit,*" fmt.format(Locale.FRANCE, "%25s",
58 sn); // -> " Fruit Titanesque, Inc."
60 Formattables are not necessarily safe for multithreaded access. Thread safety
61 is optional and may be enforced by classes that extend and implement this
64 Unless otherwise specified, passing a null argument to any method in this
65 interface will cause a (|java.lang.NullPointerException|) to be thrown.
68 *java.util.Formattable.formatTo(Formatter,int,int,int)*
71 java.util.Formatter formatter,
76 Formats the object using the provided formatter(|java.util.Formatter|) .
78 formatter - The {@link Formatter formatter}. Implementing classes may call {@link
79 Formatter#out() formatter.out()} or {@link Formatter#locale()
80 formatter.locale()} to obtain the {@link Appendable} or {@link Locale}
81 used by this formatter respectively.
82 flags - The flags modify the output format. The value is interpreted as a bitmask. Any
83 combination of the following flags may be set: {@link
84 FormattableFlags#LEFT_JUSTIFY}, {@link FormattableFlags#UPPERCASE}, and
85 {@link FormattableFlags#ALTERNATE}. If no flags are set, the default
86 formatting of the implementing class will apply.
87 width - The minimum number of characters to be written to the output. If the length of
88 the converted value is less than the width then the output will be
89 padded by '' until the total number of characters equals width. The
90 padding is at the beginning by default. If the {@link
91 FormattableFlags#LEFT_JUSTIFY} flag is set then the padding will be at
92 the end. If width is -1 then there is no minimum.
93 precision - The maximum number of characters to be written to the output. The precision is
94 applied before the width, thus the output will be truncated to precision
95 characters even if the width is greater than the precision. If precision
96 is -1 then there is no explicit limit on the number of characters.