From 0845ab52e8d2aeec6afa01b17c867b3fe866cdc3 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Thu, 4 May 2017 14:32:00 +0200 Subject: [PATCH] Renamed StringListUtils to StringsUtils. Added findLongest, findShortest, findShortestLength, findLongestLength and capitalize methods. --- ...tringListUtils.java => StringstUtils.java} | 158 +++++++++++++- .../util/strings/StringListUtilsTest.java | 174 ---------------- .../util/strings/StringsUtilsTest.java | 192 ++++++++++++++++++ 3 files changed, 348 insertions(+), 176 deletions(-) rename src/fr/devinsy/util/strings/{StringListUtils.java => StringstUtils.java} (87%) delete mode 100644 test/fr/devinsy/util/strings/StringListUtilsTest.java create mode 100644 test/fr/devinsy/util/strings/StringsUtilsTest.java diff --git a/src/fr/devinsy/util/strings/StringListUtils.java b/src/fr/devinsy/util/strings/StringstUtils.java similarity index 87% rename from src/fr/devinsy/util/strings/StringListUtils.java rename to src/fr/devinsy/util/strings/StringstUtils.java index 01e8cba..06fafac 100644 --- a/src/fr/devinsy/util/strings/StringListUtils.java +++ b/src/fr/devinsy/util/strings/StringstUtils.java @@ -34,23 +34,59 @@ import java.io.UnsupportedEncodingException; import java.net.URL; import java.util.Collection; +import org.apache.commons.lang3.StringUtils; + /** * The {@code StringUtils} class defines helper methods to string collection. * * Operations that are null safe. * */ -public class StringListUtils +public class StringstUtils { public static final String DEFAULT_CHARSET_NAME = "UTF-8"; /** * */ - private StringListUtils() + private StringstUtils() { } + /** + * Capitalize. + * + * @param source + * the source + */ + public static void capitalize(final StringList source) + { + if (source != null) + { + for (int index = 0; index < source.size(); index++) + { + source.set(index, StringUtils.capitalize(source.get(index))); + } + } + } + + /** + * Capitalize. + * + * @param source + * the source + */ + public static void capitalize(final StringSet source) + { + if (source != null) + { + StringList values = new StringList(source); + capitalize(values); + source.clear(); + source.addAll(values); + } + } + /** * Builds a string concatenating many times a source string. * @@ -215,6 +251,124 @@ public class StringListUtils return result; } + /** + * Find longest. + * + * @param source + * the source + * @return the string + */ + public static String findLongest(final Collection source) + { + String result; + + if ((source == null) || (source.isEmpty())) + { + result = null; + } + else + { + int value = 0; + result = null; + + for (String string : source) + { + if ((string != null) && (string.length() > value)) + { + value = string.length(); + result = string; + } + } + } + + // + return result; + } + + /** + * Find longest length. + * + * @param source + * the source + * @return the int + */ + public static int findLongestLength(final Collection source) + { + int result; + + String string = findLongest(source); + if (string == null) + { + result = 0; + } + else + { + result = string.length(); + } + + // + return result; + } + + /** + * Find smallest. + * + * @param source + * the source + * @return the string + */ + public static String findShortest(final Collection source) + { + String result; + + if ((source == null) || (source.isEmpty())) + { + result = null; + } + else + { + int value = Integer.MAX_VALUE; + result = null; + + for (String string : source) + { + if ((string != null) && (string.length() < value)) + { + value = string.length(); + result = string; + } + } + } + + // + return result; + } + + /** + * Find smallest length. + * + * @param source + * the source + * @return the int + */ + public static int findShortestLength(final Collection source) + { + int result; + + String string = findShortest(source); + if (string == null) + { + result = 0; + } + else + { + result = string.length(); + } + + // + return result; + } + /** * * @param target diff --git a/test/fr/devinsy/util/strings/StringListUtilsTest.java b/test/fr/devinsy/util/strings/StringListUtilsTest.java deleted file mode 100644 index e4be85b..0000000 --- a/test/fr/devinsy/util/strings/StringListUtilsTest.java +++ /dev/null @@ -1,174 +0,0 @@ -/** - * Copyright (C) 2013,2014,2017 Christian Pierre MOMON - * - * This file is part of devinsy-strings. - * - * devinsy-strings is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * devinsy-strings is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with devinsy-strings. If not, see - */ -package fr.devinsy.util.strings; - -import java.util.ArrayList; -import java.util.Collection; - -import org.apache.log4j.BasicConfigurator; -import org.apache.log4j.Level; -import org.apache.log4j.Logger; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -/** - * - * @author Christian P. Momon - */ -public class StringListUtilsTest -{ - private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(StringListUtilsTest.class); - - /** - * - */ - @Before - public void before() - { - BasicConfigurator.configure(); - Logger.getRootLogger().setLevel(Level.ERROR); - } - - /** - * - */ - @Test - public void testContainsBlank01() - { - Assert.assertFalse(StringListUtils.containsBlank((Collection) null)); - Assert.assertFalse(StringListUtils.containsBlank(new StringList("aaa", "bbb", "ccc"))); - Assert.assertTrue(StringListUtils.containsBlank(new StringList("aaa", null, "ccc"))); - Assert.assertTrue(StringListUtils.containsBlank(new StringList("aaa", "", "ccc"))); - Assert.assertTrue(StringListUtils.containsBlank(new StringList("aaa", " ", "ccc"))); - } - - /** - * - */ - @Test - public void testContainsBlank02() - { - Assert.assertFalse(StringListUtils.containsBlank((String[]) null)); - Assert.assertFalse(StringListUtils.containsBlank("aaa", "bbb", "ccc")); - Assert.assertTrue(StringListUtils.containsBlank("aaa", null, "ccc")); - Assert.assertTrue(StringListUtils.containsBlank("aaa", "", "ccc")); - Assert.assertTrue(StringListUtils.containsBlank("aaa", " ", "ccc")); - } - - /** - * - */ - @Test - public void testContainsBlank03() - { - Assert.assertFalse(StringListUtils.containsBlank((ArrayList) null)); - ArrayList source = new ArrayList(); - source.add("aaa"); - source.add("bbb"); - source.add("ccc"); - Assert.assertFalse(StringListUtils.containsBlank(source)); - source.set(1, null); - Assert.assertTrue(StringListUtils.containsBlank(source)); - source.set(1, ""); - Assert.assertTrue(StringListUtils.containsBlank(source)); - source.set(1, " "); - Assert.assertTrue(StringListUtils.containsBlank(source)); - } - - /** - * - */ - @Test - public void testContainsEmpty01() - { - Assert.assertFalse(StringListUtils.containsEmpty((Collection) null)); - Assert.assertFalse(StringListUtils.containsEmpty(new StringList("aaa", "bbb", "ccc"))); - Assert.assertTrue(StringListUtils.containsEmpty(new StringList("aaa", null, "ccc"))); - Assert.assertTrue(StringListUtils.containsEmpty(new StringList("aaa", "", "ccc"))); - } - - /** - * - */ - @Test - public void testContainsEmpty02() - { - Assert.assertFalse(StringListUtils.containsEmpty((String[]) null)); - Assert.assertFalse(StringListUtils.containsEmpty("aaa", "bbb", "ccc")); - Assert.assertTrue(StringListUtils.containsEmpty("aaa", null, "ccc")); - Assert.assertTrue(StringListUtils.containsEmpty("aaa", "", "ccc")); - } - - /** - * - */ - @Test - public void testContainsEmpty03() - { - Assert.assertFalse(StringListUtils.containsEmpty((ArrayList) null)); - ArrayList source = new ArrayList(); - source.add("aaa"); - source.add("bbb"); - source.add("ccc"); - Assert.assertFalse(StringListUtils.containsEmpty(source)); - source.set(1, null); - Assert.assertTrue(StringListUtils.containsEmpty(source)); - source.set(1, ""); - Assert.assertTrue(StringListUtils.containsEmpty(source)); - } - - /** - * - */ - @Test - public void testContainsNull01() - { - Assert.assertFalse(StringListUtils.containsNull((Collection) null)); - Assert.assertFalse(StringListUtils.containsNull(new StringList("aaa", "bbb", "ccc"))); - Assert.assertTrue(StringListUtils.containsNull(new StringList("aaa", null, "ccc"))); - } - - /** - * - */ - @Test - public void testContainsNull02() - { - Assert.assertFalse(StringListUtils.containsNull((String[]) null)); - Assert.assertFalse(StringListUtils.containsNull("aaa", "bbb", "ccc")); - Assert.assertTrue(StringListUtils.containsNull("aaa", null, "ccc")); - } - - /** - * - */ - @Test - public void testContainsNull03() - { - Assert.assertFalse(StringListUtils.containsNull((ArrayList) null)); - ArrayList source = new ArrayList(); - source.add("aaa"); - source.add("bbb"); - source.add("ccc"); - Assert.assertFalse(StringListUtils.containsNull(source)); - source.set(1, null); - Assert.assertTrue(StringListUtils.containsNull(source)); - } -} diff --git a/test/fr/devinsy/util/strings/StringsUtilsTest.java b/test/fr/devinsy/util/strings/StringsUtilsTest.java new file mode 100644 index 0000000..041ce4e --- /dev/null +++ b/test/fr/devinsy/util/strings/StringsUtilsTest.java @@ -0,0 +1,192 @@ +/** + * Copyright (C) 2013,2014,2017 Christian Pierre MOMON + * + * This file is part of devinsy-strings. + * + * devinsy-strings is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * devinsy-strings is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with devinsy-strings. If not, see + */ +package fr.devinsy.util.strings; + +import java.util.ArrayList; +import java.util.Collection; + +import org.apache.log4j.BasicConfigurator; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * + * @author Christian P. Momon + */ +public class StringsUtilsTest +{ + private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(StringsUtilsTest.class); + + /** + * + */ + @Before + public void before() + { + BasicConfigurator.configure(); + Logger.getRootLogger().setLevel(Level.ERROR); + } + + /** + * + */ + @Test + public void testContainsBlank01() + { + Assert.assertFalse(StringstUtils.containsBlank((Collection) null)); + Assert.assertFalse(StringstUtils.containsBlank(new StringList("aaa", "bbb", "ccc"))); + Assert.assertTrue(StringstUtils.containsBlank(new StringList("aaa", null, "ccc"))); + Assert.assertTrue(StringstUtils.containsBlank(new StringList("aaa", "", "ccc"))); + Assert.assertTrue(StringstUtils.containsBlank(new StringList("aaa", " ", "ccc"))); + } + + /** + * + */ + @Test + public void testContainsBlank02() + { + Assert.assertFalse(StringstUtils.containsBlank((String[]) null)); + Assert.assertFalse(StringstUtils.containsBlank("aaa", "bbb", "ccc")); + Assert.assertTrue(StringstUtils.containsBlank("aaa", null, "ccc")); + Assert.assertTrue(StringstUtils.containsBlank("aaa", "", "ccc")); + Assert.assertTrue(StringstUtils.containsBlank("aaa", " ", "ccc")); + } + + /** + * + */ + @Test + public void testContainsBlank03() + { + Assert.assertFalse(StringstUtils.containsBlank((ArrayList) null)); + ArrayList source = new ArrayList(); + source.add("aaa"); + source.add("bbb"); + source.add("ccc"); + Assert.assertFalse(StringstUtils.containsBlank(source)); + source.set(1, null); + Assert.assertTrue(StringstUtils.containsBlank(source)); + source.set(1, ""); + Assert.assertTrue(StringstUtils.containsBlank(source)); + source.set(1, " "); + Assert.assertTrue(StringstUtils.containsBlank(source)); + } + + /** + * + */ + @Test + public void testContainsEmpty01() + { + Assert.assertFalse(StringstUtils.containsEmpty((Collection) null)); + Assert.assertFalse(StringstUtils.containsEmpty(new StringList("aaa", "bbb", "ccc"))); + Assert.assertTrue(StringstUtils.containsEmpty(new StringList("aaa", null, "ccc"))); + Assert.assertTrue(StringstUtils.containsEmpty(new StringList("aaa", "", "ccc"))); + } + + /** + * + */ + @Test + public void testContainsEmpty02() + { + Assert.assertFalse(StringstUtils.containsEmpty((String[]) null)); + Assert.assertFalse(StringstUtils.containsEmpty("aaa", "bbb", "ccc")); + Assert.assertTrue(StringstUtils.containsEmpty("aaa", null, "ccc")); + Assert.assertTrue(StringstUtils.containsEmpty("aaa", "", "ccc")); + } + + /** + * + */ + @Test + public void testContainsEmpty03() + { + Assert.assertFalse(StringstUtils.containsEmpty((ArrayList) null)); + ArrayList source = new ArrayList(); + source.add("aaa"); + source.add("bbb"); + source.add("ccc"); + Assert.assertFalse(StringstUtils.containsEmpty(source)); + source.set(1, null); + Assert.assertTrue(StringstUtils.containsEmpty(source)); + source.set(1, ""); + Assert.assertTrue(StringstUtils.containsEmpty(source)); + } + + /** + * + */ + @Test + public void testContainsNull01() + { + Assert.assertFalse(StringstUtils.containsNull((Collection) null)); + Assert.assertFalse(StringstUtils.containsNull(new StringList("aaa", "bbb", "ccc"))); + Assert.assertTrue(StringstUtils.containsNull(new StringList("aaa", null, "ccc"))); + } + + /** + * + */ + @Test + public void testContainsNull02() + { + Assert.assertFalse(StringstUtils.containsNull((String[]) null)); + Assert.assertFalse(StringstUtils.containsNull("aaa", "bbb", "ccc")); + Assert.assertTrue(StringstUtils.containsNull("aaa", null, "ccc")); + } + + /** + * + */ + @Test + public void testContainsNull03() + { + Assert.assertFalse(StringstUtils.containsNull((ArrayList) null)); + ArrayList source = new ArrayList(); + source.add("aaa"); + source.add("bbb"); + source.add("ccc"); + Assert.assertFalse(StringstUtils.containsNull(source)); + source.set(1, null); + Assert.assertTrue(StringstUtils.containsNull(source)); + } + + /** + * + */ + @Test + public void testFindSmallest01() + { + // TODO + Assert.assertFalse(StringstUtils.containsNull((ArrayList) null)); + ArrayList source = new ArrayList(); + source.add("aaa"); + source.add("bbb"); + source.add("ccc"); + Assert.assertFalse(StringstUtils.containsNull(source)); + source.set(1, null); + Assert.assertTrue(StringstUtils.containsNull(source)); + } + +}