Source Code Cross Referenced for Formattable.java in  » 6.0-JDK-Core » Collections-Jar-Zip-Logging-regex » java » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » Collections Jar Zip Logging regex » java.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 2003-2004 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025
026        package java.util;
027
028        import java.io.IOException;
029
030        /**
031         * The <tt>Formattable</tt> interface must be implemented by any class that
032         * needs to perform custom formatting using the <tt>'s'</tt> conversion
033         * specifier of {@link java.util.Formatter}.  This interface allows basic
034         * control for formatting arbitrary objects.
035         *
036         * For example, the following class prints out different representations of a
037         * stock's name depending on the flags and length constraints:
038         *
039         * <blockquote><pre>
040         *   import java.nio.CharBuffer;
041         *   import java.util.Formatter;
042         *   import java.util.Formattable;
043         *   import java.util.Locale;
044         *   import static java.util.FormattableFlags.*;
045         *
046         *  ...
047         * 
048         *   public class StockName implements Formattable {
049         *       private String symbol, companyName, frenchCompanyName;
050         *       public StockName(String symbol, String companyName,
051         *                        String frenchCompanyName) {
052         *           ...
053         *       }
054         *
055         *       ...
056         *
057         *       public void formatTo(Formatter fmt, int f, int width, int precision) {
058         *           StringBuilder sb = new StringBuilder();
059         *
060         *           // decide form of name 
061         *           String name = companyName;
062         *           if (fmt.locale().equals(Locale.FRANCE))
063         *               name = frenchCompanyName;
064         *           boolean alternate = (f & ALTERNATE) == ALTERNATE;
065         *           boolean usesymbol = alternate || (precision != -1 && precision < 10);
066         *           String out = (usesymbol ? symbol : name);
067         *
068         *           // apply precision
069         *           if (precision == -1 || out.length() < precision) {
070         *               // write it all
071         *               sb.append(out);
072         *           } else {
073         *               sb.append(out.substring(0, precision - 1)).append('*');
074         *           }
075         *
076         *           // apply width and justification
077         *           int len = sb.length(); 
078         *           if (len < width)
079         *               for (int i = 0; i < width - len; i++)
080         *                   if ((f & LEFT_JUSTIFY) == LEFT_JUSTIFY)
081         *                       sb.append(' ');
082         *                   else
083         *                       sb.insert(0, ' ');
084         *
085         *           fmt.format(sb.toString());
086         *       }
087         *
088         *       public String toString() {
089         *           return String.format("%s - %s", symbol, companyName);
090         *       }
091         *   }
092         * </pre></blockquote>
093         *
094         * <p> When used in conjunction with the {@link java.util.Formatter}, the above
095         * class produces the following output for various format strings.
096         *
097         * <blockquote><pre>
098         *   Formatter fmt = new Formatter();
099         *   StockName sn = new StockName("HUGE", "Huge Fruit, Inc.",
100         *                                "Fruit Titanesque, Inc.");
101         *   fmt.format("%s", sn);                   //   -> "Huge Fruit, Inc."
102         *   fmt.format("%s", sn.toString());        //   -> "HUGE - Huge Fruit, Inc."
103         *   fmt.format("%#s", sn);                  //   -> "HUGE"
104         *   fmt.format("%-10.8s", sn);              //   -> "HUGE      "
105         *   fmt.format("%.12s", sn);                //   -> "Huge Fruit,*"
106         *   fmt.format(Locale.FRANCE, "%25s", sn);  //   -> "   Fruit Titanesque, Inc." 
107         * </pre></blockquote>
108         *
109         * <p> Formattables are not necessarily safe for multithreaded access.  Thread
110         * safety is optional and may be enforced by classes that extend and implement
111         * this interface. 
112         *
113         * <p> Unless otherwise specified, passing a <tt>null</tt> argument to
114         * any method in this interface will cause a {@link
115         * NullPointerException} to be thrown.
116         *
117         * @version 	1.10, 05/05/07
118         * @since  1.5
119         */
120        public interface Formattable {
121
122            /**
123             * Formats the object using the provided {@link Formatter formatter}.
124             *
125             * @param  formatter
126             *         The {@link Formatter formatter}.  Implementing classes may call
127             *         {@link Formatter#out() formatter.out()} or {@link
128             *         Formatter#locale() formatter.locale()} to obtain the {@link
129             *         Appendable} or {@link Locale} used by this
130             *         <tt>formatter</tt> respectively. 
131             *
132             * @param  flags
133             *         The flags modify the output format.  The value is interpreted as
134             *         a bitmask.  Any combination of the following flags may be set:
135             *         {@link FormattableFlags#LEFT_JUSTIFY}, {@link
136             *         FormattableFlags#UPPERCASE}, and {@link
137             *         FormattableFlags#ALTERNATE}.  If no flags are set, the default
138             *         formatting of the implementing class will apply.
139             *
140             * @param  width
141             *         The minimum number of characters to be written to the output.
142             *         If the length of the converted value is less than the
143             *         <tt>width</tt> then the output will be padded by
144             *         <tt>'&nbsp;&nbsp;'</tt> until the total number of characters
145             *         equals width.  The padding is at the beginning by default.  If
146             *         the {@link FormattableFlags#LEFT_JUSTIFY} flag is set then the
147             *         padding will be at the end.  If <tt>width</tt> is <tt>-1</tt>
148             *         then there is no minimum.
149             *
150             * @param  precision
151             *         The maximum number of characters to be written to the output.
152             *         The precision is applied before the width, thus the output will
153             *         be truncated to <tt>precision</tt> characters even if the
154             *         <tt>width</tt> is greater than the <tt>precision</tt>.  If
155             *         <tt>precision</tt> is <tt>-1</tt> then there is no explicit
156             *         limit on the number of characters.
157             *
158             * @throws  IllegalFormatException
159             *          If any of the parameters are invalid.  For specification of all
160             *          possible formatting errors, see the <a
161             *          href="../util/Formatter.html#detail">Details</a> section of the
162             *          formatter class specification.
163             */
164            void formatTo(Formatter formatter, int flags, int width,
165                    int precision);
166        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.