1 package net.sf.logdistiller.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import java.util.Arrays;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Locale;
21 import java.util.Set;
22
23 public class FormatUtil
24 {
25 private FormatUtil()
26 {
27 }
28
29 public static String formatPeriod( final long periodMs )
30 {
31 long base = ( periodMs < 60000 ) ? periodMs : ( periodMs + 500 );
32 int milliseconds = (int) ( base % 1000 );
33 long remainder = base / 1000;
34 int seconds = (int) ( remainder % 60 );
35 remainder /= 60;
36 int minutes = (int) ( remainder % 60 );
37 remainder /= 60;
38 int hours = (int) ( remainder % 24 );
39 remainder /= 24;
40 int days = (int) remainder;
41 StringBuffer buffer = new StringBuffer();
42 int fieldCount = 0;
43 if ( days > 0 )
44 {
45 fieldCount++;
46 buffer.append( days + " d" );
47 }
48 if ( ( fieldCount > 0 ) || ( hours > 0 ) )
49 {
50 buffer.append( ( ( fieldCount > 0 ) ? " " : "" ) + hours + " h" );
51 fieldCount++;
52 }
53 if ( ( fieldCount > 0 ) || ( minutes > 0 ) )
54 {
55 buffer.append( ( ( fieldCount > 0 ) ? " " : "" ) + minutes + " m" );
56 fieldCount++;
57 }
58 if ( ( fieldCount > 0 ) || ( seconds > 0 ) )
59 {
60 buffer.append( ( ( fieldCount > 0 ) ? " " : "" ) + seconds + " s" );
61 fieldCount++;
62 }
63 if ( fieldCount < 2 )
64 {
65 buffer.append( ( ( fieldCount > 0 ) ? " " : "" ) + milliseconds + " ms" );
66 }
67 return buffer.toString();
68 }
69
70 public static String formatSize( final long size )
71 {
72 if ( size < 1024 )
73 {
74 return String.valueOf( size ) + " b";
75 }
76 long sizeKb = size / 1024;
77 if ( sizeKb < 1024 )
78 {
79 return String.valueOf( sizeKb ) + '.' + String.valueOf( ( size % 1024 ) * 10 / 1024 ) + " kb";
80 }
81 long sizeMb = sizeKb / 1024;
82 return String.valueOf( sizeMb ) + '.' + String.valueOf( ( sizeKb % 1024 ) * 10 / 1024 ) + " Mb";
83 }
84
85
86
87
88
89
90
91
92 public static String formatFraction( long part, long total )
93 {
94 int fraction = Math.round( (float) ( part * 1000 ) / total );
95 return String.valueOf( fraction / 10 ) + '.' + ( fraction % 10 ) + '%';
96 }
97
98 public static String join( String separator, Iterator<? extends Object> iter )
99 {
100 StringBuffer buff = new StringBuffer();
101 while ( iter.hasNext() )
102 {
103 if ( buff.length() > 0 )
104 {
105 buff.append( separator );
106 }
107 buff.append( iter.next() );
108 }
109 return buff.toString();
110 }
111
112 public static String join( String separator, Object[] values )
113 {
114 return join( separator, Arrays.asList( values ).iterator() );
115 }
116
117 public static String join( String separator, List<? extends Object> list )
118 {
119 return join( separator, list.iterator() );
120 }
121
122 public static String join( String separator, Set<? extends Object> set )
123 {
124 return join( separator, set.iterator() );
125 }
126
127 public static Locale parseLocale( String locale )
128 {
129 int len = locale.length();
130 if ( ( len != 2 ) && ( len != 5 ) && ( len != 8 ) )
131 {
132 throw new RuntimeException( "bad locale format: '" + locale + "', sould be xx?yy?zz" );
133 }
134 String lang = locale.substring( 0, 2 );
135 if ( len == 2 )
136 {
137 return new Locale( lang );
138 }
139 String country = locale.substring( 3, 5 );
140 if ( len == 5 )
141 {
142 return new Locale( lang, country );
143 }
144 String variant = locale.substring( 6 );
145 return new Locale( lang, country, variant );
146 }
147 }