View Javadoc

1   package org.kuali.maven.wagon;
2   
3   import java.text.NumberFormat;
4   
5   import org.apache.commons.lang.StringUtils;
6   
7   /**
8    * Very simple formatter for formatting a few things - transfer rate, elapsed time, bytes
9    *
10   * @author Jeff Caddel
11   * @since May 27, 2010 6:46:17 PM
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      NumberFormat sizeFormatter = NumberFormat.getInstance();
22      NumberFormat timeFormatter = NumberFormat.getInstance();
23      NumberFormat rateFormatter = NumberFormat.getInstance();
24  
25      public SimpleFormatter() {
26          super();
27          sizeFormatter.setGroupingUsed(false);
28          sizeFormatter.setMaximumFractionDigits(1);
29          sizeFormatter.setMinimumFractionDigits(1);
30          timeFormatter.setGroupingUsed(false);
31          timeFormatter.setMaximumFractionDigits(3);
32          timeFormatter.setMinimumFractionDigits(3);
33          rateFormatter.setGroupingUsed(false);
34          rateFormatter.setMaximumFractionDigits(1);
35          rateFormatter.setMinimumFractionDigits(1);
36      }
37  
38      /**
39       * Given milliseconds and bytes return kilobytes per second
40       */
41      public String getRate(final long millis, final long bytes) {
42          int pad = 1;
43          double seconds = millis / 1000D;
44          double kilobytes = bytes / 1024D;
45          double kilobytesPerSecond = kilobytes / seconds;
46          if (kilobytesPerSecond < 1024) {
47              return StringUtils.leftPad(rateFormatter.format(kilobytesPerSecond) + " kB/s", pad, " ");
48          } else {
49              double transferRate = kilobytesPerSecond / 1024;
50              return StringUtils.leftPad(rateFormatter.format(transferRate) + " MB/s", pad, " ");
51          }
52      }
53  
54      /**
55       * Given milliseconds, return seconds or minutes
56       */
57      public String getTime(final long millis) {
58          int pad = 1;
59          if (millis < ONE_SECOND) {
60              return StringUtils.leftPad(millis + "ms", pad, " ");
61          } else if (millis < 10 * ONE_SECOND) {
62              return StringUtils.leftPad(timeFormatter.format(millis / ONE_SECOND) + "s", pad, " ");
63          } else if (millis < FIFTEEN_MINUTES) {
64              return StringUtils.leftPad(rateFormatter.format(millis / ONE_SECOND) + "s", pad, " ");
65          } else {
66              return StringUtils.leftPad(rateFormatter.format(millis / ONE_MINUTE) + "m", pad, " ");
67          }
68      }
69  
70      /**
71       * Given bytes, return kilobytes if it is less than a megabyte, megabytes if it is less than a gigabyte, otherwise
72       * gigabytes
73       */
74      public String getSize(final long bytes) {
75          int pad = 1;
76          if (bytes < MB) {
77              return StringUtils.leftPad(sizeFormatter.format(bytes / KB) + "k", pad, " ");
78          }
79          if (bytes < GB) {
80              return StringUtils.leftPad(sizeFormatter.format(bytes / MB) + "m", pad, " ");
81          }
82          return StringUtils.leftPad(sizeFormatter.format(bytes / GB) + "g", pad, " ");
83      }
84  }