1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.util;
21
22
23
24 import java.text.DateFormat;
25 import java.text.NumberFormat;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28 import java.util.StringTokenizer;
29
30
31 /***
32 * Helper class providing static methods for formatting different number and
33 * date formats.
34 */
35 public class Formatter
36 {
37
38
39
40 public static final int LEFT = 0;
41 public static final int CENTER = 1;
42 public static final int RIGHT = 2;
43
44 public static final String[] SIZES = { "B", "KB", "MB", "GB", "TB" };
45
46
47
48
49
50
51
52 /***
53 * Formats double as decimal number.
54 *
55 * @param number number to be formatted
56 * @param decimal the number of digits allowed in the fraction portion of
57 * a number
58 * @return formatted number as string
59 */
60 public static String formatNumber(double number, int decimal)
61 {
62 NumberFormat n = NumberFormat.getNumberInstance();
63
64 if (decimal > 0) {
65 n.setMinimumFractionDigits(decimal);
66 n.setMaximumFractionDigits(decimal);
67 }
68
69 return n.format(number);
70 }
71
72 /***
73 * Formats a number without any digits in the fraction portion.
74 *
75 * @param number number to be formatted
76 * @return formatted number as string
77 */
78 public static String formatNumber(double number)
79 {
80 return formatNumber(number, 0);
81 }
82
83 /***
84 * Returns <code>value</code> (byte) formatted as a file size.
85 * For example value=2048 returns "2 kb".
86 *
87 * @param size filesize to be formatted
88 * @return formatted number as string
89 */
90 public static String formatSize(double size)
91 {
92 int i = 0;
93 for (; i < SIZES.length - 1 && size >= 1024; i++) {
94 size /= 1024;
95 }
96
97 return formatNumber(size, 1) + " " + SIZES[i];
98 }
99
100 /***
101 * Formats number of seconds in appropriate time unit
102 *
103 * @param i number of seconds
104 * @return formatted duration as string
105 */
106 public static String formatLength(long i)
107 {
108 StringBuffer s = new StringBuffer();
109
110 long x = (i / 3600);
111 if (x > 0) {
112 s.append(x);
113 s.append(":");
114 }
115 x = (i % 3600) / 60;
116 if (x < 10) {
117 s.append("0");
118 }
119 s.append(x);
120 s.append(":");
121 x = (i % 60);
122 if (x < 10) {
123 s.append("0");
124 }
125 s.append(x);
126
127 return s.toString();
128 }
129
130 /***
131 * Formats date.
132 */
133 public static String formatDate(Date date)
134 {
135 return DateFormat.getDateInstance().format(date);
136 }
137
138 /***
139 * Formats time from given milliseconds time value.
140 *
141 * @param time milliseconds since January 1, 1970, 00:00:00 GMT not to
142 * exceed the milliseconds representation for the year 8099. A negative
143 * number indicates the number of milliseconds before January 1, 1970,
144 * 00:00:00 GMT.
145 */
146 public static String formatTime(long time)
147 {
148 return DateFormat.getDateInstance().format(new Date(time));
149 }
150
151 /***
152 * Formats a row of strings according to given alignments for the console
153 * output.
154 *
155 * @param rows array of strings to be formatted
156 * @param colAlignment array of alignment policies
157 */
158 public static String formatTable(String[] rows, int[] colAlignment)
159 {
160 if (rows.length == 0) {
161 return "(list is empty)";
162 }
163
164 int[] widths = new int[colAlignment.length];
165
166 for (int i = 0; i < rows.length; i++) {
167 StringTokenizer t = new StringTokenizer(rows[i], "|");
168 for (int col = 0; t.hasMoreTokens(); col++) {
169 int l = t.nextToken().length();
170 if (l > widths[col]) {
171 widths[col] = l;
172 }
173 }
174 }
175
176 StringBuffer sb = new StringBuffer();
177 for (int i = 0; i < rows.length; i++) {
178 StringTokenizer t = new StringTokenizer(rows[i], "|");
179 for (int col = 0; col < colAlignment.length; col++) {
180 String s = (t.hasMoreTokens()) ? t.nextToken() : "";
181 int appendCount = widths[col] - s.length();
182
183 switch (colAlignment[col]) {
184 case LEFT:
185 sb.append(s);
186 sb.append(fill(" ", appendCount));
187 break;
188 case CENTER:
189 int a = appendCount / 2;
190 sb.append(fill(" ", a));
191 sb.append(s);
192 sb.append(fill(" ", appendCount - a));
193 break;
194 case RIGHT:
195 sb.append(fill(" ", appendCount));
196 sb.append(s);
197 break;
198 }
199
200 if (col < colAlignment.length - 1) {
201 sb.append(" | ");
202 }
203 }
204 sb.append("\n");
205 }
206
207 return sb.toString();
208 }
209
210
211 /***
212 * Returns <code>t</code> formatted as a logfile entry in CLF.
213 *
214 * Example: joe@server - - [Date] "mymusic.ogg" "finished" 434339 - "XNap"
215 */
216
217
218
219
220
221
222
223
224
225
226 /***
227 * Creates a string from a string being <code>count</code> times appended
228 * to itself.
229 *
230 * @param s string
231 * @param count number of times it's appended to itself
232 */
233 public static String fill(String s, int count)
234 {
235 StringBuffer sb = new StringBuffer(s.length() * count);
236 for (int i = 0; i < count; i++) {
237 sb.append(s);
238 }
239 return sb.toString();
240 }
241
242
243 /***
244 * Returns the current date in the format needed for CLF.
245 *
246 * Example: 15/Dec/2002:23:46:16
247 */
248 public static String getCLFDate()
249 {
250 String pattern = "dd/MMM/yyyy:HH:mm:ss";
251 SimpleDateFormat df = new SimpleDateFormat(pattern);
252 return df.format(new Date());
253 }
254
255 /***
256 * Returns a string of the current date in short format.
257 *
258 * Example: 11.19.80
259 */
260 public static String shortDate()
261 {
262 return DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date());
263 }
264 }