View Javadoc

1   package com.imcode.util;
2   
3   import org.apache.commons.lang.time.DateUtils;
4   import org.apache.commons.lang.StringUtils;
5   
6   import java.text.DecimalFormat;
7   import java.text.DecimalFormatSymbols;
8   import java.util.List;
9   import java.util.ArrayList;
10  
11  /***
12   * Utilities for preparing data for human consumption.
13   */
14  public class HumanReadable {
15  
16      private static final int ONE_KILOBYTE = 1024;
17      private static final int ONE_MEGABYTE = ONE_KILOBYTE * ONE_KILOBYTE;
18      private static final int ONE_GIGABYTE = ONE_MEGABYTE*ONE_KILOBYTE;
19  
20      /***
21       * Format a byte size like for example "1 kB" or "1.5 MB".
22       *
23       * @param size The size to be formatted
24       * @return formatted size
25       **/
26      public static String getHumanReadableByteSize( long size ) {
27          double displaySize = size ;
28          DecimalFormat df = new DecimalFormat( "#.#" );
29          DecimalFormatSymbols decimalFormatSymbols = df.getDecimalFormatSymbols();
30          decimalFormatSymbols.setDecimalSeparator( '.' );
31          df.setDecimalFormatSymbols( decimalFormatSymbols );
32          String sizeSuffix = "B";
33          if (displaySize >= ONE_GIGABYTE) {
34              displaySize /= ONE_GIGABYTE ;
35              sizeSuffix = "GB";
36          } else if ( displaySize >= ONE_MEGABYTE ) {
37              displaySize /= ONE_MEGABYTE;
38              sizeSuffix = "MB";
39          } else if ( displaySize >= ONE_KILOBYTE ) {
40              displaySize /= ONE_KILOBYTE;
41              sizeSuffix = "kB";
42          }
43          return df.format(displaySize)+" "+sizeSuffix ;
44      }
45  
46      /***
47       * Format a time span like for example "3h, 4m, 5s, 60ms"
48       *
49       * @param milliseconds
50       * @return formatted time span
51       */
52      public static String getHumanReadableTimeSpan(long milliseconds) {
53          long ms = milliseconds ;
54          TimeLengthSuffixPair[] pairs = new TimeLengthSuffixPair[] {
55              new TimeLengthSuffixPair( DateUtils.MILLIS_IN_HOUR, "h" ),
56              new TimeLengthSuffixPair( DateUtils.MILLIS_IN_MINUTE, "m" ),
57              new TimeLengthSuffixPair( DateUtils.MILLIS_IN_SECOND, "s" ),
58              new TimeLengthSuffixPair( 1, "ms" ),
59          };
60          List resultList = new ArrayList() ;
61          for ( int i = 0; i < pairs.length; i++ ) {
62              TimeLengthSuffixPair pair = pairs[i];
63              long timeLength = pair.timeLength;
64              if ( ms >= timeLength ) {
65                  long unitTime = ms / timeLength;
66                  ms %= timeLength;
67                  resultList.add( unitTime + pair.suffix );
68              }
69          }
70          return StringUtils.join( resultList.iterator(), ", " ) ;
71      }
72  
73      private static class TimeLengthSuffixPair {
74          long timeLength;
75          String suffix;
76  
77          TimeLengthSuffixPair( long timeLength, String suffix ) {
78              this.timeLength = timeLength;
79              this.suffix = suffix;
80          }
81      }
82  }