From aee5d512ad5c0d5a9aa17aba52f3dc4a3b79c652 Mon Sep 17 00:00:00 2001 From: Shekhar Gulati Date: Wed, 4 May 2016 19:46:48 +0530 Subject: [PATCH] Added support for `collapseWhitespace` function --- README.md | 10 +++++++ src/main/java/strman/Strman.java | 39 ++++++++++++++++++++-------- src/test/java/strman/StrmanTest.java | 34 ++++++++++++++++++------ 3 files changed, 64 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 2f416db..ab7a129 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,16 @@ char[] result = chars(title); ``` +## collapseWhitespace(value) + +Replace consecutive whitespace characters with a single space. + +```java +final String input = " foo bar "; +final String result = collapseWhitespace(title); +// result = "foo bar"; +``` + ## Other functions that can be added diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index e3df10a..e06ec45 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -3,6 +3,7 @@ package strman; import java.util.Arrays; import java.util.Optional; import java.util.StringJoiner; +import java.util.function.Predicate; import java.util.function.Supplier; /** @@ -10,6 +11,10 @@ import java.util.function.Supplier; */ public interface Strman { + Predicate NULL_STRING_PREDICATE = str -> str == null; + Predicate EMPTY_STRING_PREDICATE = str -> str.length() == 0; + Predicate NULL_AND_EMPTY_STRING_PREDICATE = NULL_STRING_PREDICATE.and(EMPTY_STRING_PREDICATE); + String EMPTY_STRING = " "; /** * Appends Strings to value @@ -30,7 +35,7 @@ public interface Strman { * @return full String */ static String appendArray(final String value, final String[] appends) { - validate(value, () -> "'value' should be not null."); + validate(value, NULL_STRING_PREDICATE, () -> "'value' should be not null."); if (appends == null || appends.length == 0) { return value; } @@ -71,20 +76,14 @@ public interface Strman { */ static String[] between(final String value, final String start, final String end) { - validate(value, () -> "'value' should be not null."); - validate(start, () -> "'start' should be not null."); - validate(end, () -> "'end' should be not null."); + validate(value, NULL_STRING_PREDICATE, () -> "'value' should be not null."); + validate(start, NULL_STRING_PREDICATE, () -> "'start' should be not null."); + validate(end, NULL_STRING_PREDICATE, () -> "'end' should be not null."); String[] parts = value.split(end); return Arrays.stream(parts).map(subPart -> subPart.substring(subPart.indexOf(start) + start.length())).toArray(String[]::new); } - static void validate(final String value, final Supplier supplier) { - if (value == null) { - throw new IllegalArgumentException(supplier.get()); - } - } - /** * Returns a String array consisting of the characters in the String. * @@ -96,7 +95,7 @@ public interface Strman { * The other implementation of this could be using String's split method * String[] chars = value.split("") */ - validate(value, () -> "'value' should be not null."); + validate(value, NULL_STRING_PREDICATE, () -> "'value' should be not null."); int length = value.length(); String[] result = new String[length]; for (int i = 0; i < length; i++) { @@ -104,4 +103,22 @@ public interface Strman { } return result; } + + + /** + * Replace consecutive whitespace characters with a single space. + * + * @param value input String + * @return collapsed String + */ + static String collapseWhitespace(final String value) { + validate(value, NULL_STRING_PREDICATE, () -> "'value' should be not null."); + return value.trim().replaceAll("\\s\\s+", " "); + } + + static void validate(String value, Predicate predicate, final Supplier supplier) { + if (predicate.test(value)) { + throw new IllegalArgumentException(supplier.get()); + } + } } diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index 0c5825f..d775374 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -2,6 +2,7 @@ package strman; import org.junit.Test; +import java.util.Arrays; import java.util.Optional; import static org.hamcrest.CoreMatchers.equalTo; @@ -12,31 +13,31 @@ import static strman.Strman.*; public class StrmanTest { @Test - public void shouldAppendStringsToEndOfValue() throws Exception { + public void append_shouldAppendStringsToEndOfValue() throws Exception { assertThat(append("f", "o", "o", "b", "a", "r"), equalTo("foobar")); assertThat(append("foobar"), equalTo("foobar")); assertThat(append("", "foobar"), equalTo("foobar")); } @Test(expected = IllegalArgumentException.class) - public void appendShouldThrowIllegalArgumentExceptionWhenValueIsNull() throws Exception { + public void append_shouldThrowIllegalArgumentExceptionWhenValueIsNull() throws Exception { append(null); } @Test - public void shouldAppendStringArrayToEndOfValue() throws Exception { + public void appendArray_shouldAppendStringArrayToEndOfValue() throws Exception { assertThat(appendArray("f", new String[]{"o", "o", "b", "a", "r"}), equalTo("foobar")); assertThat(appendArray("foobar", new String[]{}), equalTo("foobar")); assertThat(appendArray("", new String[]{"foobar"}), equalTo("foobar")); } @Test(expected = IllegalArgumentException.class) - public void appendArrayShouldThrowIllegalArgumentExceptionWhenValueIsNull() throws Exception { + public void appendArray_ShouldThrowIllegalArgumentExceptionWhenValueIsNull() throws Exception { appendArray(null, new String[]{}); } @Test - public void shouldFindCharacterAtIndex() throws Exception { + public void at_shouldFindCharacterAtIndex() throws Exception { assertThat(at("foobar", 0), equalTo(Optional.of("f"))); assertThat(at("foobar", 1), equalTo(Optional.of("o"))); assertThat(at("foobar", -1), equalTo(Optional.of("r"))); @@ -46,21 +47,38 @@ public class StrmanTest { } @Test - public void shouldReturnArrayWithStringsBetweenStartAndEnd() throws Exception { + public void between_shouldReturnArrayWithStringsBetweenStartAndEnd() throws Exception { assertThat(between("[abc][def]", "[", "]"), arrayContaining("abc", "def")); assertThat(between("foo", "", ""), arrayContaining("foo")); assertThat(between("foobar", "", ""), arrayContaining("foo", "bar")); } @Test - public void betweenShouldReturnFullStringWhenStartAndEndDoesNotExist() throws Exception { + public void between_shouldReturnFullStringWhenStartAndEndDoesNotExist() throws Exception { assertThat(between("[abc][def]", "{", "}"), arrayContaining("[abc][def]")); assertThat(between("", "{", "}"), arrayContaining("")); } @Test - public void charsShouldReturnAllCharactersInString() throws Exception { + public void chars_shouldReturnAllCharactersInString() throws Exception { final String title = "title"; assertThat(chars(title), equalTo(new String[]{"t", "i", "t", "l", "e"})); } + + @Test + public void collapseWhitespace_shouldReplaceConsecutiveWhitespaceWithSingleSpace() throws Exception { + String[] fixture = { + "foo bar", + " foo bar ", + " foo bar ", + " foo bar " + }; + Arrays.stream(fixture).forEach(el -> assertThat(collapseWhitespace(el), equalTo("foo bar"))); + } + + @Test + public void collapseWhitespace_shouldReplaceConsecutiveWhitespaceBetweenMultipleStrings() throws Exception { + String input = " foo bar bazz hello world "; + assertThat(collapseWhitespace(input), equalTo("foo bar bazz hello world")); + } } \ No newline at end of file