diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index 0ccb337..55a58e7 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -31,10 +31,9 @@ import java.util.function.Predicate; import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.IntStream; import java.util.stream.Stream; -import static java.lang.Math.min; -import static java.util.Collections.emptyList; import static java.util.function.Function.identity; import static java.util.stream.Collectors.*; @@ -99,10 +98,6 @@ public abstract class Strman { return (index < length && index >= 0) ? Optional.of(String.valueOf(value.charAt(index))) : Optional.empty(); } - private static boolean isNullOrEmpty(String input) { - return input == null || input.isEmpty(); - } - /** * Returns an array with strings between start and end. * @@ -1316,6 +1311,28 @@ public abstract class Strman { return builder.toString().toLowerCase(); } + /** + * Aggregates the contents of n strings into a single list of tuples. + * + * @param inputs A list of strings. + * @return A list of strings if none of the strings in the input is null or empty. + * An empty list otherwise. + */ + public static List zip(String... inputs) { + if (inputs.length == 0) { + return Collections.emptyList(); + } + OptionalInt min = Arrays.stream(inputs).mapToInt(str -> str == null ? 0 : str.length()).min(); + if (!min.isPresent()) { + return Collections.emptyList(); + } + return IntStream.range(0, min.getAsInt()) + .mapToObj(elementIndex -> Arrays.stream(inputs) + .map(input -> String.valueOf(input.charAt(elementIndex))) + .collect(joining())) + .collect(toList()); + } + private static void validate(String value, Predicate predicate, final Supplier supplier) { if (predicate.test(value)) { throw new IllegalArgumentException(supplier.get()); @@ -1336,57 +1353,8 @@ public abstract class Strman { return countSubstr(value.substring(offset), subStr, allowOverlapping, ++count); } - /** - * Aggregates the contents of n strings into a single list of tuples. - * - * @param inputs A list of strings. - * @return A list of strings if none of the strings in the input is null or empty. - * An empty list otherwise. - */ - public static List zip(String... inputs) { - if (inputs.length == 1) { - return inputs[0].codePoints() - .mapToObj(Character::toChars) - .map(String::new) - .collect(toList()); - } - - int minLength = calculateMinLength(inputs); - - if (minLength == -1) { - return emptyList(); - } - - List zipped = new ArrayList<>(minLength); - - for (int elementIndex = 0; elementIndex < minLength; ++elementIndex) { - StringBuilder tuple = new StringBuilder(); - - for (int inputIndex = 0; inputIndex < inputs.length; ++inputIndex) { - tuple.append(inputs[inputIndex].charAt(elementIndex)); - } - - zipped.add(tuple.toString()); - } - - return zipped; - } - - private static int calculateMinLength(String[] inputs) { - if (inputs.length == 0) { - return -1; - } - - int minLength = Integer.MAX_VALUE; - - for (String input : inputs) { - if (isNullOrEmpty(input)) { - return -1; - } - minLength = min(minLength, input.length()); - } - - return minLength; + private static boolean isNullOrEmpty(String input) { + return input == null || input.isEmpty(); } } diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTests.java similarity index 99% rename from src/test/java/strman/StrmanTest.java rename to src/test/java/strman/StrmanTests.java index 8082b19..66e37ce 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTests.java @@ -26,8 +26,6 @@ package strman; -import org.hamcrest.collection.IsCollectionWithSize; -import org.hamcrest.collection.IsEmptyCollection; import org.junit.Ignore; import org.junit.Test; @@ -43,7 +41,7 @@ import static org.junit.Assert.*; import static strman.Strman.*; import static strman.Strman.endsWith; -public class StrmanTest { +public class StrmanTests { @Test public void append_shouldAppendStringsToEndOfValue() throws Exception {