2
|
1 |
/*
|
|
2 |
* Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 4965770 4992540 5030716
|
|
27 |
* @compile -source 1.5 StockName.java
|
|
28 |
* @run main StockName
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.nio.CharBuffer;
|
|
32 |
import java.util.Formatter;
|
|
33 |
import java.util.Formattable;
|
|
34 |
import java.util.Locale;
|
|
35 |
import static java.util.FormattableFlags.*;
|
|
36 |
|
|
37 |
public class StockName implements Formattable {
|
|
38 |
private String symbol, companyName, frenchCompanyName;
|
|
39 |
|
|
40 |
public StockName(String symbol, String companyName,
|
|
41 |
String frenchCompanyName)
|
|
42 |
{
|
|
43 |
this.symbol = symbol;
|
|
44 |
this.companyName = companyName;
|
|
45 |
this.frenchCompanyName = frenchCompanyName;
|
|
46 |
}
|
|
47 |
|
|
48 |
public void formatTo(Formatter fmt, int f, int width, int precision) {
|
|
49 |
StringBuilder sb = new StringBuilder();
|
|
50 |
|
|
51 |
// decide form of name
|
|
52 |
String name = companyName;
|
|
53 |
if (fmt.locale().equals(Locale.FRANCE))
|
|
54 |
name = frenchCompanyName;
|
|
55 |
boolean alternate = (f & ALTERNATE) == ALTERNATE;
|
|
56 |
boolean usesymbol = alternate || (precision != -1 && precision < 10);
|
|
57 |
String out = (usesymbol ? symbol : name);
|
|
58 |
|
|
59 |
// apply precision
|
|
60 |
if (precision == -1 || out.length() < precision) {
|
|
61 |
// write it all
|
|
62 |
sb.append(out);
|
|
63 |
} else {
|
|
64 |
sb.append(out.substring(0, precision - 1)).append('*');
|
|
65 |
}
|
|
66 |
|
|
67 |
// apply width and justification
|
|
68 |
int len = sb.length();
|
|
69 |
if (len < width)
|
|
70 |
for (int i = 0; i < width - len; i++)
|
|
71 |
if ((f & LEFT_JUSTIFY) == LEFT_JUSTIFY)
|
|
72 |
sb.append(' ');
|
|
73 |
else
|
|
74 |
sb.insert(0, ' ');
|
|
75 |
|
|
76 |
fmt.format(sb.toString());
|
|
77 |
}
|
|
78 |
|
|
79 |
public String toString() {
|
|
80 |
return String.format("%s - %s", symbol, companyName);
|
|
81 |
}
|
|
82 |
|
|
83 |
public static void main(String [] args) {
|
|
84 |
StockName sn = new StockName("HUGE", "Huge Fruit, Inc.",
|
|
85 |
"Fruit Titanesque, Inc.");
|
|
86 |
CharBuffer cb = CharBuffer.allocate(128);
|
|
87 |
Formatter fmt = new Formatter(cb);
|
|
88 |
|
|
89 |
fmt.format("%s", sn); // -> "Huge Fruit, Inc."
|
|
90 |
test(cb, "Huge Fruit, Inc.");
|
|
91 |
|
|
92 |
fmt.format("%s", sn.toString()); // -> "HUGE - Huge Fruit, Inc."
|
|
93 |
test(cb, "HUGE - Huge Fruit, Inc.");
|
|
94 |
|
|
95 |
fmt.format("%#s", sn); // -> "HUGE"
|
|
96 |
test(cb, "HUGE");
|
|
97 |
|
|
98 |
fmt.format("%-10.8s", sn); // -> "HUGE "
|
|
99 |
test(cb, "HUGE ");
|
|
100 |
|
|
101 |
fmt.format("%.12s", sn); // -> "Huge Fruit,*"
|
|
102 |
test(cb, "Huge Fruit,*");
|
|
103 |
|
|
104 |
fmt.format(Locale.FRANCE, "%25s", sn);
|
|
105 |
// -> " Fruit Titanesque, Inc."
|
|
106 |
test(cb, " Fruit Titanesque, Inc.");
|
|
107 |
}
|
|
108 |
|
|
109 |
private static void test(CharBuffer cb, String exp) {
|
|
110 |
cb.limit(cb.position());
|
|
111 |
cb.rewind();
|
|
112 |
if (!cb.toString().equals(exp))
|
|
113 |
throw new RuntimeException("expect: '" + exp + "'; got: '"
|
|
114 |
+ cb.toString() + "'");
|
|
115 |
cb.clear();
|
|
116 |
}
|
|
117 |
}
|