View Javadoc

1   /**
2    * Copyright 2010-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.maven.wagon;
17  
18  import java.text.NumberFormat;
19  
20  import org.apache.commons.lang.StringUtils;
21  
22  /**
23   * Very simple formatter for formatting a few things - transfer rate, elapsed time, bytes
24   *
25   * @author Jeff Caddel
26   * @since May 27, 2010 6:46:17 PM
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       * Given milliseconds and bytes return kilobytes per second
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       * Given milliseconds, return seconds or minutes
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       * Given bytes, return kilobytes if it is less than a megabyte, megabytes if it is less than a gigabyte, otherwise
87       * gigabytes
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  }