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

Make java.util.Objects#equals generic

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 7
    • core-libs
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      Currently (openjdk-7-ea-src-b145-07_jun_2011) the java.util.Objects#equals(Object, Object) method is declared as

      public static boolean equals(Object a, Object b) {...}

      It could be changed to:

      public static <T> boolean equals(T a, T b) {...}

      The same applies to java.util.Objects#deepEquals(Object, Object)

      This enhancement is not particularly important, but it is a simple change that would be nice-to-have.

      JUSTIFICATION :
      Imagine the following statement:

      boolean result = java.util.Objects.equals(getUid(), getKey());

      If the return type of getUid() is modified such that it is not compatible with getKey() then no compile error/warning would be emitted (e.g., CharSequence vs Number).

      If the enhancement is made then the preceding statement could be, optionally, written as (assuming current return type of the two methods are type-compatible with Number):

      boolean result = java.util.Objects.<Number>equals(getUid(), getKey());

      Now, if the return type of either method is modified such that it is not compatible with Number then a compile error would be emitted and the code can be reviewed by the programmer.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      If the java.util.Objects#equals(Object, Object) method is called with unexpected parameter types a compile error/warning is emitted.
      ACTUAL -
      If the java.util.Objects#equals(Object, Object) method is called with unexpected parameter types no compile error/warning is emitted.

      ---------- BEGIN SOURCE ----------
      public class TestObjects {

          public static String getUid() {
              return "42";
          }

          public static Long getKey() {
              return Long.valueOf(42);
          }

          public static void main(final String[] args) {
              /* Want following line to be a compile warning or error. */
              boolean result = java.util.Objects.equals(getUid(), getKey());
              System.out.println(result);
          }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The above code can be written as:

          public static void main(final String[] args) {
              Number uid = getUid();
              Number key = getKey();
              boolean result = java.util.Objects.equals(getUid(), getKey());
              System.out.println(result);
          }

      This will cause a compile error if either method return type changes.

            darcy Joe Darcy
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: