From 5eca5814defc9726167fb563ccdcdbc7170c56ea Mon Sep 17 00:00:00 2001 From: Shekhar Gulati Date: Sat, 17 Jun 2017 03:17:05 +0530 Subject: [PATCH] Resolved #83 --- src/main/java/strman/Strman.java | 16 +++++++++++++++- src/test/java/strman/StrmanTests.java | 26 +++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index 55a58e7..79fe417 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -42,8 +42,9 @@ import static java.util.stream.Collectors.*; */ public abstract class Strman { - private static final Predicate NULL_STRING_PREDICATE = str -> str == null; + private static final Predicate NULL_STRING_PREDICATE = Objects::isNull; private static final Supplier NULL_STRING_MSG_SUPPLIER = () -> "'value' should be not null."; + private static final String[] EMPTY_ARRAY = new String[0]; private Strman() { } @@ -1333,6 +1334,19 @@ public abstract class Strman { .collect(toList()); } + /** + * Split lines to an array + * + * @param input The input String + * @return lines in an array + */ + public static String[] lines(String input) { + if (input == null) { + return EMPTY_ARRAY; + } + return input.split("\r\n?|\n"); + } + private 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/StrmanTests.java b/src/test/java/strman/StrmanTests.java index 66e37ce..5d8da52 100644 --- a/src/test/java/strman/StrmanTests.java +++ b/src/test/java/strman/StrmanTests.java @@ -32,9 +32,12 @@ import org.junit.Test; import java.util.*; import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; import static java.util.stream.Collectors.toList; import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.collection.IsArrayContaining.hasItemInArray; import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining; +import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize; import static org.hamcrest.collection.IsArrayWithSize.emptyArray; import static org.hamcrest.collection.IsEmptyCollection.empty; import static org.junit.Assert.*; @@ -1114,6 +1117,27 @@ public class StrmanTests { @Test public void zip_shouldReturnInputInListForm_whenOnlyOneInput() { assertThat(zip("zip"), is(equalTo(asList("z", "i", "p")))); - assertThat(zip("z"), is(equalTo(asList("z")))); + assertThat(zip("z"), is(equalTo(singletonList("z")))); + } + + @Test + public void lines_shouldReturnEmptyArrayWhenInputIsNull() throws Exception { + assertThat(lines(null), emptyArray()); + } + + @Test + public void lines_shouldReturnArrayWithOneEmptyElementWhenInputIsEmptyString() throws Exception { + assertThat(lines(""), arrayWithSize(1)); + assertThat(lines(""), hasItemInArray("")); + } + + @Test + public void lines_shouldSplitToLines() throws Exception { + assertThat(lines("Hello\r\nWorld").length, equalTo(2)); + assertThat(lines("Hello\rWorld").length, equalTo(2)); + assertThat(lines("Hello World").length, equalTo(1)); + assertThat(lines("\r\n\n\r ").length, equalTo(4)); + assertThat(lines("Hello\r\r\nWorld").length, equalTo(3)); + assertThat(lines("Hello\r\rWorld").length, equalTo(3)); } }