FixedLength { public static String valueOf(int value, int size) { if (size > 12) throw new RuntimeException("FixedLength.valueOf support not available for sizes > 12 (" + size + " requested)"); long n = 1000000000000L + value; String digits = String.valueOf(n); return digits.substring(digits.length() - size); }  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 testValueOfInt() { assertEquals("1", FixedLength.valueOf(1, 1)); assertEquals("001", FixedLength.valueOf(1, 3)); assertEquals("", FixedLength.valueOf(5, 0)); assertEquals("05", FixedLength.valueOf(5, 2)); }
@Test public void testValueOfString() { assertEquals(" ", FixedLength.valueOf(null, 1)); assertEquals(" ", FixedLength.valueOf(null, 2)); assertEquals("1", FixedLength.valueOf("1", 1)); assertEquals("1 ", FixedLength.valueOf("1", 3)); assertEquals("", FixedLength.valueOf("5", 0)); assertEquals("5 ", FixedLength.valueOf("5", 2)); assertEquals("55", FixedLength.valueOf("5555", 2)); }
@Test(expected = RuntimeException.class) public void testTooLong() { String s = FixedLength.valueOf(1, 13); }