Commit 0845ab52 authored by Christian P. MOMON's avatar Christian P. MOMON
Browse files

Renamed StringListUtils to StringsUtils. Added findLongest,

findShortest, findShortestLength, findLongestLength and capitalize
methods.
parent 442c29a6
......@@ -34,21 +34,57 @@ 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);
}
}
/**
......@@ -215,6 +251,124 @@ public class StringListUtils
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
......
......@@ -32,9 +32,9 @@ import org.junit.Test;
*
* @author Christian P. Momon
*/
public class StringListUtilsTest
public class StringsUtilsTest
{
private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(StringListUtilsTest.class);
private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(StringsUtilsTest.class);
/**
*
......@@ -52,11 +52,11 @@ public class StringListUtilsTest
@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")));
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")));
}
/**
......@@ -65,11 +65,11 @@ public class StringListUtilsTest
@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"));
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"));
}
/**
......@@ -78,18 +78,18 @@ public class StringListUtilsTest
@Test
public void testContainsBlank03()
{
Assert.assertFalse(StringListUtils.containsBlank((ArrayList<String>) null));
Assert.assertFalse(StringstUtils.containsBlank((ArrayList<String>) null));
ArrayList<String> source = new ArrayList<String>();
source.add("aaa");
source.add("bbb");
source.add("ccc");
Assert.assertFalse(StringListUtils.containsBlank(source));
Assert.assertFalse(StringstUtils.containsBlank(source));
source.set(1, null);
Assert.assertTrue(StringListUtils.containsBlank(source));
Assert.assertTrue(StringstUtils.containsBlank(source));
source.set(1, "");
Assert.assertTrue(StringListUtils.containsBlank(source));
Assert.assertTrue(StringstUtils.containsBlank(source));
source.set(1, " ");
Assert.assertTrue(StringListUtils.containsBlank(source));
Assert.assertTrue(StringstUtils.containsBlank(source));
}
/**
......@@ -98,10 +98,10 @@ public class StringListUtilsTest
@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")));
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")));
}
/**
......@@ -110,10 +110,10 @@ public class StringListUtilsTest
@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"));
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"));
}
/**
......@@ -122,16 +122,16 @@ public class StringListUtilsTest
@Test
public void testContainsEmpty03()
{
Assert.assertFalse(StringListUtils.containsEmpty((ArrayList<String>) null));
Assert.assertFalse(StringstUtils.containsEmpty((ArrayList<String>) null));
ArrayList<String> source = new ArrayList<String>();
source.add("aaa");
source.add("bbb");
source.add("ccc");
Assert.assertFalse(StringListUtils.containsEmpty(source));
Assert.assertFalse(StringstUtils.containsEmpty(source));
source.set(1, null);
Assert.assertTrue(StringListUtils.containsEmpty(source));
Assert.assertTrue(StringstUtils.containsEmpty(source));
source.set(1, "");
Assert.assertTrue(StringListUtils.containsEmpty(source));
Assert.assertTrue(StringstUtils.containsEmpty(source));
}
/**
......@@ -140,9 +140,9 @@ public class StringListUtilsTest
@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")));
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")));
}
/**
......@@ -151,9 +151,9 @@ public class StringListUtilsTest
@Test
public void testContainsNull02()
{
Assert.assertFalse(StringListUtils.containsNull((String[]) null));
Assert.assertFalse(StringListUtils.containsNull("aaa", "bbb", "ccc"));
Assert.assertTrue(StringListUtils.containsNull("aaa", null, "ccc"));
Assert.assertFalse(StringstUtils.containsNull((String[]) null));
Assert.assertFalse(StringstUtils.containsNull("aaa", "bbb", "ccc"));
Assert.assertTrue(StringstUtils.containsNull("aaa", null, "ccc"));
}
/**
......@@ -162,13 +162,31 @@ public class StringListUtilsTest
@Test
public void testContainsNull03()
{
Assert.assertFalse(StringListUtils.containsNull((ArrayList<String>) null));
Assert.assertFalse(StringstUtils.containsNull((ArrayList<String>) null));
ArrayList<String> source = new ArrayList<String>();
source.add("aaa");
source.add("bbb");
source.add("ccc");
Assert.assertFalse(StringListUtils.containsNull(source));
Assert.assertFalse(StringstUtils.containsNull(source));
source.set(1, null);
Assert.assertTrue(StringListUtils.containsNull(source));
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));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment