Source Code Cross Referenced for IntegerFormat.java in  » Scripting » Kawa » gnu » text » Java Source Code / Java DocumentationJava Source Code and Java Documentation

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 geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Scripting » Kawa » gnu.text 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Copyright (c) 2001, 2003  Per M.A. Bothner.
002:        // This is free software;  for terms and warranty disclaimer see ./COPYING.
003:
004:        package gnu.text;
005:
006:        import java.text.FieldPosition;
007:        import java.io.Writer;
008:
009:        /** Handle formatting of integers.
010:         * Used to implement the Common Lisp ~D (Decimal), ~X (Hexadecimal),
011:         * ~O (Octal), ~B (Binary), and ~R (Radix) Common Lisp formats operators. */
012:
013:        public class IntegerFormat extends ReportFormat {
014:            public int base;
015:
016:            /** Minimal width of the result, includiing sign, commas, etc.
017:             * However, if the MIN_DIGITS flag is given, it's the minimum number
018:             * of digits instead.  This is used for printf-style "precision".  */
019:            public int minWidth;
020:
021:            /** The padding characters, by default ' '. */
022:            public int padChar;
023:
024:            public int commaChar;
025:            public int commaInterval;
026:
027:            public int flags;
028:            /** Do groups (for example thousands, using commas). */
029:            public static final int SHOW_GROUPS = 1;
030:
031:            /** If value is non-negative, emit a '+'. */
032:            public static final int SHOW_PLUS = 2;
033:
034:            /** If value is non-negative, emit an initial ' '. */
035:            public static final int SHOW_SPACE = 4;
036:
037:            /** Add "0x" (hex) or "0" (octal) prefix. */
038:            public static final int SHOW_BASE = 8;
039:
040:            public static final int PAD_RIGHT = 16;
041:
042:            public static final int UPPERCASE = 32;
043:
044:            /** The minWidth is minimum number of digits, not minimum total width. */
045:            public static final int MIN_DIGITS = 64;
046:
047:            public IntegerFormat() {
048:                base = 10;
049:                minWidth = 1;
050:                padChar = (int) ' ';
051:                commaChar = (int) ',';
052:                commaInterval = 3;
053:                flags = 0;
054:            }
055:
056:            public int format(Object[] args, int start, Writer dst,
057:                    FieldPosition fpos) throws java.io.IOException {
058:                return format((Object) args, start, dst, fpos);
059:            }
060:
061:            public int format(Object arg, int start, Writer dst,
062:                    FieldPosition fpos) throws java.io.IOException {
063:                Object[] args = arg instanceof  Object[] ? (Object[]) arg : null;
064:                int minWidth = getParam(this .minWidth, 1, args, start);
065:                if (this .minWidth == PARAM_FROM_LIST)
066:                    start++;
067:                char padChar = getParam(this .padChar, ' ', args, start);
068:                if (this .padChar == PARAM_FROM_LIST)
069:                    start++;
070:                char commaChar = getParam(this .commaChar, ',', args, start);
071:                if (this .commaChar == PARAM_FROM_LIST)
072:                    start++;
073:                int commaInterval = getParam(this .commaInterval, 3, args, start);
074:                if (this .commaInterval == PARAM_FROM_LIST)
075:                    start++;
076:                boolean printCommas = (flags & SHOW_GROUPS) != 0;
077:                boolean padRight = (flags & PAD_RIGHT) != 0;
078:                boolean padInternal = padChar == '0';
079:                if (args != null) {
080:                    if (start >= args.length) {
081:                        dst.write("#<missing format argument>");
082:                        return start;
083:                    }
084:                    arg = args[start];
085:                }
086:                String sarg = convertToIntegerString(arg, base);
087:                if (sarg != null) {
088:                    char sarg0 = sarg.charAt(0);
089:                    boolean neg = sarg0 == '-';
090:                    int slen = sarg.length();
091:                    int ndigits = neg ? slen - 1 : slen;
092:                    int numCommas = printCommas ? (ndigits - 1) / commaInterval
093:                            : 0;
094:                    int unpadded_len = ndigits + numCommas;
095:                    if (neg || (flags & (SHOW_PLUS | SHOW_SPACE)) != 0)
096:                        unpadded_len++;
097:
098:                    if ((flags & SHOW_BASE) != 0) {
099:                        if (base == 16)
100:                            unpadded_len += 2;
101:                        else if (base == 8 && sarg0 != '0')
102:                            unpadded_len += 1;
103:                    }
104:                    if ((flags & MIN_DIGITS) != 0) {
105:                        unpadded_len = ndigits;
106:                        if (slen == 1 && sarg0 == '0' && minWidth == 0)
107:                            slen = 0;
108:                    }
109:                    if (!padRight && !padInternal)
110:                        for (; minWidth > unpadded_len; --minWidth)
111:                            dst.write(padChar);
112:                    int i = 0;
113:                    if (neg) {
114:                        dst.write('-');
115:                        i++;
116:                        slen--;
117:                    } else if ((flags & SHOW_PLUS) != 0)
118:                        dst.write('+');
119:                    else if ((flags & SHOW_SPACE) != 0)
120:                        dst.write(' ');
121:                    boolean uppercase = base > 10 && (flags & UPPERCASE) != 0;
122:                    if ((flags & SHOW_BASE) != 0) {
123:                        if (base == 16) {
124:                            dst.write('0');
125:                            dst.write(uppercase ? 'X' : 'x');
126:                        } else if (base == 8 && sarg0 != '0')
127:                            dst.write('0');
128:                    }
129:                    if (padInternal)
130:                        for (; minWidth > unpadded_len; --minWidth)
131:                            dst.write(padChar);
132:                    for (;;) {
133:                        if (slen == 0)
134:                            break;
135:                        char ch = sarg.charAt(i++);
136:                        if (uppercase)
137:                            ch = Character.toUpperCase(ch);
138:                        dst.write(ch);
139:                        --slen;
140:                        if (printCommas && slen > 0
141:                                && (slen % commaInterval) == 0)
142:                            dst.write(commaChar);
143:                    }
144:                    if (padRight)
145:                        for (; minWidth > unpadded_len; --minWidth)
146:                            dst.write(padChar);
147:                } else
148:                    print(dst, arg.toString());
149:                return start + 1;
150:            }
151:
152:            public String convertToIntegerString(Object x, int radix) {
153:                if (!(x instanceof  Number))
154:                    return null;
155:                else if (x instanceof  java.math.BigInteger)
156:                    return ((java.math.BigInteger) x).toString(radix);
157:                else
158:                    return Long.toString(((Number) x).longValue(), radix);
159:            }
160:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.