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 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")) {
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, 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
|