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