// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Padding {
    // the funky way
    private static final String ONE_ZERO = "0";
    private static String pad1(String s, int length) {
        StringBuffer buffer = new StringBuffer(s);
        int l = buffer.length();
        if (l == length) {
            return s;
        }
        buffer.reverse();
        for (int i = 0; i < (length - l); i++) {
            buffer.append(ONE_ZERO);
        }
        buffer.reverse();
        return buffer.toString();
    }
	public static String theFunkyWay(int v) {
        return pad1(Integer.toString(v), 9);
	}
	// the hacker way
	private static final String ZEROES = "000000000";
    private static String pad2(String s, int length) {
    	return ZEROES.substring(s.length(), length) + s;
    }
	public static String theHackerWay(int v) {
		return pad2(Integer.toString(v), 9);
	}
	// the array way
    private static String pad3(String s, int length) {
    	char[] res = new char[length];
    	int padlen = length - s.length();
    	for(int i = 0; i < padlen; i++) res[i] = '0';
    	for(int i = 0; i < s.length(); i++) res[padlen + i] = s.charAt(i);
    	return new String(res);
    }
	public static String theArrayWay(int v) {
		return pad3(Integer.toString(v), 9);
	}
	// the smart way
	private static final NumberFormat nf = new DecimalFormat("000000000");
	public static String theSmartWay(int v) {
		return nf.format(v);
	}
	// the Java 5 way
	public static String theJava5Way(int v) {
		return String.format("%09d", v);
	}
	private static final int N = 10000000; 
	public static void main(String[] args) {
		long t1 = System.currentTimeMillis();
		for(int i = 0; i < N; i++) {
			if(!theFunkyWay(123).equals("000000123")) {
				System.out.println("Oops");
				System.exit(1);
			}
		}
		long t2 = System.currentTimeMillis();
		System.out.println("Funky : " + (t2 - t1));
		long t3 = System.currentTimeMillis();
		for(int i = 0; i < N; i++) {
			if(!theHackerWay(123).equals("000000123")) {
				System.out.println("Oops");
				System.exit(1);
			}
		}
		long t4 = System.currentTimeMillis();
		System.out.println("Hacker : " + (t4 - t3));
		long t5 = System.currentTimeMillis();
		for(int i = 0; i < N; i++) {
			if(!theArrayWay(123).equals("000000123")) {
				System.out.println("Oops");
				System.exit(1);
			}
		}
		long t6 = System.currentTimeMillis();
		System.out.println("Array : " + (t6 - t5));
		long t7 = System.currentTimeMillis();
		for(int i = 0; i < N; i++) {
			if(!theSmartWay(123).equals("000000123")) {
				System.out.println("Oops");
				System.exit(1);
			}
		}
		long t8 = System.currentTimeMillis();
		System.out.println("Smart : " + (t8 - t7));
		long t9 = System.currentTimeMillis();
		for(int i = 0; i < N; i++) {
			if(!theJava5Way(123).equals("000000123")) {
				System.out.println("Oops");
				System.exit(1);
			}
		}
		long t10 = System.currentTimeMillis();
		System.out.println("Java 5 : " + (t10 - t9));
	}
}
