1/*
2 * Copyright (c) 2004, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 4965770 4992540 5030716
27 */
28
29import java.nio.CharBuffer;
30import java.util.Formatter;
31import java.util.Formattable;
32import java.util.Locale;
33import static java.util.FormattableFlags.*;
34
35public class StockName implements Formattable {
36    private String symbol, companyName, frenchCompanyName;
37
38    public StockName(String symbol, String companyName,
39                     String frenchCompanyName)
40    {
41        this.symbol = symbol;
42        this.companyName = companyName;
43        this.frenchCompanyName = frenchCompanyName;
44    }
45
46    public void formatTo(Formatter fmt, int f, int width, int precision) {
47        StringBuilder sb = new StringBuilder();
48
49        // decide form of name
50        String name = companyName;
51        if (fmt.locale().equals(Locale.FRANCE))
52            name = frenchCompanyName;
53        boolean alternate = (f & ALTERNATE) == ALTERNATE;
54        boolean usesymbol = alternate || (precision != -1 && precision < 10);
55        String out = (usesymbol ? symbol : name);
56
57        // apply precision
58        if (precision == -1 || out.length() < precision) {
59            // write it all
60            sb.append(out);
61        } else {
62            sb.append(out.substring(0, precision - 1)).append('*');
63        }
64
65        // apply width and justification
66        int len = sb.length();
67        if (len < width)
68            for (int i = 0; i < width - len; i++)
69                if ((f & LEFT_JUSTIFY) == LEFT_JUSTIFY)
70                    sb.append(' ');
71                else
72                    sb.insert(0, ' ');
73
74        fmt.format(sb.toString());
75    }
76
77    public String toString() {
78        return String.format("%s - %s", symbol, companyName);
79    }
80
81    public static void main(String [] args) {
82        StockName sn = new StockName("HUGE", "Huge Fruit, Inc.",
83                                     "Fruit Titanesque, Inc.");
84        CharBuffer cb = CharBuffer.allocate(128);
85        Formatter fmt = new Formatter(cb);
86
87        fmt.format("%s", sn);            //   -> "Huge Fruit, Inc."
88        test(cb, "Huge Fruit, Inc.");
89
90        fmt.format("%s", sn.toString()); //   -> "HUGE - Huge Fruit, Inc."
91        test(cb, "HUGE - Huge Fruit, Inc.");
92
93        fmt.format("%#s", sn);           //   -> "HUGE"
94        test(cb, "HUGE");
95
96        fmt.format("%-10.8s", sn);       //   -> "HUGE      "
97        test(cb, "HUGE      ");
98
99        fmt.format("%.12s", sn);         //   -> "Huge Fruit,*"
100        test(cb, "Huge Fruit,*");
101
102        fmt.format(Locale.FRANCE, "%25s", sn);
103                                         //   -> "   Fruit Titanesque, Inc."
104        test(cb, "   Fruit Titanesque, Inc.");
105    }
106
107    private static void test(CharBuffer cb, String exp) {
108        cb.limit(cb.position());
109        cb.rewind();
110        if (!cb.toString().equals(exp))
111            throw new RuntimeException("expect: '" + exp + "'; got: '"
112                                       + cb.toString() + "'");
113        cb.clear();
114    }
115}
116