RFR: 8324718: Add a static function to java.util.Objects to simplify object equality checks [v6]

David Alayachew duke at openjdk.org
Sat Jan 27 16:43:34 UTC 2024


On Sat, 27 Jan 2024 07:52:56 GMT, David Alayachew <duke at openjdk.org> wrote:

>> Adding a function to Objects in order to facilitate equality checking and enhance readability. You simply specify the 2 objects that you want to check for equality, and then provide the functions which will be used to provide the values that we will check for equality.
>
> David Alayachew has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Rather than reiterating the precondition, let's explain why the method failed

> It can be used for general classes. See an example:
> 
> ```java
> public class Stuff {
>   public int a, b;
>   private static final MethodHandle EQUALS;
> 
>   static {
>     var lookup = MethodHandles.lookup();
>     try {
>       EQUALS = ObjectMethods.bootstrap(lookup, "equals", MethodHandle.class, "",
>           lookup.findGetter(Stuff.class, "a", int.class),
>           lookup.findGetter(Stuff.class, "b", int.class));
>     } catch (ReflectiveOperationException ex) {
>       throw new ExceptionInInitializerError(ex);
>     }
>   }
> 
>   public boolean equals(Object o) {
>     return EQUALS.invokeExact(this, o);
>   }
> }
> ```

@liach, Ok, I concede that point.

Regardless, my original intention was to have something that could be used ad-hoc, along the lines of `Comparator`. In your example, it would be quite difficult to provide the necessary parameters ad hoc.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/17603#issuecomment-1913252584


More information about the core-libs-dev mailing list