package org.gametrack.util; import java.text.SimpleDateFormat; import java.util.*; /** *
Title: BFTracker
*Description: Tracks online Battlefield 1942 games, outputs game records
*Copyright: Copyright (c) 2003
*Company: bf1942.gametrack.org
* @author Brian Cairns * @version 1.0 */ // routines for formatting output public class Formatting { private static SimpleDateFormat timestampFormat = new SimpleDateFormat( "yyMMdd:HHmmss.SSS" ); private static Date dateTemp = new Date(); // used for abbreviateFileSize() private final static String[] stPostfix = { "bytes", "KB", "MB", "GB", "TB" }; private final static int DEFAULT_PRECISION = 1; // private final static NumberFormat nf = NumberFormat.getInstance(); private static float pFactor = ( float )Math.pow( 10, DEFAULT_PRECISION ); /** * Returns a timestamp for the given time * @param time the given time * @return the formatted timestamp */ public static String timestamp( long time ) { synchronized( dateTemp ) { dateTemp.setTime( time ); return timestampFormat.format( dateTemp ); } } public static String timestamp() { return timestamp( System.currentTimeMillis() ); } /** * Round to the given number of places * @param val the value to round * @param places the number of places * @return the rounded number */ public static float round( double val, int places ) { double factor = Math.pow( 10, places ); return ( float )( Math.round(val*factor) / factor ); } public static long roundToNearest( long val, int factor ) { return Math.round( val / ( double )factor ) * factor; } /** * Converts an array to a string * @param arr the array * @return output as {a1,a2,a3...} */ public static String arrayToString( byte[] arr ) { if( arr == null ) return "NULL"; if( arr.length == 0 ) return "{}"; StringBuffer buf = new StringBuffer( "{" ); for( int i = 0; i < arr.length; i++ ) buf.append( arr[i] ).append( i + 1 == arr.length ? '}' : ',' ); return buf.toString(); } /** * Converts an array to a string * @param arr the array * @return output as {a1,a2,a3...} */ public static String arrayToString( Object[] arr ) { if( arr == null ) return "NULL"; if( arr.length == 0 ) return "{}"; StringBuffer buf = new StringBuffer( "{" ); for( int i = 0; i < arr.length; i++ ) buf.append( arr[i].toString() ).append( i + 1 == arr.length ? '}' : ',' ); return buf.toString(); } /** * Repeat a string * @param str the string to repeat * @param count number of times to repeat * @return the string repeated count times */ public static String repeat( String str, int count ) { if( str == null ) return null; if( count == 0 ) return ""; StringBuffer buf = new StringBuffer( str ); for( int i = 1; i < count; i++ ) buf.append( str ); return buf.toString(); } /** * Prints a list, with tabbed columns * @param strs the List * @param cols number of tabbed columns */ public static void printList( List strs, int cols ) { for( int i = 0; i < strs.size(); i+=cols ) for ( int c = 0; c < cols; c++ ) { try { System.out.print( strs.get( i + c ) + ( c + 1 == cols ? "\n" : "\t" ) ); } catch( ArrayIndexOutOfBoundsException ex ) { System.out.println( "