/** * 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 justifyCenterShouldPrependOneMoreCharacterThanAppendingForStringsThatAreTooShortButOfAnOddLength() { assertEquals(" Thing ", Strings.justifyCenter("Thing", 10, ' ')); }
@Test public void justifyCenterShouldTruncateStringsThatAreTooLong() { assertEquals("This is the st", Strings.justifyCenter("This is the string", 14, ' ')); }
@Test public void justifyCenterShouldReturnStringsThatAreTheDesiredLength() { assertEquals("This is the string", Strings.justifyCenter("This is the string", 18, ' ')); }
@Test public void justifyCenterShouldPrependCharacterForEmptyStrings() { assertEquals(" ", Strings.justifyCenter("", 10, ' ')); }
@Test public void justifyCenterShouldPrependCharacterForNullStrings() { assertEquals(" ", Strings.justifyCenter(null, 10, ' ')); }
@Test public void justifyCenterShouldPrependAndAppendSameNumberOfCharacterForStringsThatAreTooShortButOfAnEvenLength() { assertEquals(" This ", Strings.justifyCenter("This", 10, ' ')); }
/** * 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; }