From 4aca746ee0d6e8fffd7603126610a40f2d3cff18 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Tue, 29 Sep 2020 18:18:25 +0200 Subject: [PATCH] Added isMatching strings by regexps, and tests. --- src/fr/devinsy/strings/StringsUtils.java | 82 ++++++++ test/fr/devinsy/strings/StringsUtilsTest.java | 188 ++++++++++++++++++ 2 files changed, 270 insertions(+) diff --git a/src/fr/devinsy/strings/StringsUtils.java b/src/fr/devinsy/strings/StringsUtils.java index 9bc5a9d..f752f58 100644 --- a/src/fr/devinsy/strings/StringsUtils.java +++ b/src/fr/devinsy/strings/StringsUtils.java @@ -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. * diff --git a/test/fr/devinsy/strings/StringsUtilsTest.java b/test/fr/devinsy/strings/StringsUtilsTest.java index 5f5e1e5..b306a95 100644 --- a/test/fr/devinsy/strings/StringsUtilsTest.java +++ b/test/fr/devinsy/strings/StringsUtilsTest.java @@ -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)); } + }