From 17c37ba07f6ff848e48af01ba97aea11afb22710 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Thu, 4 May 2017 21:59:40 +0200 Subject: [PATCH] Added methods and tests. --- src/fr/devinsy/util/strings/StringList.java | 23 +++ .../devinsy/util/strings/StringstUtils.java | 155 ++++++++++++++++++ .../devinsy/util/strings/StringListTest.java | 50 ++++++ 3 files changed, 228 insertions(+) diff --git a/src/fr/devinsy/util/strings/StringList.java b/src/fr/devinsy/util/strings/StringList.java index 94f41cc..daae59e 100644 --- a/src/fr/devinsy/util/strings/StringList.java +++ b/src/fr/devinsy/util/strings/StringList.java @@ -1274,6 +1274,29 @@ public class StringList extends ArrayList implements CharSequence return result; } + /** + * Shrink all string of the list to only one string. + * + * @return the string list + */ + public StringList shrink() + { + StringList result; + + if (!isEmpty()) + { + String shrinked = toString(); + clear(); + add(shrinked); + } + + // + result = this; + + // + return result; + } + /** * Sorts this list. * diff --git a/src/fr/devinsy/util/strings/StringstUtils.java b/src/fr/devinsy/util/strings/StringstUtils.java index cffed78..70fea9b 100644 --- a/src/fr/devinsy/util/strings/StringstUtils.java +++ b/src/fr/devinsy/util/strings/StringstUtils.java @@ -31,6 +31,7 @@ import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.URL; import java.util.Collection; +import java.util.Iterator; import org.apache.commons.lang3.StringUtils; @@ -502,6 +503,92 @@ public class StringstUtils return result; } + /** + * Checks if is blank. + * + * @param source + * the source + */ + public static boolean isBlank(final Collection source) + { + boolean result; + + if (source == null) + { + result = true; + } + else + { + Iterator iterator = source.iterator(); + boolean ended = false; + result = false; + while (!ended) + { + if (iterator.hasNext()) + { + String value = iterator.next(); + + if (StringUtils.isNotBlank(value)) + { + ended = true; + result = false; + } + } + else + { + ended = true; + result = true; + } + } + } + + // + return result; + } + + /** + * Checks if is empty. + * + * @param source + * the source + * @return true, if is empty + */ + public static boolean isEmptyFully(final Collection source) + { + boolean result; + + if ((source == null) || (source.isEmpty())) + { + result = true; + } + else + { + Iterator iterator = source.iterator(); + boolean ended = false; + result = false; + while (!ended) + { + if (iterator.hasNext()) + { + String value = iterator.next(); + if (StringUtils.isNotEmpty(value)) + { + ended = true; + result = false; + } + } + else + { + ended = true; + result = true; + } + } + } + + // + return result; + } + /** * Load. * @@ -748,6 +835,40 @@ public class StringstUtils } } + /** + * Swap case. + * + * @param source + * the source + */ + public static void swapCase(final StringList source) + { + if (source != null) + { + for (int index = 0; index < source.size(); index++) + { + source.set(index, StringUtils.swapCase(source.get(index))); + } + } + } + + /** + * Swap case. + * + * @param source + * the source + */ + public static void swapCase(final StringSet source) + { + if (source != null) + { + StringList values = new StringList(source); + swapCase(values); + source.clear(); + source.addAll(values); + } + } + /** * Concatenates the string from an array to a string. * @@ -1338,6 +1459,40 @@ public class StringstUtils return result; } + /** + * Uncapitalize. + * + * @param source + * the source + */ + public static void uncapitalize(final StringList source) + { + if (source != null) + { + for (int index = 0; index < source.size(); index++) + { + source.set(index, StringUtils.uncapitalize(source.get(index))); + } + } + } + + /** + * Uncapitalize. + * + * @param source + * the source + */ + public static void uncapitalize(final StringSet source) + { + if (source != null) + { + StringList values = new StringList(source); + uncapitalize(values); + source.clear(); + source.addAll(values); + } + } + /** * Upper case. * diff --git a/test/fr/devinsy/util/strings/StringListTest.java b/test/fr/devinsy/util/strings/StringListTest.java index 30a1345..82c0c76 100644 --- a/test/fr/devinsy/util/strings/StringListTest.java +++ b/test/fr/devinsy/util/strings/StringListTest.java @@ -245,6 +245,56 @@ public class StringListTest logger.debug("===== test done."); } + /** + * Test shrink 01. + */ + @Test + public void testShrink01() + { + // + logger.debug("===== test starting..."); + + // + StringList source = new StringList(); + + Assert.assertEquals(0, source.size()); + + source.shrink(); + + Assert.assertEquals(0, source.size()); + + // + logger.debug("===== test done."); + } + + /** + * Test shrink 01. + */ + @Test + public void testShrink02() + { + // + logger.debug("===== test starting..."); + + // + StringList source = new StringList(); + + source.append("alpha"); + source.append("bravo"); + source.append("charlie"); + source.append("delta"); + + Assert.assertEquals(4, source.size()); + + source.shrink(); + + Assert.assertEquals(1, source.size()); + Assert.assertEquals("alphabravocharliedelta", source.get(0)); + + // + logger.debug("===== test done."); + } + /** * Test substring 01. */