Path: blob/master/src/java.base/share/classes/java/util/Formattable.java
41152 views
/*1* Copyright (c) 2003, 2013, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.util;2627import java.io.IOException;2829/**30* The {@code Formattable} interface must be implemented by any class that31* needs to perform custom formatting using the {@code 's'} conversion32* specifier of {@link java.util.Formatter}. This interface allows basic33* control for formatting arbitrary objects.34*35* For example, the following class prints out different representations of a36* stock's name depending on the flags and length constraints:37*38* <pre> {@code39* import java.nio.CharBuffer;40* import java.util.Formatter;41* import java.util.Formattable;42* import java.util.Locale;43* import static java.util.FormattableFlags.*;44*45* ...46*47* public class StockName implements Formattable {48* private String symbol, companyName, frenchCompanyName;49* public StockName(String symbol, String companyName,50* String frenchCompanyName) {51* ...52* }53*54* ...55*56* public void formatTo(Formatter fmt, int f, int width, int precision) {57* StringBuilder sb = new StringBuilder();58*59* // decide form of name60* String name = companyName;61* if (fmt.locale().equals(Locale.FRANCE))62* name = frenchCompanyName;63* boolean alternate = (f & ALTERNATE) == ALTERNATE;64* boolean usesymbol = alternate || (precision != -1 && precision < 10);65* String out = (usesymbol ? symbol : name);66*67* // apply precision68* if (precision == -1 || out.length() < precision) {69* // write it all70* sb.append(out);71* } else {72* sb.append(out.substring(0, precision - 1)).append('*');73* }74*75* // apply width and justification76* int len = sb.length();77* if (len < width)78* for (int i = 0; i < width - len; i++)79* if ((f & LEFT_JUSTIFY) == LEFT_JUSTIFY)80* sb.append(' ');81* else82* sb.insert(0, ' ');83*84* fmt.format(sb.toString());85* }86*87* public String toString() {88* return String.format("%s - %s", symbol, companyName);89* }90* }91* }</pre>92*93* <p> When used in conjunction with the {@link java.util.Formatter}, the above94* class produces the following output for various format strings.95*96* <pre> {@code97* Formatter fmt = new Formatter();98* StockName sn = new StockName("HUGE", "Huge Fruit, Inc.",99* "Fruit Titanesque, Inc.");100* fmt.format("%s", sn); // -> "Huge Fruit, Inc."101* fmt.format("%s", sn.toString()); // -> "HUGE - Huge Fruit, Inc."102* fmt.format("%#s", sn); // -> "HUGE"103* fmt.format("%-10.8s", sn); // -> "HUGE "104* fmt.format("%.12s", sn); // -> "Huge Fruit,*"105* fmt.format(Locale.FRANCE, "%25s", sn); // -> " Fruit Titanesque, Inc."106* }</pre>107*108* <p> Formattables are not necessarily safe for multithreaded access. Thread109* safety is optional and may be enforced by classes that extend and implement110* this interface.111*112* <p> Unless otherwise specified, passing a {@code null} argument to113* any method in this interface will cause a {@link114* NullPointerException} to be thrown.115*116* @since 1.5117*/118public interface Formattable {119120/**121* Formats the object using the provided {@link Formatter formatter}.122*123* @param formatter124* The {@link Formatter formatter}. Implementing classes may call125* {@link Formatter#out() formatter.out()} or {@link126* Formatter#locale() formatter.locale()} to obtain the {@link127* Appendable} or {@link Locale} used by this128* {@code formatter} respectively.129*130* @param flags131* The flags modify the output format. The value is interpreted as132* a bitmask. Any combination of the following flags may be set:133* {@link FormattableFlags#LEFT_JUSTIFY}, {@link134* FormattableFlags#UPPERCASE}, and {@link135* FormattableFlags#ALTERNATE}. If no flags are set, the default136* formatting of the implementing class will apply.137*138* @param width139* The minimum number of characters to be written to the output.140* If the length of the converted value is less than the141* {@code width} then the output will be padded by142* <code>' '</code> until the total number of characters143* equals width. The padding is at the beginning by default. If144* the {@link FormattableFlags#LEFT_JUSTIFY} flag is set then the145* padding will be at the end. If {@code width} is {@code -1}146* then there is no minimum.147*148* @param precision149* The maximum number of characters to be written to the output.150* The precision is applied before the width, thus the output will151* be truncated to {@code precision} characters even if the152* {@code width} is greater than the {@code precision}. If153* {@code precision} is {@code -1} then there is no explicit154* limit on the number of characters.155*156* @throws IllegalFormatException157* If any of the parameters are invalid. For specification of all158* possible formatting errors, see the <a159* href="../util/Formatter.html#detail">Details</a> section of the160* formatter class specification.161*/162void formatTo(Formatter formatter, int flags, int width, int precision);163}164165166