1 /*
2  * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package sun.tools.jconsole;
27 
28 import java.text.*;
29 import java.util.*;
30 
31 
32 class Formatter {
33     final static long SECOND = 1000;
34     final static long MINUTE = 60 * SECOND;
35     final static long HOUR   = 60 * MINUTE;
36     final static long DAY    = 24 * HOUR;
37 
38     final static String cr = System.getProperty("line.separator");
39 
40     final static DateFormat timeDF            = new SimpleDateFormat("HH:mm");
41     private final static DateFormat timeWithSecondsDF = new SimpleDateFormat("HH:mm:ss");
42     private final static DateFormat dateDF            = new SimpleDateFormat("yyyy-MM-dd");
43     private final static String decimalZero =
44                                 new DecimalFormatSymbols().getDecimalSeparator() + "0";
45 
formatTime(long t)46     static String formatTime(long t) {
47         String str;
48         if (t < 1 * MINUTE) {
49             String seconds = String.format("%.3f", t / (double)SECOND);
50             str = Resources.format(Messages.DURATION_SECONDS, seconds);
51         } else {
52             long remaining = t;
53             long days = remaining / DAY;
54             remaining %= 1 * DAY;
55             long hours = remaining / HOUR;
56             remaining %= 1 * HOUR;
57             long minutes = remaining / MINUTE;
58 
59             if (t >= 1 * DAY) {
60                 str = Resources.format(Messages.DURATION_DAYS_HOURS_MINUTES,
61                                        days, hours, minutes);
62             } else if (t >= 1 * HOUR) {
63                 str = Resources.format(Messages.DURATION_HOURS_MINUTES,
64                                        hours, minutes);
65             } else {
66                 str = Resources.format(Messages.DURATION_MINUTES, minutes);
67             }
68         }
69         return str;
70     }
71 
formatNanoTime(long t)72     static String formatNanoTime(long t) {
73         long ms = t / 1000000;
74         return formatTime(ms);
75     }
76 
77 
formatClockTime(long time)78     static String formatClockTime(long time) {
79         return timeDF.format(time);
80     }
81 
formatDate(long time)82     static String formatDate(long time) {
83         return dateDF.format(time);
84     }
85 
formatDateTime(long time)86     static String formatDateTime(long time) {
87         return dateDF.format(time) + " " + timeWithSecondsDF.format(time);
88     }
89 
getDateTimeFormat(String dtfStr)90     static DateFormat getDateTimeFormat(String dtfStr) {
91         int dateStyle = -1;
92         int timeStyle = -1;
93 
94         if (dtfStr.startsWith("SHORT")) {
95             dateStyle = DateFormat.SHORT;
96         } else if (dtfStr.startsWith("MEDIUM")) {
97             dateStyle = DateFormat.MEDIUM;
98         } else if (dtfStr.startsWith("LONG")) {
99             dateStyle = DateFormat.LONG;
100         } else if (dtfStr.startsWith("FULL")) {
101             dateStyle = DateFormat.FULL;
102         }
103 
104         if (dtfStr.endsWith("SHORT")) {
105             timeStyle = DateFormat.SHORT;
106         } else if (dtfStr.endsWith("MEDIUM")) {
107             timeStyle = DateFormat.MEDIUM;
108         } else if (dtfStr.endsWith("LONG")) {
109             timeStyle = DateFormat.LONG;
110         } else if (dtfStr.endsWith("FULL")) {
111             timeStyle = DateFormat.FULL;
112         }
113 
114         if (dateStyle != -1 && timeStyle != -1) {
115             return DateFormat.getDateTimeInstance(dateStyle, timeStyle);
116         } else if (dtfStr.length() > 0) {
117             return new SimpleDateFormat(dtfStr);
118         } else {
119             return DateFormat.getDateTimeInstance();
120         }
121     }
122 
toExcelTime(long time)123     static double toExcelTime(long time) {
124         // Excel is bug compatible with Lotus 1-2-3 and pretends
125         // that 1900 was a leap year, so count from 1899-12-30.
126         // Note that the month index is zero-based in Calendar.
127         Calendar cal = new GregorianCalendar(1899, 11, 30);
128 
129         // Adjust for the fact that now may be DST but then wasn't
130         Calendar tmpCal = new GregorianCalendar();
131         tmpCal.setTimeInMillis(time);
132         int dst = tmpCal.get(Calendar.DST_OFFSET);
133         if (dst > 0) {
134             cal.set(Calendar.DST_OFFSET, dst);
135         }
136 
137         long millisSince1900 = time - cal.getTimeInMillis();
138         double value = (double)millisSince1900 / (24 * 60 * 60 * 1000);
139 
140         return value;
141     }
142 
143 
144 
formatKByteStrings(long... bytes)145     static String[] formatKByteStrings(long... bytes) {
146         int n = bytes.length;
147         for (int i = 0; i < n; i++) {
148             if (bytes[i] > 0) {
149                 bytes[i] /= 1024;
150             }
151         }
152         String[] strings = formatLongs(bytes);
153         for (int i = 0; i < n; i++) {
154             strings[i] = Resources.format(Messages.KBYTES, strings[i]);
155         }
156         return strings;
157     }
158 
formatKBytes(long bytes)159     static String formatKBytes(long bytes) {
160         if (bytes == -1) {
161             return Resources.format(Messages.KBYTES, "-1");
162         }
163 
164         long kb = bytes / 1024;
165         return Resources.format(Messages.KBYTES, justify(kb, 10));
166     }
167 
168 
formatBytes(long v, boolean html)169     static String formatBytes(long v, boolean html) {
170         return formatBytes(v, v, html);
171     }
172 
formatBytes(long v, long vMax)173     static String formatBytes(long v, long vMax) {
174         return formatBytes(v, vMax, false);
175     }
176 
formatBytes(long v, long vMax, boolean html)177     static String formatBytes(long v, long vMax, boolean html) {
178         String s;
179 
180         int exp = (int)Math.log10((double)vMax);
181 
182         if (exp < 3) {
183             s = Resources.format(Messages.SIZE_BYTES, v);
184         } else if (exp < 6) {
185             s = Resources.format(Messages.SIZE_KB, trimDouble(v / Math.pow(10.0, 3)));
186         } else if (exp < 9) {
187             s = Resources.format(Messages.SIZE_MB, trimDouble(v / Math.pow(10.0, 6)));
188         } else {
189             s = Resources.format(Messages.SIZE_GB, trimDouble(v / Math.pow(10.0, 9)));
190         }
191         if (html) {
192             s = s.replace(" ", "&nbsp;");
193         }
194         return s;
195     }
196 
197     /*
198      * Return the input value rounded to one decimal place.  If after
199      * rounding the string ends in the (locale-specific) decimal point
200      * followed by a zero then trim that off as well.
201      */
trimDouble(double d)202     private static String trimDouble(double d) {
203         String s = String.format("%.1f", d);
204         if (s.length() > 3 && s.endsWith(decimalZero)) {
205             s = s.substring(0, s.length()-2);
206         }
207         return s;
208     }
209 
formatLong(long value)210     static String formatLong(long value) {
211         return String.format("%,d", value);
212     }
213 
formatLongs(long... longs)214     static String[] formatLongs(long... longs) {
215         int n = longs.length;
216         int size = 0;
217         String[] strings = new String[n];
218         for (int i = 0; i < n; i++) {
219             strings[i] = formatLong(longs[i]);
220             size = Math.max(size, strings[i].length());
221         }
222         for (int i = 0; i < n; i++) {
223             strings[i] = justify(strings[i], size);
224         }
225         return strings;
226     }
227 
228 
229     // A poor attempt at right-justifying for numerical data
justify(long value, int size)230     static String justify(long value, int size) {
231         return justify(formatLong(value), size);
232     }
233 
justify(String str, int size)234     static String justify(String str, int size) {
235         StringBuilder sb = new StringBuilder();
236         sb.append("<TT>");
237         int n = size - str.length();
238         for (int i = 0; i < n; i++) {
239             sb.append("&nbsp;");
240         }
241         sb.append(str);
242         sb.append("</TT>");
243         return sb.toString();
244     }
245 
newRow(String label, String value)246     static String newRow(String label, String value) {
247         return newRow(label, value, 2);
248     }
249 
newRow(String label, String value, int columnPerRow)250     static String newRow(String label, String value, int columnPerRow) {
251         if (label == null) {
252             label = "";
253         } else {
254             label += ":&nbsp;";
255         }
256         label = "<th nowrap align=right valign=top>" + label;
257         value = "<td colspan=" + (columnPerRow-1) + "> <font size =-1>" + value;
258 
259         return "<tr>" + label + value + "</tr>";
260     }
261 
newRow(String label1, String value1, String label2, String value2)262     static String newRow(String label1, String value1,
263                          String label2, String value2) {
264         label1 = "<th nowrap align=right valign=top>" + label1 + ":&nbsp;";
265         value1 = "<td><font size =-1>" + value1;
266         label2 = "<th nowrap align=right valign=top>" + label2 + ":&nbsp;";
267         value2 = "<td><font size =-1>" + value2;
268 
269         return "<tr>" + label1 + value1 + label2 + value2 + "</tr>";
270     }
271 
272 }
273