34 * control for formatting arbitrary objects. |
34 * control for formatting arbitrary objects. |
35 * |
35 * |
36 * For example, the following class prints out different representations of a |
36 * For example, the following class prints out different representations of a |
37 * stock's name depending on the flags and length constraints: |
37 * stock's name depending on the flags and length constraints: |
38 * |
38 * |
39 * {@code |
39 * <pre> {@code |
40 * import java.nio.CharBuffer; |
40 * import java.nio.CharBuffer; |
41 * import java.util.Formatter; |
41 * import java.util.Formatter; |
42 * import java.util.Formattable; |
42 * import java.util.Formattable; |
43 * import java.util.Locale; |
43 * import java.util.Locale; |
44 * import static java.util.FormattableFlags.*; |
44 * import static java.util.FormattableFlags.*; |
45 * |
45 * |
46 * ... |
46 * ... |
47 * |
47 * |
48 * public class StockName implements Formattable { |
48 * public class StockName implements Formattable { |
49 * private String symbol, companyName, frenchCompanyName; |
49 * private String symbol, companyName, frenchCompanyName; |
50 * public StockName(String symbol, String companyName, |
50 * public StockName(String symbol, String companyName, |
51 * String frenchCompanyName) { |
51 * String frenchCompanyName) { |
87 * |
87 * |
88 * public String toString() { |
88 * public String toString() { |
89 * return String.format("%s - %s", symbol, companyName); |
89 * return String.format("%s - %s", symbol, companyName); |
90 * } |
90 * } |
91 * } |
91 * } |
92 * } |
92 * }</pre> |
93 * |
93 * |
94 * <p> When used in conjunction with the {@link java.util.Formatter}, the above |
94 * <p> When used in conjunction with the {@link java.util.Formatter}, the above |
95 * class produces the following output for various format strings. |
95 * class produces the following output for various format strings. |
96 * |
96 * |
97 * {@code |
97 * <pre> {@code |
98 * Formatter fmt = new Formatter(); |
98 * Formatter fmt = new Formatter(); |
99 * StockName sn = new StockName("HUGE", "Huge Fruit, Inc.", |
99 * StockName sn = new StockName("HUGE", "Huge Fruit, Inc.", |
100 * "Fruit Titanesque, Inc."); |
100 * "Fruit Titanesque, Inc."); |
101 * fmt.format("%s", sn); // -> "Huge Fruit, Inc." |
101 * fmt.format("%s", sn); // -> "Huge Fruit, Inc." |
102 * fmt.format("%s", sn.toString()); // -> "HUGE - Huge Fruit, Inc." |
102 * fmt.format("%s", sn.toString()); // -> "HUGE - Huge Fruit, Inc." |
103 * fmt.format("%#s", sn); // -> "HUGE" |
103 * fmt.format("%#s", sn); // -> "HUGE" |
104 * fmt.format("%-10.8s", sn); // -> "HUGE " |
104 * fmt.format("%-10.8s", sn); // -> "HUGE " |
105 * fmt.format("%.12s", sn); // -> "Huge Fruit,*" |
105 * fmt.format("%.12s", sn); // -> "Huge Fruit,*" |
106 * fmt.format(Locale.FRANCE, "%25s", sn); // -> " Fruit Titanesque, Inc." |
106 * fmt.format(Locale.FRANCE, "%25s", sn); // -> " Fruit Titanesque, Inc." |
107 * } |
107 * }</pre> |
108 * |
108 * |
109 * <p> Formattables are not necessarily safe for multithreaded access. Thread |
109 * <p> Formattables are not necessarily safe for multithreaded access. Thread |
110 * safety is optional and may be enforced by classes that extend and implement |
110 * safety is optional and may be enforced by classes that extend and implement |
111 * this interface. |
111 * this interface. |
112 * |
112 * |