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 sun.tools.jconsole.resources.Messages; 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.format(Messages.DURATION_SECONDS, 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.format(Messages.DURATION_DAYS_HOURS_MINUTES, 62 days, hours, minutes); 63 } else if (t >= 1 * HOUR) { 64 str = Resources.format(Messages.DURATION_HOURS_MINUTES, 65 hours, minutes); 66 } else { 67 str = Resources.format(Messages.DURATION_MINUTES, 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 dtfStr) { 92 int dateStyle = -1; 93 int timeStyle = -1; 94 95 if (dtfStr.startsWith("SHORT")) { 96 dateStyle = DateFormat.SHORT; 97 } else if (dtfStr.startsWith("MEDIUM")) { 98 dateStyle = DateFormat.MEDIUM; 99 } else if (dtfStr.startsWith("LONG")) { 100 dateStyle = DateFormat.LONG; 101 } else if (dtfStr.startsWith("FULL")) { 102 dateStyle = DateFormat.FULL; 103 } 104 105 if (dtfStr.endsWith("SHORT")) { 106 timeStyle = DateFormat.SHORT; 107 } else if (dtfStr.endsWith("MEDIUM")) { 108 timeStyle = DateFormat.MEDIUM; 109 } else if (dtfStr.endsWith("LONG")) { 110 timeStyle = DateFormat.LONG; 111 } else if (dtfStr.endsWith("FULL")) { 112 timeStyle = DateFormat.FULL; 113 } 114 115 if (dateStyle != -1 && timeStyle != -1) { 116 return DateFormat.getDateTimeInstance(dateStyle, timeStyle); 117 } else if (dtfStr.length() > 0) { 118 return new SimpleDateFormat(dtfStr); 119 } else { 120 return DateFormat.getDateTimeInstance(); 121 } 122 } 123 124 static double toExcelTime(long time) { 125 // Excel is bug compatible with Lotus 1-2-3 and pretends 126 // that 1900 was a leap year, so count from 1899-12-30. 127 // Note that the month index is zero-based in Calendar. 128 Calendar cal = new GregorianCalendar(1899, 11, 30); 129 130 // Adjust for the fact that now may be DST but then wasn't 131 Calendar tmpCal = new GregorianCalendar(); 132 tmpCal.setTimeInMillis(time); 133 int dst = tmpCal.get(Calendar.DST_OFFSET); 134 if (dst > 0) { 135 cal.set(Calendar.DST_OFFSET, dst); 136 } 137 138 long millisSince1900 = time - cal.getTimeInMillis(); 139 double value = (double)millisSince1900 / (24 * 60 * 60 * 1000); 140 141 return value; 142 } 143 144 145 146 static String[] formatKByteStrings(long... bytes) { 147 int n = bytes.length; 148 for (int i = 0; i < n; i++) { 149 if (bytes[i] > 0) { 150 bytes[i] /= 1024; 151 } 152 } 153 String[] strings = formatLongs(bytes); 154 for (int i = 0; i < n; i++) { 155 strings[i] = Resources.format(Messages.KBYTES, strings[i]); 156 } 157 return strings; 158 } 159 160 static String formatKBytes(long bytes) { 161 if (bytes == -1) { 162 return Resources.format(Messages.KBYTES, "-1"); 163 } 164 165 long kb = bytes / 1024; 166 return Resources.format(Messages.KBYTES, justify(kb, 10)); 167 } 168 169 170 static String formatBytes(long v, boolean html) { 171 return formatBytes(v, v, html); 172 } 173 174 static String formatBytes(long v, long vMax) { 175 return formatBytes(v, vMax, false); 176 } 177 178 static String formatBytes(long v, long vMax, boolean html) { 179 String s; 180 181 int exp = (int)Math.log10((double)vMax); 182 183 if (exp < 3) { 184 s = Resources.format(Messages.SIZE_BYTES, "Size Bytes", v); 185 } else if (exp < 6) { 186 s = Resources.format(Messages.SIZE_KB, trimDouble(v / Math.pow(10.0, 3))); 187 } else if (exp < 9) { 188 s = Resources.format(Messages.SIZE_MB, trimDouble(v / Math.pow(10.0, 6))); 189 } else { 190 s = Resources.format(Messages.SIZE_GB, trimDouble(v / Math.pow(10.0, 9))); 191 } 192 if (html) { 193 s = s.replace(" ", " "); 194 } 195 return s; 196 } 197 198 /* 199 * Return the input value rounded to one decimal place. If after 200 * rounding the string ends in the (locale-specific) decimal point 201 * followed by a zero then trim that off as well. 202 */ 203 private static String trimDouble(double d) { 204 String s = String.format("%.1f", d); 205 if (s.length() > 3 && s.endsWith(decimalZero)) { 206 s = s.substring(0, s.length()-2); 207 } 208 return s; 209 } 210 211 static String formatLong(long value) { 212 return String.format("%,d", value); 213 } 214 215 static String[] formatLongs(long... longs) { 216 int n = longs.length; 217 int size = 0; 218 String[] strings = new String[n]; 219 for (int i = 0; i < n; i++) { 220 strings[i] = formatLong(longs[i]); 221 size = Math.max(size, strings[i].length()); 222 } 223 for (int i = 0; i < n; i++) { 224 strings[i] = justify(strings[i], size); 225 } 226 return strings; 227 } 228 229 230 // A poor attempt at right-justifying for numerical data 231 static String justify(long value, int size) { 232 return justify(formatLong(value), size); 233 } 234 235 static String justify(String str, int size) { 236 StringBuffer buf = new StringBuffer(); 237 buf.append("<TT>"); 238 int n = size - str.length(); 239 for (int i = 0; i < n; i++) { 240 buf.append(" "); 241 } 242 buf.append(str); 243 buf.append("</TT>"); 244 return buf.toString(); 245 } 246 247 static String newRow(String label, String value) { 248 return newRow(label, value, 2); 249 } 250 251 static String newRow(String label, String value, int columnPerRow) { 252 if (label == null) { 253 label = ""; 254 } else { 255 label += ": "; 256 } 257 label = "<th nowrap align=right valign=top>" + label; 258 value = "<td colspan=" + (columnPerRow-1) + "> <font size =-1>" + value; 259 260 return "<tr>" + label + value + "</tr>"; 261 } 262 263 static String newRow(String label1, String value1, 264 String label2, String value2) { 265 label1 = "<th nowrap align=right valign=top>" + label1 + ": "; 266 value1 = "<td><font size =-1>" + value1; 267 label2 = "<th nowrap align=right valign=top>" + label2 + ": "; 268 value2 = "<td><font size =-1>" + value2; 269 270 return "<tr>" + label1 + value1 + label2 + value2 + "</tr>"; 271 } 272 273 }