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")) {
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
|
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
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
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
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")) {
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++) {
154 strings[i] = Resources.format(Messages.KBYTES, strings[i]);
155 }
156 return strings;
157 }
158
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
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) {
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(" ", " ");
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
|