001 package net.sf.logdistiller.util;
002
003 /*
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 import java.util.Arrays;
018 import java.util.Iterator;
019 import java.util.List;
020 import java.util.Locale;
021 import java.util.Set;
022
023 public class FormatUtil
024 {
025 private FormatUtil()
026 {
027 }
028
029 public static String formatPeriod( final long periodMs )
030 {
031 long base = ( periodMs < 60000 ) ? periodMs : ( periodMs + 500 );
032 int milliseconds = (int) ( base % 1000 );
033 long remainder = base / 1000;
034 int seconds = (int) ( remainder % 60 );
035 remainder /= 60;
036 int minutes = (int) ( remainder % 60 );
037 remainder /= 60;
038 int hours = (int) ( remainder % 24 );
039 remainder /= 24;
040 int days = (int) remainder;
041 StringBuffer buffer = new StringBuffer();
042 int fieldCount = 0;
043 if ( days > 0 )
044 {
045 fieldCount++;
046 buffer.append( days + " d" );
047 }
048 if ( ( fieldCount > 0 ) || ( hours > 0 ) )
049 {
050 buffer.append( ( ( fieldCount > 0 ) ? " " : "" ) + hours + " h" );
051 fieldCount++;
052 }
053 if ( ( fieldCount > 0 ) || ( minutes > 0 ) )
054 {
055 buffer.append( ( ( fieldCount > 0 ) ? " " : "" ) + minutes + " m" );
056 fieldCount++;
057 }
058 if ( ( fieldCount > 0 ) || ( seconds > 0 ) )
059 {
060 buffer.append( ( ( fieldCount > 0 ) ? " " : "" ) + seconds + " s" );
061 fieldCount++;
062 }
063 if ( fieldCount < 2 )
064 {
065 buffer.append( ( ( fieldCount > 0 ) ? " " : "" ) + milliseconds + " ms" );
066 }
067 return buffer.toString();
068 }
069
070 public static String formatSize( final long size )
071 {
072 if ( size < 1024 )
073 {
074 return String.valueOf( size ) + " b";
075 }
076 long sizeKb = size / 1024;
077 if ( sizeKb < 1024 )
078 {
079 return String.valueOf( sizeKb ) + '.' + String.valueOf( ( size % 1024 ) * 10 / 1024 ) + " kb";
080 }
081 long sizeMb = sizeKb / 1024;
082 return String.valueOf( sizeMb ) + '.' + String.valueOf( ( sizeKb % 1024 ) * 10 / 1024 ) + " Mb";
083 }
084
085 /**
086 * Formats a fraction as a "xx.x%" string.
087 *
088 * @param part int the portion of the total
089 * @param total int the total
090 * @return String the value of (fraction / total) as "xx.x%"
091 */
092 public static String formatFraction( long part, long total )
093 {
094 int fraction = Math.round( (float) ( part * 1000 ) / total );
095 return String.valueOf( fraction / 10 ) + '.' + ( fraction % 10 ) + '%';
096 }
097
098 public static String join( String separator, Iterator iter )
099 {
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 list )
118 {
119 return join( separator, list.iterator() );
120 }
121
122 public static String join( String separator, Set 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 }