Coverage Report - org.kuali.maven.mojo.s3.SimpleFormatter
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleFormatter
0%
0/36
0%
0/12
4
 
 1  
 package org.kuali.maven.mojo.s3;
 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  0
     NumberFormat sizeFormatter = NumberFormat.getInstance();
 22  0
     NumberFormat timeFormatter = NumberFormat.getInstance();
 23  0
     NumberFormat rateFormatter = NumberFormat.getInstance();
 24  
 
 25  
     public SimpleFormatter() {
 26  0
         super();
 27  0
         sizeFormatter.setGroupingUsed(false);
 28  0
         sizeFormatter.setMaximumFractionDigits(1);
 29  0
         sizeFormatter.setMinimumFractionDigits(1);
 30  0
         timeFormatter.setGroupingUsed(false);
 31  0
         timeFormatter.setMaximumFractionDigits(3);
 32  0
         timeFormatter.setMinimumFractionDigits(3);
 33  0
         rateFormatter.setGroupingUsed(false);
 34  0
         rateFormatter.setMaximumFractionDigits(1);
 35  0
         rateFormatter.setMinimumFractionDigits(1);
 36  0
     }
 37  
 
 38  
     /**
 39  
      * Given milliseconds and bytes return kilobytes per second
 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  
      * Given milliseconds, return seconds or minutes
 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  
      * 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  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  
 }