FixedLength { public static String leadingZeros(String value, int size) { if (value == null) return zeroes(size); int n = value.length(); if (n == size) return value; else if (n > size) return value.substring(0, size); else return zeroes(size - n) + value; }  static String valueOf(int value, int size); static String valueOf(String value, int size); static String leadingZeros(String value, int size); static String spaces(int n); static String zeroes(int n); static boolean isPresent(String value); static boolean isPresent(List<?> list); static String emptyIfNull(String value);  }
@Test public void testLeadingZeros() { assertEquals("0001", FixedLength.leadingZeros("1", 4)); assertEquals("0012", FixedLength.leadingZeros("12", 4)); assertEquals("0123", FixedLength.leadingZeros("123", 4)); assertEquals("1234", FixedLength.leadingZeros("1234", 4)); assertEquals("1234", FixedLength.leadingZeros("12345", 4)); assertEquals("0000", FixedLength.leadingZeros(null, 4)); assertEquals("0000", FixedLength.leadingZeros("", 4)); assertEquals("000 ", FixedLength.leadingZeros(" ", 4)); assertEquals("00 ", FixedLength.leadingZeros(" ", 4)); assertEquals("0000", FixedLength.leadingZeros("0", 4)); assertEquals("0 0 ", FixedLength.leadingZeros(" 0 ", 4)); }