1 /* 2 * Copyright (c) 2004, 2006, 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 import static sun.tools.jconsole.Resources.*; 32 33 class Formatter { 34 final static long SECOND = 1000; 35 final static long MINUTE = 60 * SECOND; 36 final static long HOUR = 60 * MINUTE; 37 final static long DAY = 24 * HOUR; 38 39 final static String cr = System.getProperty("line.separator"); 40 41 final static DateFormat timeDF = new SimpleDateFormat("HH:mm"); 42 private final static DateFormat timeWithSecondsDF = new SimpleDateFormat("HH:mm:ss"); 43 private final static DateFormat dateDF = new SimpleDateFormat("yyyy-MM-dd"); 44 private final static String decimalZero = 45 new DecimalFormatSymbols().getDecimalSeparator() + "0"; 46 47 static String formatTime(long t) { 48 String str; 49 if (t < 1 * MINUTE) { 50 String seconds = String.format("%.3f", t / (double)SECOND); 51 str = Resources.getText("DurationSeconds", seconds); 52 } else { 53 long remaining = t; 54 long days = remaining / DAY; 55 remaining %= 1 * DAY; 56 long hours = remaining / HOUR; 57 remaining %= 1 * HOUR; 58 long minutes = remaining / MINUTE; 59 60 if (t >= 1 * DAY) { 61 str = Resources.getText("DurationDaysHoursMinutes", 62 days, hours, minutes); 63 } else if (t >= 1 * HOUR) { 64 str = Resources.getText("DurationHoursMinutes", 65 hours, minutes); 66 } else { 67 str = Resources.getText("DurationMinutes", minutes); 68 } 69 } 70 return str; 71 } 72 73 static String formatNanoTime(long t) { 74 long ms = t / 1000000; 75 return formatTime(ms); 76 } 77 78 79 static String formatClockTime(long time) { 80 return timeDF.format(time); 81 } 82 83 static String formatDate(long time) { 84 return dateDF.format(time); 85 } 86 87 static String formatDateTime(long time) { 88 return dateDF.format(time) + " " + timeWithSecondsDF.format(time); 89 } 90 91 static DateFormat getDateTimeFormat(String key) { 92 String dtfStr = getText(key); 93 int dateStyle = -1; 94 int timeStyle = -1; 95 96 if (dtfStr.startsWith("SHORT")) { 97 dateStyle = DateFormat.SHORT; 98 } else if (dtfStr.startsWith("MEDIUM")) { 99 dateStyle = DateFormat.MEDIUM; 100 } else if (dtfStr.startsWith("LONG")) { 101 dateStyle = DateFormat.LONG; 102 } else if (dtfStr.startsWith("FULL")) { 103 dateStyle = DateFormat.FULL; 104 } 105 106 if (dtfStr.endsWith("SHORT")) { 107 timeStyle = DateFormat.SHORT; 108 } else if (dtfStr.endsWith("MEDIUM")) { 109 timeStyle = DateFormat.MEDIUM; 110 } else if (dtfStr.endsWith("LONG")) { 111 timeStyle = DateFormat.LONG; 112 } else if (dtfStr.endsWith("FULL")) { 113 timeStyle = DateFormat.FULL; 114 } 115 116 if (dateStyle != -1 && timeStyle != -1) { 117 return DateFormat.getDateTimeInstance(dateStyle, timeStyle); 118 } else if (dtfStr.length() > 0) { 119 return new SimpleDateFormat(dtfStr); 120 } else { 121 return DateFormat.getDateTimeInstance(); 122 } 123 } 124 125 static double toExcelTime(long time) { 126 // Excel is bug compatible with Lotus 1-2-3 and pretends 127 // that 1900 was a leap year, so count from 1899-12-30. 128 // Note that the month index is zero-based in Calendar. 129 Calendar cal = new GregorianCalendar(1899, 11, 30); 130 131 // Adjust for the fact that now may be DST but then wasn't 132 Calendar tmpCal = new GregorianCalendar(); 133 tmpCal.setTimeInMillis(time); 134 int dst = tmpCal.get(Calendar.DST_OFFSET); 135 if (dst > 0) { 136 cal.set(Calendar.DST_OFFSET, dst); 137 } 138 139 long millisSince1900 = time - cal.getTimeInMillis(); 140 double value = (double)millisSince1900 / (24 * 60 * 60 * 1000); 141 142 return value; 143 } 144 145 146 147 static String[] formatKByteStrings(long... bytes) { 148 int n = bytes.length; 149 for (int i = 0; i < n; i++) { 150 if (bytes[i] > 0) { 151 bytes[i] /= 1024; 152 } 153 } 154 String[] strings = formatLongs(bytes); 155 for (int i = 0; i < n; i++) { 156 strings[i] = getText("kbytes", strings[i]); 157 } 158 return strings; 159 } 160 161 static String formatKBytes(long bytes) { 162 if (bytes == -1) { 163 return getText("kbytes", "-1"); 164 } 165 166 long kb = bytes / 1024; 167 return getText("kbytes", justify(kb, 10)); 168 } 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 / Math.pow(10.0, 3))); 188 } else if (exp < 9) { 189 s = Resources.getText("Size Mb", trimDouble(v / Math.pow(10.0, 6))); 190 } else { 191 s = Resources.getText("Size Gb", trimDouble(v / Math.pow(10.0, 9))); 192 } 193 if (html) { 194 s = s.replace(" ", " "); 195 } 196 return s; 197 } 198 199 /* 200 * Return the input value rounded to one decimal place. If after 201 * rounding the string ends in the (locale-specific) decimal point 202 * followed by a zero then trim that off as well. 203 */ 204 private static String trimDouble(double d) { 205 String s = String.format("%.1f", d); 206 if (s.length() > 3 && s.endsWith(decimalZero)) { 207 s = s.substring(0, s.length()-2); 208 } 209 return s; 210 } 211 212 static String formatLong(long value) { 213 return String.format("%,d", value); 214 } 215 216 static String[] formatLongs(long... longs) { 217 int n = longs.length; 218 int size = 0; 219 String[] strings = new String[n]; 220 for (int i = 0; i < n; i++) { 221 strings[i] = formatLong(longs[i]); 222 size = Math.max(size, strings[i].length()); 223 } 224 for (int i = 0; i < n; i++) { 225 strings[i] = justify(strings[i], size); 226 } 227 return strings; 228 } 229 230 231 // A poor attempt at right-justifying for numerical data 232 static String justify(long value, int size) { 233 return justify(formatLong(value), size); 234 } 235 236 static String justify(String str, int size) { 237 StringBuffer buf = new StringBuffer(); 238 buf.append("<TT>"); 239 int n = size - str.length(); 240 for (int i = 0; i < n; i++) { 241 buf.append(" "); 242 } 243 buf.append(str); 244 buf.append("</TT>"); 245 return buf.toString(); 246 } 247 248 static String newRow(String label, String value) { 249 return newRow(label, value, 2); 250 } 251 252 static String newRow(String label, String value, int columnPerRow) { 253 if (label == null) { 254 label = ""; 255 } else { 256 label += ": "; 257 } 258 label = "<th nowrap align=right valign=top>" + label; 259 value = "<td colspan=" + (columnPerRow-1) + "> <font size =-1>" + value; 260 261 return "<tr>" + label + value + "</tr>"; 262 } 263 264 static String newRow(String label1, String value1, 265 String label2, String value2) { 266 label1 = "<th nowrap align=right valign=top>" + label1 + ": "; 267 value1 = "<td><font size =-1>" + value1; 268 label2 = "<th nowrap align=right valign=top>" + label2 + ": "; 269 value2 = "<td><font size =-1>" + value2; 270 271 return "<tr>" + label1 + value1 + label2 + value2 + "</tr>"; 272 } 273 274 }