/** * Justify the contents of the string. * * @param justify the way in which the string is to be justified * @param str the string to be right justified; if null, an empty string is used * @param width the desired width of the string; must be positive * @param padWithChar the character to use for padding, if needed * @return the right justified string */ public static String justify(Justify justify, String str, final int width, char padWithChar) { switch (justify) { case LEFT: return justifyLeft(str, width, padWithChar); case RIGHT: return justifyRight(str, width, padWithChar); case CENTER: return justifyCenter(str, width, padWithChar); } assert false; return null; }
@Test public void justifyRightShouldPrependCharacterForStringsThatAreTooShort() { assertEquals(" This", Strings.justifyRight("This", 10, ' ')); }
@Test public void justifyRightShouldPrependCharacterForEmptyStrings() { assertEquals(" ", Strings.justifyRight("", 10, ' ')); }
@Test public void justifyRightShouldTruncateStringsThatAreTooLong() { assertEquals(" is the string", Strings.justifyRight("This is the string", 14, ' ')); }
@Test public void justifyRightShouldPrependCharacterForNullStrings() { assertEquals(" ", Strings.justifyRight(null, 10, ' ')); }
@Test public void justifyRightShouldReturnStringsThatAreTheDesiredLength() { assertEquals("This is the string", Strings.justifyRight("This is the string", 18, ' ')); }
/** * Justify the contents of the string. * * @param justify the way in which the string is to be justified * @param str the string to be right justified; if null, an empty string is used * @param width the desired width of the string; must be positive * @param padWithChar the character to use for padding, if needed * @return the right justified string */ public static String justify(Justify justify, String str, final int width, char padWithChar) { switch (justify) { case LEFT: return justifyLeft(str, width, padWithChar); case RIGHT: return justifyRight(str, width, padWithChar); case CENTER: return justifyCenter(str, width, padWithChar); } assert false; return null; }