jdk/test/java/util/Formattable/StockName.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 4347 ab0a9f495844
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
     2
 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
4347
ab0a9f495844 6907177: Update jdk tests to remove unncessary -source and -target options
darcy
parents: 2
diff changeset
    24
/*
2
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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Formatter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Formattable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import static java.util.FormattableFlags.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class StockName implements Formattable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private String symbol, companyName, frenchCompanyName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    public StockName(String symbol, String companyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
                     String frenchCompanyName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        this.symbol = symbol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        this.companyName = companyName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        this.frenchCompanyName = frenchCompanyName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public void formatTo(Formatter fmt, int f, int width, int precision) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        // decide form of name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        String name = companyName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        if (fmt.locale().equals(Locale.FRANCE))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            name = frenchCompanyName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        boolean alternate = (f & ALTERNATE) == ALTERNATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        boolean usesymbol = alternate || (precision != -1 && precision < 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        String out = (usesymbol ? symbol : name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        // apply precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (precision == -1 || out.length() < precision) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            // write it all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            sb.append(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            sb.append(out.substring(0, precision - 1)).append('*');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        // apply width and justification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        int len = sb.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (len < width)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            for (int i = 0; i < width - len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                if ((f & LEFT_JUSTIFY) == LEFT_JUSTIFY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    sb.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    sb.insert(0, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        fmt.format(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return String.format("%s - %s", symbol, companyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public static void main(String [] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        StockName sn = new StockName("HUGE", "Huge Fruit, Inc.",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                     "Fruit Titanesque, Inc.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        CharBuffer cb = CharBuffer.allocate(128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        Formatter fmt = new Formatter(cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        fmt.format("%s", sn);            //   -> "Huge Fruit, Inc."
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        test(cb, "Huge Fruit, Inc.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        fmt.format("%s", sn.toString()); //   -> "HUGE - Huge Fruit, Inc."
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        test(cb, "HUGE - Huge Fruit, Inc.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        fmt.format("%#s", sn);           //   -> "HUGE"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        test(cb, "HUGE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        fmt.format("%-10.8s", sn);       //   -> "HUGE      "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        test(cb, "HUGE      ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        fmt.format("%.12s", sn);         //   -> "Huge Fruit,*"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        test(cb, "Huge Fruit,*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        fmt.format(Locale.FRANCE, "%25s", sn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                         //   -> "   Fruit Titanesque, Inc."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        test(cb, "   Fruit Titanesque, Inc.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static void test(CharBuffer cb, String exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        cb.limit(cb.position());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        cb.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (!cb.toString().equals(exp))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            throw new RuntimeException("expect: '" + exp + "'; got: '"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                       + cb.toString() + "'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        cb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
}