Path: blob/master/test/jdk/java/util/Formattable/StockName.java
41149 views
/*1* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4965770 4992540 503071626*/2728import java.nio.CharBuffer;29import java.util.Formatter;30import java.util.Formattable;31import java.util.Locale;32import static java.util.FormattableFlags.*;3334public class StockName implements Formattable {35private String symbol, companyName, frenchCompanyName;3637public StockName(String symbol, String companyName,38String frenchCompanyName)39{40this.symbol = symbol;41this.companyName = companyName;42this.frenchCompanyName = frenchCompanyName;43}4445public void formatTo(Formatter fmt, int f, int width, int precision) {46StringBuilder sb = new StringBuilder();4748// decide form of name49String name = companyName;50if (fmt.locale().equals(Locale.FRANCE))51name = frenchCompanyName;52boolean alternate = (f & ALTERNATE) == ALTERNATE;53boolean usesymbol = alternate || (precision != -1 && precision < 10);54String out = (usesymbol ? symbol : name);5556// apply precision57if (precision == -1 || out.length() < precision) {58// write it all59sb.append(out);60} else {61sb.append(out.substring(0, precision - 1)).append('*');62}6364// apply width and justification65int len = sb.length();66if (len < width)67for (int i = 0; i < width - len; i++)68if ((f & LEFT_JUSTIFY) == LEFT_JUSTIFY)69sb.append(' ');70else71sb.insert(0, ' ');7273fmt.format(sb.toString());74}7576public String toString() {77return String.format("%s - %s", symbol, companyName);78}7980public static void main(String [] args) {81StockName sn = new StockName("HUGE", "Huge Fruit, Inc.",82"Fruit Titanesque, Inc.");83CharBuffer cb = CharBuffer.allocate(128);84Formatter fmt = new Formatter(cb);8586fmt.format("%s", sn); // -> "Huge Fruit, Inc."87test(cb, "Huge Fruit, Inc.");8889fmt.format("%s", sn.toString()); // -> "HUGE - Huge Fruit, Inc."90test(cb, "HUGE - Huge Fruit, Inc.");9192fmt.format("%#s", sn); // -> "HUGE"93test(cb, "HUGE");9495fmt.format("%-10.8s", sn); // -> "HUGE "96test(cb, "HUGE ");9798fmt.format("%.12s", sn); // -> "Huge Fruit,*"99test(cb, "Huge Fruit,*");100101fmt.format(Locale.FRANCE, "%25s", sn);102// -> " Fruit Titanesque, Inc."103test(cb, " Fruit Titanesque, Inc.");104}105106private static void test(CharBuffer cb, String exp) {107cb.limit(cb.position());108cb.rewind();109if (!cb.toString().equals(exp))110throw new RuntimeException("expect: '" + exp + "'; got: '"111+ cb.toString() + "'");112cb.clear();113}114}115116117