author | sla |
Tue, 19 Mar 2013 13:26:42 +0100 | |
changeset 18197 | ae73e4f50e08 |
parent 14342 | 8435a30053c1 |
child 24969 | afa6934dd8e8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
12851
diff
changeset
|
2 |
* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
||
46 |
static String formatTime(long t) { |
|
47 |
String str; |
|
48 |
if (t < 1 * MINUTE) { |
|
49 |
String seconds = String.format("%.3f", t / (double)SECOND); |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
50 |
str = Resources.format(Messages.DURATION_SECONDS, seconds); |
2 | 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) { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
60 |
str = Resources.format(Messages.DURATION_DAYS_HOURS_MINUTES, |
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
61 |
days, hours, minutes); |
2 | 62 |
} else if (t >= 1 * HOUR) { |
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
63 |
str = Resources.format(Messages.DURATION_HOURS_MINUTES, |
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
64 |
hours, minutes); |
2 | 65 |
} else { |
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
66 |
str = Resources.format(Messages.DURATION_MINUTES, minutes); |
2 | 67 |
} |
68 |
} |
|
69 |
return str; |
|
70 |
} |
|
71 |
||
72 |
static String formatNanoTime(long t) { |
|
73 |
long ms = t / 1000000; |
|
74 |
return formatTime(ms); |
|
75 |
} |
|
76 |
||
77 |
||
78 |
static String formatClockTime(long time) { |
|
79 |
return timeDF.format(time); |
|
80 |
} |
|
81 |
||
82 |
static String formatDate(long time) { |
|
83 |
return dateDF.format(time); |
|
84 |
} |
|
85 |
||
86 |
static String formatDateTime(long time) { |
|
87 |
return dateDF.format(time) + " " + timeWithSecondsDF.format(time); |
|
88 |
} |
|
89 |
||
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
90 |
static DateFormat getDateTimeFormat(String dtfStr) { |
2 | 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 |
||
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 |
||
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++) { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
154 |
strings[i] = Resources.format(Messages.KBYTES, strings[i]); |
2 | 155 |
} |
156 |
return strings; |
|
157 |
} |
|
158 |
||
159 |
static String formatKBytes(long bytes) { |
|
160 |
if (bytes == -1) { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
161 |
return Resources.format(Messages.KBYTES, "-1"); |
2 | 162 |
} |
163 |
||
164 |
long kb = bytes / 1024; |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
165 |
return Resources.format(Messages.KBYTES, justify(kb, 10)); |
2 | 166 |
} |
167 |
||
168 |
||
169 |
static String formatBytes(long v, boolean html) { |
|
170 |
return formatBytes(v, v, html); |
|
171 |
} |
|
172 |
||
173 |
static String formatBytes(long v, long vMax) { |
|
174 |
return formatBytes(v, vMax, false); |
|
175 |
} |
|
176 |
||
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) { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
183 |
s = Resources.format(Messages.SIZE_BYTES, v); |
2 | 184 |
} else if (exp < 6) { |
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
185 |
s = Resources.format(Messages.SIZE_KB, trimDouble(v / Math.pow(10.0, 3))); |
2 | 186 |
} else if (exp < 9) { |
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
187 |
s = Resources.format(Messages.SIZE_MB, trimDouble(v / Math.pow(10.0, 6))); |
2 | 188 |
} else { |
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
189 |
s = Resources.format(Messages.SIZE_GB, trimDouble(v / Math.pow(10.0, 9))); |
2 | 190 |
} |
191 |
if (html) { |
|
192 |
s = s.replace(" ", " "); |
|
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 |
*/ |
|
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 |
||
210 |
static String formatLong(long value) { |
|
211 |
return String.format("%,d", value); |
|
212 |
} |
|
213 |
||
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 |
|
230 |
static String justify(long value, int size) { |
|
231 |
return justify(formatLong(value), size); |
|
232 |
} |
|
233 |
||
234 |
static String justify(String str, int size) { |
|
235 |
StringBuffer buf = new StringBuffer(); |
|
236 |
buf.append("<TT>"); |
|
237 |
int n = size - str.length(); |
|
238 |
for (int i = 0; i < n; i++) { |
|
239 |
buf.append(" "); |
|
240 |
} |
|
241 |
buf.append(str); |
|
242 |
buf.append("</TT>"); |
|
243 |
return buf.toString(); |
|
244 |
} |
|
245 |
||
246 |
static String newRow(String label, String value) { |
|
247 |
return newRow(label, value, 2); |
|
248 |
} |
|
249 |
||
250 |
static String newRow(String label, String value, int columnPerRow) { |
|
251 |
if (label == null) { |
|
252 |
label = ""; |
|
253 |
} else { |
|
254 |
label += ": "; |
|
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 |
||
262 |
static String newRow(String label1, String value1, |
|
263 |
String label2, String value2) { |
|
264 |
label1 = "<th nowrap align=right valign=top>" + label1 + ": "; |
|
265 |
value1 = "<td><font size =-1>" + value1; |
|
266 |
label2 = "<th nowrap align=right valign=top>" + label2 + ": "; |
|
267 |
value2 = "<td><font size =-1>" + value2; |
|
268 |
||
269 |
return "<tr>" + label1 + value1 + label2 + value2 + "</tr>"; |
|
270 |
} |
|
271 |
||
272 |
} |