1 | |
package org.kuali.maven.wagon; |
2 | |
|
3 | |
import java.text.NumberFormat; |
4 | |
|
5 | |
import org.apache.commons.lang.StringUtils; |
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
public class SimpleFormatter { |
14 | |
private static final double KB = 1024; |
15 | |
private static final double MB = 1024 * KB; |
16 | |
private static final double GB = 1024 * MB; |
17 | |
private static final double ONE_SECOND = 1000; |
18 | |
private static final double ONE_MINUTE = 60 * ONE_SECOND; |
19 | |
private static final double FIFTEEN_MINUTES = 15 * ONE_MINUTE; |
20 | |
|
21 | 1 | NumberFormat sizeFormatter = NumberFormat.getInstance(); |
22 | 1 | NumberFormat timeFormatter = NumberFormat.getInstance(); |
23 | 1 | NumberFormat rateFormatter = NumberFormat.getInstance(); |
24 | |
|
25 | |
public SimpleFormatter() { |
26 | 1 | super(); |
27 | 1 | sizeFormatter.setGroupingUsed(false); |
28 | 1 | sizeFormatter.setMaximumFractionDigits(1); |
29 | 1 | sizeFormatter.setMinimumFractionDigits(1); |
30 | 1 | timeFormatter.setGroupingUsed(false); |
31 | 1 | timeFormatter.setMaximumFractionDigits(3); |
32 | 1 | timeFormatter.setMinimumFractionDigits(3); |
33 | 1 | rateFormatter.setGroupingUsed(false); |
34 | 1 | rateFormatter.setMaximumFractionDigits(1); |
35 | 1 | rateFormatter.setMinimumFractionDigits(1); |
36 | 1 | } |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public String getRate(final long millis, final long bytes) { |
42 | 0 | int pad = 1; |
43 | 0 | double seconds = millis / 1000D; |
44 | 0 | double kilobytes = bytes / 1024D; |
45 | 0 | double kilobytesPerSecond = kilobytes / seconds; |
46 | 0 | if (kilobytesPerSecond < 1024) { |
47 | 0 | return StringUtils.leftPad(rateFormatter.format(kilobytesPerSecond) + " kB/s", pad, " "); |
48 | |
} else { |
49 | 0 | double transferRate = kilobytesPerSecond / 1024; |
50 | 0 | return StringUtils.leftPad(rateFormatter.format(transferRate) + " MB/s", pad, " "); |
51 | |
} |
52 | |
} |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public String getTime(final long millis) { |
58 | 0 | int pad = 1; |
59 | 0 | if (millis < ONE_SECOND) { |
60 | 0 | return StringUtils.leftPad(millis + "ms", pad, " "); |
61 | 0 | } else if (millis < 10 * ONE_SECOND) { |
62 | 0 | return StringUtils.leftPad(timeFormatter.format(millis / ONE_SECOND) + "s", pad, " "); |
63 | 0 | } else if (millis < FIFTEEN_MINUTES) { |
64 | 0 | return StringUtils.leftPad(rateFormatter.format(millis / ONE_SECOND) + "s", pad, " "); |
65 | |
} else { |
66 | 0 | return StringUtils.leftPad(rateFormatter.format(millis / ONE_MINUTE) + "m", pad, " "); |
67 | |
} |
68 | |
} |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public String getSize(final long bytes) { |
75 | 0 | int pad = 1; |
76 | 0 | if (bytes < MB) { |
77 | 0 | return StringUtils.leftPad(sizeFormatter.format(bytes / KB) + "k", pad, " "); |
78 | |
} |
79 | 0 | if (bytes < GB) { |
80 | 0 | return StringUtils.leftPad(sizeFormatter.format(bytes / MB) + "m", pad, " "); |
81 | |
} |
82 | 0 | return StringUtils.leftPad(sizeFormatter.format(bytes / GB) + "g", pad, " "); |
83 | |
} |
84 | |
} |