Path: blob/master/src/jdk.jconsole/share/classes/sun/tools/jconsole/Formatter.java
40948 views
/*1* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jconsole;2627import java.text.*;28import java.util.*;293031class Formatter {32static final long SECOND = 1000;33static final long MINUTE = 60 * SECOND;34static final long HOUR = 60 * MINUTE;35static final long DAY = 24 * HOUR;3637static final String cr = System.getProperty("line.separator");3839static final DateFormat timeDF = new SimpleDateFormat("HH:mm");40private static final DateFormat timeWithSecondsDF = new SimpleDateFormat("HH:mm:ss");41private static final DateFormat dateDF = new SimpleDateFormat("yyyy-MM-dd");42private static final String decimalZero =43new DecimalFormatSymbols().getDecimalSeparator() + "0";4445static String formatTime(long t) {46String str;47if (t < 1 * MINUTE) {48String seconds = String.format("%.3f", t / (double)SECOND);49str = Resources.format(Messages.DURATION_SECONDS, seconds);50} else {51long remaining = t;52long days = remaining / DAY;53remaining %= 1 * DAY;54long hours = remaining / HOUR;55remaining %= 1 * HOUR;56long minutes = remaining / MINUTE;5758if (t >= 1 * DAY) {59str = Resources.format(Messages.DURATION_DAYS_HOURS_MINUTES,60days, hours, minutes);61} else if (t >= 1 * HOUR) {62str = Resources.format(Messages.DURATION_HOURS_MINUTES,63hours, minutes);64} else {65str = Resources.format(Messages.DURATION_MINUTES, minutes);66}67}68return str;69}7071static String formatNanoTime(long t) {72long ms = t / 1000000;73return formatTime(ms);74}757677static String formatClockTime(long time) {78return timeDF.format(time);79}8081static String formatDate(long time) {82return dateDF.format(time);83}8485static String formatDateTime(long time) {86return dateDF.format(time) + " " + timeWithSecondsDF.format(time);87}8889static DateFormat getDateTimeFormat(String dtfStr) {90int dateStyle = -1;91int timeStyle = -1;9293if (dtfStr.startsWith("SHORT")) {94dateStyle = DateFormat.SHORT;95} else if (dtfStr.startsWith("MEDIUM")) {96dateStyle = DateFormat.MEDIUM;97} else if (dtfStr.startsWith("LONG")) {98dateStyle = DateFormat.LONG;99} else if (dtfStr.startsWith("FULL")) {100dateStyle = DateFormat.FULL;101}102103if (dtfStr.endsWith("SHORT")) {104timeStyle = DateFormat.SHORT;105} else if (dtfStr.endsWith("MEDIUM")) {106timeStyle = DateFormat.MEDIUM;107} else if (dtfStr.endsWith("LONG")) {108timeStyle = DateFormat.LONG;109} else if (dtfStr.endsWith("FULL")) {110timeStyle = DateFormat.FULL;111}112113if (dateStyle != -1 && timeStyle != -1) {114return DateFormat.getDateTimeInstance(dateStyle, timeStyle);115} else if (dtfStr.length() > 0) {116return new SimpleDateFormat(dtfStr);117} else {118return DateFormat.getDateTimeInstance();119}120}121122static double toExcelTime(long time) {123// Excel is bug compatible with Lotus 1-2-3 and pretends124// that 1900 was a leap year, so count from 1899-12-30.125// Note that the month index is zero-based in Calendar.126Calendar cal = new GregorianCalendar(1899, 11, 30);127128// Adjust for the fact that now may be DST but then wasn't129Calendar tmpCal = new GregorianCalendar();130tmpCal.setTimeInMillis(time);131int dst = tmpCal.get(Calendar.DST_OFFSET);132if (dst > 0) {133cal.set(Calendar.DST_OFFSET, dst);134}135136long millisSince1900 = time - cal.getTimeInMillis();137double value = (double)millisSince1900 / (24 * 60 * 60 * 1000);138139return value;140}141142143144static String[] formatKByteStrings(long... bytes) {145int n = bytes.length;146for (int i = 0; i < n; i++) {147if (bytes[i] > 0) {148bytes[i] /= 1024;149}150}151String[] strings = formatLongs(bytes);152for (int i = 0; i < n; i++) {153strings[i] = Resources.format(Messages.KBYTES, strings[i]);154}155return strings;156}157158static String formatKBytes(long bytes) {159if (bytes == -1) {160return Resources.format(Messages.KBYTES, "-1");161}162163long kb = bytes / 1024;164return Resources.format(Messages.KBYTES, justify(kb, 10));165}166167168static String formatBytes(long v, boolean html) {169return formatBytes(v, v, html);170}171172static String formatBytes(long v, long vMax) {173return formatBytes(v, vMax, false);174}175176static String formatBytes(long v, long vMax, boolean html) {177String s;178179int exp = (int)Math.log10((double)vMax);180181if (exp < 3) {182s = Resources.format(Messages.SIZE_BYTES, v);183} else if (exp < 6) {184s = Resources.format(Messages.SIZE_KB, trimDouble(v / Math.pow(10.0, 3)));185} else if (exp < 9) {186s = Resources.format(Messages.SIZE_MB, trimDouble(v / Math.pow(10.0, 6)));187} else {188s = Resources.format(Messages.SIZE_GB, trimDouble(v / Math.pow(10.0, 9)));189}190if (html) {191s = s.replace(" ", " ");192}193return s;194}195196/*197* Return the input value rounded to one decimal place. If after198* rounding the string ends in the (locale-specific) decimal point199* followed by a zero then trim that off as well.200*/201private static String trimDouble(double d) {202String s = String.format("%.1f", d);203if (s.length() > 3 && s.endsWith(decimalZero)) {204s = s.substring(0, s.length()-2);205}206return s;207}208209static String formatLong(long value) {210return String.format("%,d", value);211}212213static String[] formatLongs(long... longs) {214int n = longs.length;215int size = 0;216String[] strings = new String[n];217for (int i = 0; i < n; i++) {218strings[i] = formatLong(longs[i]);219size = Math.max(size, strings[i].length());220}221for (int i = 0; i < n; i++) {222strings[i] = justify(strings[i], size);223}224return strings;225}226227228// A poor attempt at right-justifying for numerical data229static String justify(long value, int size) {230return justify(formatLong(value), size);231}232233static String justify(String str, int size) {234StringBuilder sb = new StringBuilder();235sb.append("<TT>");236int n = size - str.length();237for (int i = 0; i < n; i++) {238sb.append(" ");239}240sb.append(str);241sb.append("</TT>");242return sb.toString();243}244245static String newRow(String label, String value) {246return newRow(label, value, 2);247}248249static String newRow(String label, String value, int columnPerRow) {250if (label == null) {251label = "";252} else {253label += ": ";254}255label = "<th nowrap align=right valign=top>" + label;256value = "<td colspan=" + (columnPerRow-1) + "> <font size =-1>" + value;257258return "<tr>" + label + value + "</tr>";259}260261static String newRow(String label1, String value1,262String label2, String value2) {263label1 = "<th nowrap align=right valign=top>" + label1 + ": ";264value1 = "<td><font size =-1>" + value1;265label2 = "<th nowrap align=right valign=top>" + label2 + ": ";266value2 = "<td><font size =-1>" + value2;267268return "<tr>" + label1 + value1 + label2 + value2 + "</tr>";269}270271}272273274