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