From f62a6a30ffc7779fc396995bc138744adcbd469e Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Thu, 1 Dec 2022 15:21:00 +0100 Subject: [PATCH] Implement RegexSet to validate a single user-id against multiple wildcards --- .../org/pgpainless/algorithm/RegexSet.java | 40 +++++++++++++++ .../pgpainless/algorithm/RegexSetTest.java | 51 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 pgpainless-core/src/main/java/org/pgpainless/algorithm/RegexSet.java create mode 100644 pgpainless-core/src/test/java/org/pgpainless/algorithm/RegexSetTest.java diff --git a/pgpainless-core/src/main/java/org/pgpainless/algorithm/RegexSet.java b/pgpainless-core/src/main/java/org/pgpainless/algorithm/RegexSet.java new file mode 100644 index 00000000..e4639e03 --- /dev/null +++ b/pgpainless-core/src/main/java/org/pgpainless/algorithm/RegexSet.java @@ -0,0 +1,40 @@ +// SPDX-FileCopyrightText: 2022 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package org.pgpainless.algorithm; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +public final class RegexSet { + + private final Set regexSet = new HashSet<>(); + + private RegexSet(Collection regexes) { + this.regexSet.addAll(regexes); + } + + public static RegexSet matchAnything() { + return new RegexSet(Collections.singleton(Regex.wildcard())); + } + + public static RegexSet matchNothing() { + return new RegexSet(Collections.emptySet()); + } + + public static RegexSet matchSome(Collection regexes) { + return new RegexSet(regexes); + } + + public boolean matches(String userId) { + for (Regex regex : regexSet) { + if (regex.matches(userId)) { + return true; + } + } + return false; + } +} diff --git a/pgpainless-core/src/test/java/org/pgpainless/algorithm/RegexSetTest.java b/pgpainless-core/src/test/java/org/pgpainless/algorithm/RegexSetTest.java new file mode 100644 index 00000000..ff8b12fe --- /dev/null +++ b/pgpainless-core/src/test/java/org/pgpainless/algorithm/RegexSetTest.java @@ -0,0 +1,51 @@ +// SPDX-FileCopyrightText: 2022 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package org.pgpainless.algorithm; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Arrays; + +import org.junit.jupiter.api.Test; + +public class RegexSetTest { + + @Test + public void matchNothingTest() { + RegexSet set = RegexSet.matchNothing(); + assertFalse(set.matches("")); + assertFalse(set.matches("Alice")); + assertFalse(set.matches("Alice ")); + assertFalse(set.matches("")); + } + + @Test + public void matchAnything() { + RegexSet set = RegexSet.matchAnything(); + assertTrue(set.matches("Alice")); + assertTrue(set.matches("")); + assertTrue(set.matches("Alice ")); + assertTrue(set.matches("Alice ")); + assertTrue(set.matches("")); + } + + @Test + public void matchSome() { + Regex pgpainless_org = RegexInterpreterFactory.createDefaultMailDomainRegex("pgpainless.org"); + Regex example_org = RegexInterpreterFactory.createDefaultMailDomainRegex("example.org"); + + RegexSet set = RegexSet.matchSome(Arrays.asList(pgpainless_org, example_org)); + assertTrue(set.matches("Alice ")); + assertTrue(set.matches("")); + assertTrue(set.matches("Bob ")); + assertTrue(set.matches("")); + assertFalse(set.matches("Bob ")); + assertFalse(set.matches("Alice ")); + assertFalse(set.matches("alice@pgpainless.org")); + assertFalse(set.matches("Alice")); + assertFalse(set.matches("")); + } +}