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

java.util.regex.Pattern needs equals() and hashCode() implementations

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 7
    • core-libs

      A DESCRIPTION OF THE REQUEST :
      Pattern inherits hashCode() and equals() from Object which makes it useless for all except identity comparisons.

      Pattern's only unique "state" is its pattern String so hashCode()/equals() using only pattern() as data seems reasonable.

      JUSTIFICATION :
      classes containing Pattern[] and Collection<Pattern> have to unpackage and perform pat.pattern().hashCode()/pat.pattern().equals(otherpat.pattern()) when implementing their own hashCode() and equals() methods. this is because hashCode() of all Collection<Pattern> subclasses use the hashCode() of the elements to generate its own hashCode value as does Arrays.hashCode(Pattern[]).

      if Pattern implemented hashCode() at least one could use Arrays.hashCode(patArray) and Arrays.hashCode(col.toArray()) (for non-order-agnostic comparisons - sorting would have to be added to make such comparisons order-agnostic).


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      i would like to be able to compare and hash collections and arrays of Pattern without having to constantly extract its pattern() value.
      ACTUAL -
      Pattern.equals() only returns true for (this == other); ditto for hashCode(). this is no better than == alone (which could be useful if Pattern.compile() cached its results so that Pattern.compile("foo") == Pattern.compile("foo"), but that's a different matter).

      ---------- BEGIN SOURCE ----------

          public void testPattern() {
              Pattern pat1 = Pattern.compile("foo"), pat2 = Pattern.compile("foo");
              assertTrue("hashCodes don't equal for equivalent objects", pat1.hashCode() == pat2.hashCode());
              assertTrue("equivalent objects aren't equal", pat1.equals(pat2));
              assertTrue("sets of equivalent objects aren't equal", Sets.newHashSet(pat1).hashCode() == Sets.newHashSet(pat2).hashCode());
              assertEquals("sets of equivalent objects don't have identical hashCodes", Sets.newHashSet(pat1), Sets.newHashSet(pat2));
              assertTrue("arrays of equivalent objects aren't equal", Arrays.equals(new Pattern[]{pat1},new Pattern[]{pat2}));
              assertEquals("arrays of equivalent objects don't have identical hashCodes", Arrays.hashCode(new Pattern[]{pat1}),Arrays.hashCode(new Pattern[]{pat2}));
          }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      I currently use a utility class (HashEqualsHelper) to centralize the unpackaging of pattern() from every Pattern when calculating hashCode and equals to ensure it's consistent system-wide. but for Collection<Pattern> implemented as:
      public static int<T> hashCode(Collection<Pattern> col)
      i'm stuck so all calling code must unpackage their collections into Pattern[] to call
      public static int hashCode(Pattern[] array)
      to compare pattern() to pattern().

            sherman Xueming Shen
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: