Added containsNull, containsEmpty, ContainsBlank methods. Added tests.

This commit is contained in:
Christian P. MOMON 2016-09-12 18:39:54 +02:00
parent bd66e9c732
commit 3740f95409
3 changed files with 1167 additions and 842 deletions

View file

@ -44,10 +44,181 @@ public class StringListUtils
/**
*
*/
private StringListUtils()
{
}
/**
* Builds a string concatenating many times a source string.
*
* @param source
* The string to concatenate several time.
*
* @param multiplier
* The number of concatenate to produce.
* @return
*/
public static String concatenate(final String source, final int multiplier)
{
String result;
result = repeat(source, multiplier).toString();
//
return result;
}
/**
*
* @param source
* @return
*/
public static boolean containsBlank(final Collection<String> source)
{
boolean result;
if (source == null)
{
result = false;
}
else if (source instanceof StringList)
{
result = ((StringList) source).containsBlank();
}
else
{
result = new StringList(source).containsBlank();
}
//
return result;
}
/**
*
* @param source
* @return
*/
public static boolean containsBlank(final String... source)
{
boolean result;
if (source == null)
{
result = false;
}
else
{
result = new StringList(source).containsBlank();
}
//
return result;
}
/**
*
* @param source
* @return
*/
public static boolean containsEmpty(final Collection<String> source)
{
boolean result;
if (source == null)
{
result = false;
}
else if (source instanceof StringList)
{
result = ((StringList) source).containsEmpty();
}
else
{
result = new StringList(source).containsEmpty();
}
//
return result;
}
/**
*
* @param source
* @return
*/
public static boolean containsEmpty(final String... source)
{
boolean result;
if (source == null)
{
result = false;
}
else
{
result = new StringList(source).containsEmpty();
}
//
return result;
}
/**
*
* @param source
* @return
*/
public static boolean containsNull(final Collection<String> source)
{
boolean result;
if (source == null)
{
result = false;
}
else if (source instanceof StringList)
{
result = ((StringList) source).containsNull();
}
else
{
result = new StringList(source).containsNull();
}
//
return result;
}
/**
*
* @param source
* @return
*/
public static boolean containsNull(final String... source)
{
boolean result;
if (source == null)
{
result = false;
}
else
{
result = new StringList(source).containsNull();
}
//
return result;
}
/**
*
* @param target
* @return
*/
public StringSet getComplement(final Collection<String> alpha, final Collection<String> bravo)
public static StringSet getComplement(final Collection<String> alpha, final Collection<String> bravo)
{
StringSet result;
@ -62,7 +233,7 @@ public class StringListUtils
* @param target
* @return
*/
public StringSet getDifference(final Collection<String> alpha, final Collection<String> bravo)
public static StringSet getDifference(final Collection<String> alpha, final Collection<String> bravo)
{
StringSet result;
@ -78,7 +249,7 @@ public class StringListUtils
* @param target
* @return
*/
public StringSet getDisjunction(final Collection<String> alpha, final Collection<String> bravo)
public static StringSet getDisjunction(final Collection<String> alpha, final Collection<String> bravo)
{
StringSet result;
@ -109,7 +280,7 @@ public class StringListUtils
* @param target
* @return
*/
public StringSet getIntersectionOf(final Collection<String> alpha, final Collection<String> bravo)
public static StringSet getIntersectionOf(final Collection<String> alpha, final Collection<String> bravo)
{
StringSet result;
@ -132,7 +303,7 @@ public class StringListUtils
* @param target
* @return
*/
public StringSet getUnionOf(final Collection<String> alpha, final Collection<String> bravo)
public static StringSet getUnionOf(final Collection<String> alpha, final Collection<String> bravo)
{
StringSet result;
@ -147,26 +318,6 @@ public class StringListUtils
return result;
}
/**
* Builds a string concatenating many times a source string.
*
* @param source
* The string to concatenate several time.
*
* @param multiplier
* The number of concatenate to produce.
* @return
*/
public static String concatenate(final String source, final int multiplier)
{
String result;
result = repeat(source, multiplier).toString();
//
return result;
}
/**
*
* @param file

View file

@ -16,7 +16,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.util;
package fr.devinsy.util.strings;
import java.util.Iterator;

View file

@ -0,0 +1,174 @@
/**
* Copyright (C) 2013,2014 Christian Pierre MOMON
*
* This file is part of Devinsy-utils.
*
* Devinsy-utils 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-utils 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-utils. 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));
}
}