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

Pattern.asPredicate specification is incomplete

XMLWordPrintable

    • b09
    • Not verified

      A DESCRIPTION OF THE REQUEST :
      The javadoc of Pattern#asPredicate states that it:

      "[...]Creates a predicate which can be used to match a string."

      But it is very confusing. Only by digging into the source code can it be found that in fact what this method does is create a Predicate<String> as:

          s -> matcher(s).find()

      Which in fact does "real" pattern matching (that is, the pattern can be found anywhere within the string).

      Whereas both Pattern's and String's .matches() methods will only return true if the whole input matches the given pattern.

      This is highly confusing. If anything I'd suggest that the javadoc for .asPredicate() refers to Matcher#find.

      JUSTIFICATION :
      It is damaging enough that Java's .matches() methods don't do real pattern matching. This lack of documentation only fosters more confusion.


      CUSTOMER SUBMITTED WORKAROUND :
      Document what Pattern#asPredicate does accurately, for instance, as suggested, document that it in fact uses Matcher#find.

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

              Created:
              Updated:
              Resolved: