changeset 49763:1ccf5fae9664

8184692: add Pattern.asMatchPredicate Reviewed-by: psandoz, rriggs
author vtheeyarath
date Mon, 16 Apr 2018 11:21:32 -0700
parents b07d069b189a
children 906712e6afbf e39a356eed2c
files src/java.base/share/classes/java/util/regex/Pattern.java test/jdk/java/util/regex/RegExTest.java
diffstat 2 files changed, 44 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/util/regex/Pattern.java	Tue Apr 17 15:25:13 2018 +0200
+++ b/src/java.base/share/classes/java/util/regex/Pattern.java	Mon Apr 16 11:21:32 2018 -0700
@@ -5821,15 +5821,35 @@
      * }</pre>
      *
      * @return  The predicate which can be used for finding a match on a
-     * subsequence of a string
+     *          subsequence of a string
      * @since   1.8
-     * @see Matcher#find
+     * @see     Matcher#find
      */
     public Predicate<String> asPredicate() {
         return s -> matcher(s).find();
     }
 
     /**
+     * Creates a predicate that tests if this pattern matches a given input string.
+     *
+     * @apiNote
+     * This method creates a predicate that behaves as if it creates a matcher
+     * from the input sequence and then calls {@code matches}, for example a
+     * predicate of the form:
+     * <pre>{@code
+     *   s -> matcher(s).matches();
+     * }</pre>
+     *
+     * @return  The predicate which can be used for matching an input string
+     *          against this pattern.
+     * @since   11
+     * @see     Matcher#matches
+     */
+    public Predicate<String> asMatchPredicate() {
+        return s -> matcher(s).matches();
+    }
+
+    /**
      * Creates a stream from the given input sequence around matches of this
      * pattern.
      *
--- a/test/jdk/java/util/regex/RegExTest.java	Tue Apr 17 15:25:13 2018 +0200
+++ b/test/jdk/java/util/regex/RegExTest.java	Mon Apr 16 11:21:32 2018 -0700
@@ -35,7 +35,7 @@
  * 8027645 8035076 8039124 8035975 8074678 6854417 8143854 8147531 7071819
  * 8151481 4867170 7080302 6728861 6995635 6736245 4916384 6328855 6192895
  * 6345469 6988218 6693451 7006761 8140212 8143282 8158482 8176029 8184706
- * 8194667 8197462
+ * 8194667 8197462 8184692
  *
  * @library /test/lib
  * @build jdk.test.lib.RandomFactory
@@ -164,6 +164,7 @@
         groupCurlyNotFoundSuppTest();
         groupCurlyBackoffTest();
         patternAsPredicate();
+        patternAsMatchPredicate();
         invalidFlags();
         embeddedFlags();
         grapheme();
@@ -4689,6 +4690,26 @@
         report("Pattern.asPredicate");
     }
 
+    // This test is for 8184692
+    private static void patternAsMatchPredicate() throws Exception {
+        Predicate<String> p = Pattern.compile("[a-z]+").asMatchPredicate();
+
+        if (p.test("")) {
+            failCount++;
+        }
+        if (!p.test("word")) {
+            failCount++;
+        }
+        if (p.test("1234word")) {
+            failCount++;
+        }
+        if (p.test("1234")) {
+            failCount++;
+        }
+        report("Pattern.asMatchPredicate");
+    }
+
+
     // This test is for 8035975
     private static void invalidFlags() throws Exception {
         for (int flag = 1; flag != 0; flag <<= 1) {