Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8200603

Pattern.asPredicate specification is incomplete

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 11
    • core-libs
    • None
    • behavioral
    • minimal
    • This is a clarification of behaviour, so there is no compatibility risk
    • Java API
    • SE

      Summary

      The proposed change is to explicitly state that Pattern#asPredicate uses s -> matcher(s).find()

      Problem

      The javadoc of Pattern#asPredicate states that it: "[...]Creates a predicate which can be used to match a string."

      The specification for asPredicate isn't sufficiently specific about its behavior. It refers to matching a string, which is misleading because that makes it sound like it's based on Matcher.matches(), Pattern.matches(), or String.matches(). However, it actually uses Matcher.find() in the predicate implementation.

      Solution

      Make the documentation more explicit about the implemented behavior.

      Specification

      src/java.base/share/classes/java/util/regex/Pattern.java

           /**
      -     * Creates a predicate which can be used to match a string.
      +     * Creates a predicate that tests if this pattern is found in a given input
      +     * string.
            *
      -     * @return  The predicate which can be used for matching on a string
      +     * @apiNote
      +     * This method creates a predicate that behaves as if it creates a matcher
      +     * from the input sequence and then calls {@code find}, for example a
      +     * predicate of the form:
      +     *{@code
      +     *   s -> matcher(s).find();
      +     * }
      +     *
      +     * @return  The predicate which can be used for finding a match on a
      +     * subsequence of a string
            * @since   1.8
      +     * @see Matcher#find
            */
      
      

            vtheeyarath Vivek Theeyarath (Inactive)
            webbuggrp Webbug Group
            Paul Sandoz
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: