From 9143987fa02cb1d4be45b175fd7eb7b748fffb7b Mon Sep 17 00:00:00 2001 From: Niall Pemberton Date: Thu, 21 Jan 2010 18:22:15 +0000 Subject: [PATCH] LANG-405 rename method from truncateMiddle() to abbreviateMiddle() git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@901815 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang3/StringUtils.java | 98 +++++++++---------- .../apache/commons/lang3/StringUtilsTest.java | 80 +++++++-------- 2 files changed, 89 insertions(+), 89 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 82016aa23..f90382289 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -5511,6 +5511,55 @@ public class StringUtils { } return "..." + str.substring(str.length() - (maxWidth - 3)); } + + /** + *

Abbreviates a String to the length passed, replacing the middle characters with the supplied + * replacement String.

+ * + *

This abbreviation only occurs if the following criteria is met: + *

+ * Otherwise, the returned String will be the same as the supplied String for abbreviation. + *

+ * + *
+     * StringUtils.abbreviateMiddle(null, null, 0)      = null
+     * StringUtils.abbreviateMiddle("abc", null, 0)      = "abc"
+     * StringUtils.abbreviateMiddle("abc", ".", 0)      = "abc"
+     * StringUtils.abbreviateMiddle("abc", ".", 3)      = "abc"
+     * StringUtils.abbreviateMiddle("abcdef", ".", 4)     = "ab.f"
+     * 
+ * + * @param str the String to abbreviate, may be null + * @param middle the String to replace the middle characters with, may be null + * @param length the length to abbreviate str to. + * @return the abbreviated String if the above criteria is met, or the original String supplied for abbreviation. + */ + public static String abbreviateMiddle(String str, String middle, int length) { + if (isEmpty(str) || isEmpty(middle)) { + return str; + } + + if (length >= str.length() || length < (middle.length()+2)) { + return str; + } + + int targetSting = length-middle.length(); + int startOffset = targetSting/2+targetSting%2; + int endOffset = str.length()-targetSting/2; + + StringBuilder builder = new StringBuilder(length); + builder.append(str.substring(0,startOffset)); + builder.append(middle); + builder.append(str.substring(endOffset)); + + return builder.toString(); + } // Difference //----------------------------------------------------------------------- @@ -6052,53 +6101,4 @@ public class StringUtils { int strOffset = str.length() - suffix.length(); return str.regionMatches(ignoreCase, strOffset, suffix, 0, suffix.length()); } - - /** - *

Truncates a String to the length passed, replacing the middle characters with the supplied - * replacement String.

- * - *

This truncation only occurs if the following criteria is met: - *

- * Otherwise, the returned String will be the same as the supplied String for truncation. - *

- * - *
-     * StringUtils.truncateMiddle(null, null, 0)      = null
-     * StringUtils.truncateMiddle("abc", null, 0)      = "abc"
-     * StringUtils.truncateMiddle("abc", ".", 0)      = "abc"
-     * StringUtils.truncateMiddle("abc", ".", 3)      = "abc"
-     * StringUtils.truncateMiddle("abcdef", ".", 4)     = "ab.f"
-     * 
- * - * @param str the String to truncate, may be null - * @param middle the String to replace the middle characters with, may be null - * @param length the length to truncate str to. - * @return the truncated String if the above criteria is met, or the original String supplied for truncation. - */ - public static String truncateMiddle(String str, String middle, int length) { - if (isEmpty(str) || isEmpty(middle)) { - return str; - } - - if (length >= str.length() || length < (middle.length()+2)) { - return str; - } - - int targetSting = length-middle.length(); - int startOffset = targetSting/2+targetSting%2; - int endOffset = str.length()-targetSting/2; - - StringBuilder builder = new StringBuilder(length); - builder.append(str.substring(0,startOffset)); - builder.append(middle); - builder.append(str.substring(endOffset)); - - return builder.toString(); - } } diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index b5c192043..0011f8ad9 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -1474,6 +1474,46 @@ public class StringUtilsTest extends TestCase { assertEquals(message, expected, actual); } + public void testAbbreviateMiddle() { + // javadoc examples + assertNull( StringUtils.abbreviateMiddle(null, null, 0) ); + assertEquals( "abc", StringUtils.abbreviateMiddle("abc", null, 0) ); + assertEquals( "abc", StringUtils.abbreviateMiddle("abc", ".", 0) ); + assertEquals( "abc", StringUtils.abbreviateMiddle("abc", ".", 3) ); + assertEquals( "ab.f", StringUtils.abbreviateMiddle("abcdef", ".", 4) ); + + // JIRA issue (LANG-405) example (slightly different than actual expected result) + assertEquals( + "A very long text with un...f the text is complete.", + StringUtils.abbreviateMiddle( + "A very long text with unimportant stuff in the middle but interesting start and " + + "end to see if the text is complete.", "...", 50) ); + + // Test a much longer text :) + String longText = "Start text" + StringUtils.repeat("x", 10000) + "Close text"; + assertEquals( + "Start text->Close text", + StringUtils.abbreviateMiddle( longText, "->", 22 ) ); + + // Test negative length + assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", -1)); + + // Test boundaries + // Fails to change anything as method ensures first and last char are kept + assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 1)); + assertEquals("abc", StringUtils.abbreviateMiddle("abc", ".", 2)); + + // Test length of n=1 + assertEquals("a", StringUtils.abbreviateMiddle("a", ".", 1)); + + // Test smallest length that can lead to success + assertEquals("a.d", StringUtils.abbreviateMiddle("abcd", ".", 3)); + + // More from LANG-405 + assertEquals("a..f", StringUtils.abbreviateMiddle("abcdef", "..", 4)); + assertEquals("ab.ef", StringUtils.abbreviateMiddle("abcdef", ".", 5)); + } + //----------------------------------------------------------------------- public void testDifference_StringString() { assertEquals(null, StringUtils.difference(null, null)); @@ -1685,46 +1725,6 @@ public class StringUtilsTest extends TestCase { // StringUtils.remove("queued", 'z') = "queued" assertEquals("queued", StringUtils.remove("queued", 'z')); } - - public void testTruncateMiddle() { - // javadoc examples - assertNull( StringUtils.truncateMiddle(null, null, 0) ); - assertEquals( "abc", StringUtils.truncateMiddle("abc", null, 0) ); - assertEquals( "abc", StringUtils.truncateMiddle("abc", ".", 0) ); - assertEquals( "abc", StringUtils.truncateMiddle("abc", ".", 3) ); - assertEquals( "ab.f", StringUtils.truncateMiddle("abcdef", ".", 4) ); - - // JIRA issue (LANG-405) example (slightly different than actual expected result) - assertEquals( - "A very long text with un...f the text is complete.", - StringUtils.truncateMiddle( - "A very long text with unimportant stuff in the middle but interesting start and " + - "end to see if the text is complete.", "...", 50) ); - - // Test a much longer text :) - String longText = "Start text" + StringUtils.repeat("x", 10000) + "Close text"; - assertEquals( - "Start text->Close text", - StringUtils.truncateMiddle( longText, "->", 22 ) ); - - // Test negative length - assertEquals("abc", StringUtils.truncateMiddle("abc", ".", -1)); - - // Test boundaries - // Fails to change anything as method ensures first and last char are kept - assertEquals("abc", StringUtils.truncateMiddle("abc", ".", 1)); - assertEquals("abc", StringUtils.truncateMiddle("abc", ".", 2)); - - // Test length of n=1 - assertEquals("a", StringUtils.truncateMiddle("a", ".", 1)); - - // Test smallest length that can lead to success - assertEquals("a.d", StringUtils.truncateMiddle("abcd", ".", 3)); - - // More from LANG-405 - assertEquals("a..f", StringUtils.truncateMiddle("abcdef", "..", 4)); - assertEquals("ab.ef", StringUtils.truncateMiddle("abcdef", ".", 5)); - } public void testDifferenceAt_StringArray(){ assertEquals(-1, StringUtils.indexOfDifference(null));