Source Code Cross Referenced for Formatter.java in  » 6.0-JDK-Modules-sun » tools » sun » tools » jconsole » 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 » 6.0 JDK Modules sun » tools » sun.tools.jconsole 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004-2006 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 sun.tools.jconsole;
027:
028:        import java.text.*;
029:        import java.util.*;
030:
031:        import static sun.tools.jconsole.Resources.*;
032:
033:        class Formatter {
034:            final static long SECOND = 1000;
035:            final static long MINUTE = 60 * SECOND;
036:            final static long HOUR = 60 * MINUTE;
037:            final static long DAY = 24 * HOUR;
038:
039:            final static String cr = System.getProperty("line.separator");
040:
041:            final static DateFormat timeDF = new SimpleDateFormat("HH:mm");
042:            private final static DateFormat timeWithSecondsDF = new SimpleDateFormat(
043:                    "HH:mm:ss");
044:            private final static DateFormat dateDF = new SimpleDateFormat(
045:                    "yyyy-MM-dd");
046:            private final static String decimalZero = new DecimalFormatSymbols()
047:                    .getDecimalSeparator()
048:                    + "0";
049:
050:            static String formatTime(long t) {
051:                String str;
052:                if (t < 1 * MINUTE) {
053:                    String seconds = String.format("%.3f", t / (double) SECOND);
054:                    str = Resources.getText("DurationSeconds", seconds);
055:                } else {
056:                    long remaining = t;
057:                    long days = remaining / DAY;
058:                    remaining %= 1 * DAY;
059:                    long hours = remaining / HOUR;
060:                    remaining %= 1 * HOUR;
061:                    long minutes = remaining / MINUTE;
062:
063:                    if (t >= 1 * DAY) {
064:                        str = Resources.getText("DurationDaysHoursMinutes",
065:                                days, hours, minutes);
066:                    } else if (t >= 1 * HOUR) {
067:                        str = Resources.getText("DurationHoursMinutes", hours,
068:                                minutes);
069:                    } else {
070:                        str = Resources.getText("DurationMinutes", minutes);
071:                    }
072:                }
073:                return str;
074:            }
075:
076:            static String formatNanoTime(long t) {
077:                long ms = t / 1000000;
078:                return formatTime(ms);
079:            }
080:
081:            static String formatClockTime(long time) {
082:                return timeDF.format(time);
083:            }
084:
085:            static String formatDate(long time) {
086:                return dateDF.format(time);
087:            }
088:
089:            static String formatDateTime(long time) {
090:                return dateDF.format(time) + " "
091:                        + timeWithSecondsDF.format(time);
092:            }
093:
094:            static DateFormat getDateTimeFormat(String key) {
095:                String dtfStr = getText(key);
096:                int dateStyle = -1;
097:                int timeStyle = -1;
098:
099:                if (dtfStr.startsWith("SHORT")) {
100:                    dateStyle = DateFormat.SHORT;
101:                } else if (dtfStr.startsWith("MEDIUM")) {
102:                    dateStyle = DateFormat.MEDIUM;
103:                } else if (dtfStr.startsWith("LONG")) {
104:                    dateStyle = DateFormat.LONG;
105:                } else if (dtfStr.startsWith("FULL")) {
106:                    dateStyle = DateFormat.FULL;
107:                }
108:
109:                if (dtfStr.endsWith("SHORT")) {
110:                    timeStyle = DateFormat.SHORT;
111:                } else if (dtfStr.endsWith("MEDIUM")) {
112:                    timeStyle = DateFormat.MEDIUM;
113:                } else if (dtfStr.endsWith("LONG")) {
114:                    timeStyle = DateFormat.LONG;
115:                } else if (dtfStr.endsWith("FULL")) {
116:                    timeStyle = DateFormat.FULL;
117:                }
118:
119:                if (dateStyle != -1 && timeStyle != -1) {
120:                    return DateFormat.getDateTimeInstance(dateStyle, timeStyle);
121:                } else if (dtfStr.length() > 0) {
122:                    return new SimpleDateFormat(dtfStr);
123:                } else {
124:                    return DateFormat.getDateTimeInstance();
125:                }
126:            }
127:
128:            static double toExcelTime(long time) {
129:                // Excel is bug compatible with Lotus 1-2-3 and pretends
130:                // that 1900 was a leap year, so count from 1899-12-30.
131:                // Note that the month index is zero-based in Calendar.
132:                Calendar cal = new GregorianCalendar(1899, 11, 30);
133:
134:                // Adjust for the fact that now may be DST but then wasn't
135:                Calendar tmpCal = new GregorianCalendar();
136:                tmpCal.setTimeInMillis(time);
137:                int dst = tmpCal.get(Calendar.DST_OFFSET);
138:                if (dst > 0) {
139:                    cal.set(Calendar.DST_OFFSET, dst);
140:                }
141:
142:                long millisSince1900 = time - cal.getTimeInMillis();
143:                double value = (double) millisSince1900 / (24 * 60 * 60 * 1000);
144:
145:                return value;
146:            }
147:
148:            static String[] formatKByteStrings(long... bytes) {
149:                int n = bytes.length;
150:                for (int i = 0; i < n; i++) {
151:                    if (bytes[i] > 0) {
152:                        bytes[i] /= 1024;
153:                    }
154:                }
155:                String[] strings = formatLongs(bytes);
156:                for (int i = 0; i < n; i++) {
157:                    strings[i] = getText("kbytes", strings[i]);
158:                }
159:                return strings;
160:            }
161:
162:            static String formatKBytes(long bytes) {
163:                if (bytes == -1) {
164:                    return getText("kbytes", "-1");
165:                }
166:
167:                long kb = bytes / 1024;
168:                return getText("kbytes", justify(kb, 10));
169:            }
170:
171:            static String formatBytes(long v, boolean html) {
172:                return formatBytes(v, v, html);
173:            }
174:
175:            static String formatBytes(long v, long vMax) {
176:                return formatBytes(v, vMax, false);
177:            }
178:
179:            static String formatBytes(long v, long vMax, boolean html) {
180:                String s;
181:
182:                int exp = (int) Math.log10((double) vMax);
183:
184:                if (exp < 3) {
185:                    s = Resources.getText("Size Bytes", v);
186:                } else if (exp < 6) {
187:                    s = Resources.getText("Size Kb", trimDouble(v
188:                            / Math.pow(10.0, 3)));
189:                } else if (exp < 9) {
190:                    s = Resources.getText("Size Mb", trimDouble(v
191:                            / Math.pow(10.0, 6)));
192:                } else {
193:                    s = Resources.getText("Size Gb", trimDouble(v
194:                            / Math.pow(10.0, 9)));
195:                }
196:                if (html) {
197:                    s = s.replace(" ", "&nbsp;");
198:                }
199:                return s;
200:            }
201:
202:            /*
203:             * Return the input value rounded to one decimal place.  If after
204:             * rounding the string ends in the (locale-specific) decimal point
205:             * followed by a zero then trim that off as well.
206:             */
207:            private static String trimDouble(double d) {
208:                String s = String.format("%.1f", d);
209:                if (s.length() > 3 && s.endsWith(decimalZero)) {
210:                    s = s.substring(0, s.length() - 2);
211:                }
212:                return s;
213:            }
214:
215:            static String formatLong(long value) {
216:                return String.format("%,d", value);
217:            }
218:
219:            static String[] formatLongs(long... longs) {
220:                int n = longs.length;
221:                int size = 0;
222:                String[] strings = new String[n];
223:                for (int i = 0; i < n; i++) {
224:                    strings[i] = formatLong(longs[i]);
225:                    size = Math.max(size, strings[i].length());
226:                }
227:                for (int i = 0; i < n; i++) {
228:                    strings[i] = justify(strings[i], size);
229:                }
230:                return strings;
231:            }
232:
233:            // A poor attempt at right-justifying for numerical data
234:            static String justify(long value, int size) {
235:                return justify(formatLong(value), size);
236:            }
237:
238:            static String justify(String str, int size) {
239:                StringBuffer buf = new StringBuffer();
240:                buf.append("<TT>");
241:                int n = size - str.length();
242:                for (int i = 0; i < n; i++) {
243:                    buf.append("&nbsp;");
244:                }
245:                buf.append(str);
246:                buf.append("</TT>");
247:                return buf.toString();
248:            }
249:
250:            static String newRow(String label, String value) {
251:                return newRow(label, value, 2);
252:            }
253:
254:            static String newRow(String label, String value, int columnPerRow) {
255:                if (label == null) {
256:                    label = "";
257:                } else {
258:                    label += ":&nbsp;";
259:                }
260:                label = "<th nowrap align=right valign=top>" + label;
261:                value = "<td colspan=" + (columnPerRow - 1)
262:                        + "> <font size =-1>" + value;
263:
264:                return "<tr>" + label + value + "</tr>";
265:            }
266:
267:            static String newRow(String label1, String value1, String label2,
268:                    String value2) {
269:                label1 = "<th nowrap align=right valign=top>" + label1
270:                        + ":&nbsp;";
271:                value1 = "<td><font size =-1>" + value1;
272:                label2 = "<th nowrap align=right valign=top>" + label2
273:                        + ":&nbsp;";
274:                value2 = "<td><font size =-1>" + value2;
275:
276:                return "<tr>" + label1 + value1 + label2 + value2 + "</tr>";
277:            }
278:
279:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.