Renamed StringListUtils to StringsUtils. Added findLongest,

findShortest, findShortestLength, findLongestLength and capitalize
methods.
This commit is contained in:
Christian P. MOMON 2017-05-04 14:32:00 +02:00
parent 442c29a647
commit 0845ab52e8
3 changed files with 348 additions and 176 deletions

View file

@ -34,23 +34,59 @@ import java.io.UnsupportedEncodingException;
import java.net.URL; import java.net.URL;
import java.util.Collection; import java.util.Collection;
import org.apache.commons.lang3.StringUtils;
/** /**
* The {@code StringUtils} class defines helper methods to string collection. * The {@code StringUtils} class defines helper methods to string collection.
* *
* Operations that are null safe. * Operations that are null safe.
* *
*/ */
public class StringListUtils public class StringstUtils
{ {
public static final String DEFAULT_CHARSET_NAME = "UTF-8"; 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. * Builds a string concatenating many times a source string.
* *
@ -215,6 +251,124 @@ public class StringListUtils
return result; return result;
} }
/**
* Find longest.
*
* @param source
* the source
* @return the string
*/
public static String findLongest(final Collection<String> 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<String> 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<String> 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<String> source)
{
int result;
String string = findShortest(source);
if (string == null)
{
result = 0;
}
else
{
result = string.length();
}
//
return result;
}
/** /**
* *
* @param target * @param target

View file

@ -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 <http://www.gnu.org/licenses/>
*/
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<String>) 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<String>) null));
ArrayList<String> source = new ArrayList<String>();
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<String>) 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<String>) null));
ArrayList<String> source = new ArrayList<String>();
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<String>) 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<String>) null));
ArrayList<String> source = new ArrayList<String>();
source.add("aaa");
source.add("bbb");
source.add("ccc");
Assert.assertFalse(StringListUtils.containsNull(source));
source.set(1, null);
Assert.assertTrue(StringListUtils.containsNull(source));
}
}

View file

@ -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 <http://www.gnu.org/licenses/>
*/
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<String>) 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<String>) null));
ArrayList<String> source = new ArrayList<String>();
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<String>) 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<String>) null));
ArrayList<String> source = new ArrayList<String>();
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<String>) 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<String>) null));
ArrayList<String> source = new ArrayList<String>();
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<String>) null));
ArrayList<String> source = new ArrayList<String>();
source.add("aaa");
source.add("bbb");
source.add("ccc");
Assert.assertFalse(StringstUtils.containsNull(source));
source.set(1, null);
Assert.assertTrue(StringstUtils.containsNull(source));
}
}