1 /* 2 * File : FormattersImpl.java 3 * Created : 30-Mar-2004 4 * By : parg 5 * 6 * Azureus - a Java Bittorrent client 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details ( see the LICENSE file ). 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 */ 22 23 package org.gudy.azureus2.pluginsimpl.local.utils; 24 25 /** 26 * @author parg 27 * 28 */ 29 30 import java.io.IOException; 31 import java.util.Comparator; 32 import java.util.Map; 33 34 import org.gudy.azureus2.core3.util.*; 35 36 import org.gudy.azureus2.plugins.utils.*; 37 38 public class 39 FormattersImpl 40 implements Formatters 41 { 42 public FormattersImpl()43 FormattersImpl() 44 { 45 } 46 47 public String formatByteCountToKiBEtc( long bytes )48 formatByteCountToKiBEtc( 49 long bytes ) 50 { 51 return( DisplayFormatters.formatByteCountToKiBEtc( bytes )); 52 } 53 54 public String formatByteCountToKiBEtcPerSec( long bytes )55 formatByteCountToKiBEtcPerSec( 56 long bytes ) 57 { 58 return( DisplayFormatters.formatByteCountToKiBEtcPerSec( bytes )); 59 } 60 61 public String formatPercentFromThousands( long thousands )62 formatPercentFromThousands( 63 long thousands ) 64 { 65 return( DisplayFormatters.formatPercentFromThousands( (int)thousands )); 66 } 67 68 public String formatByteArray( byte[] data, boolean no_spaces )69 formatByteArray( 70 byte[] data, 71 boolean no_spaces ) 72 { 73 return( ByteFormatter.nicePrint( data, no_spaces )); 74 } 75 76 public String encodeBytesToString( byte[] bytes )77 encodeBytesToString( 78 byte[] bytes ) 79 { 80 return( ByteFormatter.encodeString( bytes )); 81 } 82 83 public byte[] decodeBytesFromString( String str )84 decodeBytesFromString( 85 String str ) 86 { 87 return( ByteFormatter.decodeString( str )); 88 } 89 formatDate(long millis)90 public String formatDate(long millis) { 91 return DisplayFormatters.formatCustomDateTime(millis); 92 } 93 formatTimeOnly(long millis)94 public String formatTimeOnly(long millis) { 95 return DisplayFormatters.formatCustomTimeOnly(millis); 96 } 97 formatTimeOnly(long millis, boolean with_secs)98 public String formatTimeOnly(long millis, boolean with_secs) { 99 return DisplayFormatters.formatCustomTimeOnly(millis, with_secs); 100 } 101 formatDateOnly(long millis)102 public String formatDateOnly(long millis) { 103 return DisplayFormatters.formatCustomDateOnly(millis); 104 } 105 106 107 public String formatTimeFromSeconds( long seconds )108 formatTimeFromSeconds( 109 long seconds ) 110 { 111 return( DisplayFormatters.formatTime( seconds*1000 )); 112 } 113 114 formatETAFromSeconds( long seconds )115 public String formatETAFromSeconds( long seconds ) { 116 return TimeFormatter.format( seconds ); 117 } 118 119 120 public byte[] bEncode( Map map )121 bEncode( 122 Map map ) 123 124 throws IOException 125 { 126 return( BEncoder.encode( map )); 127 } 128 129 public Map bDecode( byte[] data )130 bDecode( 131 byte[] data ) 132 133 throws IOException 134 { 135 return( BDecoder.decode( data )); 136 } 137 138 public String base32Encode( byte[] data )139 base32Encode( 140 byte[] data ) 141 { 142 return( Base32.encode( data )); 143 } 144 145 public byte[] base32Decode( String data )146 base32Decode( 147 String data ) 148 { 149 return( Base32.decode( data )); 150 } 151 152 public Comparator getAlphanumericComparator( final boolean ignore_case )153 getAlphanumericComparator( 154 final boolean ignore_case ) 155 { 156 return( 157 new Comparator() 158 { 159 public int 160 compare( 161 Object o1, 162 Object o2 ) 163 { 164 if ( o1 instanceof String && 165 o2 instanceof String ){ 166 167 String s1 = (String)o1; 168 String s2 = (String)o2; 169 170 int l1 = s1.length(); 171 int l2 = s2.length(); 172 173 int c1_pos = 0; 174 int c2_pos = 0; 175 176 while( c1_pos < l1 && c2_pos < l2 ){ 177 178 char c1 = s1.charAt( c1_pos++ ); 179 char c2 = s2.charAt( c2_pos++ ); 180 181 if ( Character.isDigit(c1) && Character.isDigit(c2)){ 182 183 int n1_pos = c1_pos-1; 184 int n2_pos = c2_pos-1; 185 186 while( c1_pos < l1 ){ 187 188 if ( !Character.isDigit( s1.charAt( c1_pos ))){ 189 190 break; 191 } 192 193 c1_pos++; 194 } 195 196 while(c2_pos<l2){ 197 198 if ( !Character.isDigit( s2.charAt( c2_pos ))){ 199 200 break; 201 } 202 203 c2_pos++; 204 } 205 206 int n1_length = c1_pos - n1_pos; 207 int n2_length = c2_pos - n2_pos; 208 209 if ( n1_length != n2_length ){ 210 211 return( n1_length - n2_length ); 212 } 213 214 for (int i=0;i<n1_length;i++){ 215 216 char nc1 = s1.charAt( n1_pos++ ); 217 char nc2 = s2.charAt( n2_pos++ ); 218 219 if ( nc1 != nc2 ){ 220 221 return( nc1 - nc2 ); 222 } 223 } 224 }else{ 225 226 if ( ignore_case ){ 227 228 c1 = Character.toLowerCase( c1 ); 229 230 c2 = Character.toLowerCase( c2 ); 231 } 232 233 if ( c1 != c2 ){ 234 235 return( c1 - c2 ); 236 } 237 } 238 } 239 240 return( l1 - l2); 241 242 }else{ 243 244 return( 0 ); 245 } 246 } 247 }); 248 } 249 } 250