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(" ", " ");
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(" ");
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 += ": ";
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: + ": ";
271: value1 = "<td><font size =-1>" + value1;
272: label2 = "<th nowrap align=right valign=top>" + label2
273: + ": ";
274: value2 = "<td><font size =-1>" + value2;
275:
276: return "<tr>" + label1 + value1 + label2 + value2 + "</tr>";
277: }
278:
279: }
|