|
4 | 4 | import java.util.Collection; |
5 | 5 | import java.util.Map; |
6 | 6 | import java.util.function.Supplier; |
| 7 | +import java.util.regex.Pattern; |
7 | 8 | import org.intellij.lang.annotations.RegExp; |
8 | 9 | import org.jetbrains.annotations.Contract; |
9 | 10 |
|
@@ -1671,6 +1672,62 @@ public static String matches( |
1671 | 1672 | return EnsureStringOps.matches(string, regex, exceptionSupplier); |
1672 | 1673 | } |
1673 | 1674 |
|
| 1675 | + /** |
| 1676 | + * Ensures that the provided string matches the specified pattern. |
| 1677 | + * |
| 1678 | + * @param string the string to check |
| 1679 | + * @param pattern the pattern to match against |
| 1680 | + * @return the provided string if it matches the pattern |
| 1681 | + * @throws EnsureException if the string is {@code null} or does not match the pattern, with the |
| 1682 | + * message {@code "string must match pattern %s"} |
| 1683 | + * @see #matches(String, Pattern, String) |
| 1684 | + * @see #matches(String, Pattern, Supplier) |
| 1685 | + */ |
| 1686 | + @Contract("null, _ -> fail; !null, _ -> param1") |
| 1687 | + public static String matches(String string, Pattern pattern) { |
| 1688 | + return Ensure.matches( |
| 1689 | + string, |
| 1690 | + pattern, |
| 1691 | + EnsureStringOps.STRING_MUST_MATCH_REGEX_FORMAT.formatted( |
| 1692 | + pattern == null ? "null" : pattern.pattern())); |
| 1693 | + } |
| 1694 | + |
| 1695 | + /** |
| 1696 | + * Ensures that the provided string matches the specified pattern. |
| 1697 | + * |
| 1698 | + * @param string the string to check |
| 1699 | + * @param pattern the pattern to match against |
| 1700 | + * @param exceptionMessage the message to include in the exception if validation fails |
| 1701 | + * @return the provided string if it matches the pattern |
| 1702 | + * @throws EnsureException if the string is {@code null} or does not match the pattern, with the |
| 1703 | + * provided message |
| 1704 | + * @see #matches(String, Pattern) |
| 1705 | + * @see #matches(String, Pattern, Supplier) |
| 1706 | + */ |
| 1707 | + @Contract("null, _, _ -> fail; !null, _, _ -> param1") |
| 1708 | + public static String matches(String string, Pattern pattern, String exceptionMessage) { |
| 1709 | + return Ensure.matches(string, pattern, () -> EnsureException.from(exceptionMessage)); |
| 1710 | + } |
| 1711 | + |
| 1712 | + /** |
| 1713 | + * Ensures that the provided string matches the specified pattern. |
| 1714 | + * |
| 1715 | + * @param string the string to check |
| 1716 | + * @param pattern the pattern to match against |
| 1717 | + * @param exceptionSupplier the supplier that provides the exception to be thrown if validation |
| 1718 | + * fails |
| 1719 | + * @return the provided string if it matches the pattern |
| 1720 | + * @throws RuntimeException if the string is {@code null} or does not match the pattern; the |
| 1721 | + * thrown exception is provided by {@code exceptionSupplier} |
| 1722 | + * @see #matches(String, Pattern) |
| 1723 | + * @see #matches(String, Pattern, String) |
| 1724 | + */ |
| 1725 | + @Contract("null, _, _ -> fail; !null, _, _ -> param1") |
| 1726 | + public static String matches( |
| 1727 | + String string, Pattern pattern, Supplier<? extends RuntimeException> exceptionSupplier) { |
| 1728 | + return EnsurePatternOps.matches(string, pattern, exceptionSupplier); |
| 1729 | + } |
| 1730 | + |
1674 | 1731 | /** |
1675 | 1732 | * Ensures that the provided string matches the alphanumeric pattern (including spaces). |
1676 | 1733 | * |
|
0 commit comments