Added isMatching strings by regexps, and tests.
This commit is contained in:
parent
05c1b543fe
commit
4aca746ee0
2 changed files with 270 additions and 0 deletions
|
@ -871,6 +871,88 @@ public class StringsUtils
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if is matching.
|
||||
*
|
||||
* @param args
|
||||
* the args
|
||||
* @param regexps
|
||||
* the regexps
|
||||
* @return true, if is matching
|
||||
*/
|
||||
public static boolean isMatching(final String[] args, final String... regexps)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
result = isMatching(new StringList(args), regexps);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if is matching.
|
||||
*
|
||||
* @param args
|
||||
* the args
|
||||
* @param regexps
|
||||
* the regexps
|
||||
* @return true, if is matching
|
||||
*/
|
||||
public static boolean isMatching(final StringList args, final String... regexps)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (((args == null) || (args.size() == 0)) && ((regexps == null) || (regexps.length == 0)))
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
else if (((args == null) || (args.size() == 0)) && ((regexps != null) && (regexps.length != 0)))
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else if (((args != null) && (args.size() != 0)) && ((regexps == null) || (regexps.length == 0)))
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else if (args.size() != regexps.length)
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
boolean ended = false;
|
||||
int index = 0;
|
||||
result = false;
|
||||
while (!ended)
|
||||
{
|
||||
if (index < args.size())
|
||||
{
|
||||
String arg = args.get(index);
|
||||
String regexp = regexps[index];
|
||||
|
||||
if (arg.matches(regexp))
|
||||
{
|
||||
index += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
ended = true;
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ended = true;
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load.
|
||||
*
|
||||
|
|
|
@ -445,6 +445,193 @@ public class StringsUtilsTest
|
|||
Assert.assertTrue(StringsUtils.containsNull(source));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is matching 01.
|
||||
*
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
@Test
|
||||
public void testIsMatching01() throws IOException
|
||||
{
|
||||
//
|
||||
StringList source = null;
|
||||
String[] regexps = null;
|
||||
|
||||
//
|
||||
Assert.assertEquals(true, StringsUtils.isMatching(source, regexps));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is matching 02.
|
||||
*
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
@Test
|
||||
public void testIsMatching02() throws IOException
|
||||
{
|
||||
//
|
||||
StringList source = new StringList();
|
||||
String[] regexps = null;
|
||||
|
||||
//
|
||||
Assert.assertEquals(true, StringsUtils.isMatching(source, regexps));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is matching 03.
|
||||
*
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
@Test
|
||||
public void testIsMatching03() throws IOException
|
||||
{
|
||||
//
|
||||
StringList source = null;
|
||||
String[] regexps = new String[0];
|
||||
|
||||
//
|
||||
Assert.assertEquals(true, StringsUtils.isMatching(source, regexps));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is matching 04.
|
||||
*
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
@Test
|
||||
public void testIsMatching04() throws IOException
|
||||
{
|
||||
//
|
||||
StringList source = new StringList();
|
||||
String[] regexps = new String[0];
|
||||
|
||||
//
|
||||
Assert.assertEquals(true, StringsUtils.isMatching(source, regexps));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is matching 05.
|
||||
*
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
@Test
|
||||
public void testIsMatching05() throws IOException
|
||||
{
|
||||
//
|
||||
StringList source = new StringList("aaa", "bbb", "ccc");
|
||||
String[] regexps = null;
|
||||
|
||||
//
|
||||
Assert.assertEquals(false, StringsUtils.isMatching(source, regexps));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is matching 06.
|
||||
*
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
@Test
|
||||
public void testIsMatching06() throws IOException
|
||||
{
|
||||
//
|
||||
StringList source = new StringList("aaa", "bbb", "ccc");
|
||||
String[] regexps = new String[0];
|
||||
|
||||
//
|
||||
Assert.assertEquals(false, StringsUtils.isMatching(source, regexps));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is matching 07.
|
||||
*
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
@Test
|
||||
public void testIsMatching07() throws IOException
|
||||
{
|
||||
//
|
||||
StringList source = new StringList("aaa", "bbb", "ccc");
|
||||
String[] regexps = new String[] { "^a+$" };
|
||||
|
||||
//
|
||||
Assert.assertEquals(false, StringsUtils.isMatching(source, regexps));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is matching 08.
|
||||
*
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
@Test
|
||||
public void testIsMatching08() throws IOException
|
||||
{
|
||||
//
|
||||
StringList source = new StringList("aaa", "bbb", "ccc");
|
||||
String[] regexps = new String[] { "^a+$", "^b+$", "^c+$" };
|
||||
|
||||
//
|
||||
Assert.assertEquals(true, StringsUtils.isMatching(source, regexps));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is matching 09.
|
||||
*
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
@Test
|
||||
public void testIsMatching09() throws IOException
|
||||
{
|
||||
//
|
||||
StringList source = null;
|
||||
String[] regexps = new String[] { "^a+$", "^b+$", "^c+$" };
|
||||
|
||||
//
|
||||
Assert.assertEquals(false, StringsUtils.isMatching(source, regexps));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is matching 10.
|
||||
*
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
@Test
|
||||
public void testIsMatching10() throws IOException
|
||||
{
|
||||
//
|
||||
StringList source = new StringList();
|
||||
String[] regexps = new String[] { "^a+$", "^b+$", "^c+$" };
|
||||
|
||||
//
|
||||
Assert.assertEquals(false, StringsUtils.isMatching(source, regexps));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is matching 11.
|
||||
*
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
@Test
|
||||
public void testIsMatching11() throws IOException
|
||||
{
|
||||
//
|
||||
StringList source = new StringList("aaa");
|
||||
String[] regexps = new String[] { "^a+$", "^b+$", "^c+$" };
|
||||
|
||||
//
|
||||
Assert.assertEquals(false, StringsUtils.isMatching(source, regexps));
|
||||
}
|
||||
|
||||
/**
|
||||
* Load to string list URL 01.
|
||||
*
|
||||
|
@ -461,4 +648,5 @@ public class StringsUtilsTest
|
|||
Assert.assertEquals(4, source.size());
|
||||
Assert.assertEquals("trois", source.get(3 - 1));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue