Proposal: Sameness operators

Tim Keith tim.keith at gmail.com
Thu Apr 2 16:49:13 PDT 2009


I would like to suggest calling the class java.lang.Objects, similar
to java.util.Collections and java.util.Arrays.

And it would be handy to also have in there a null-safe static method
to help in implementing hashCode:
    public static int hashCode(Object o) {
        return o == null ? 0 : o.hashCode();
    }

-- Tim

On Thu, Apr 2, 2009 at 4:02 PM, Joe Darcy <Joe.Darcy at sun.com> wrote:
> Reinier Zwitserloot wrote:
>
> [snip]
>
>>
>> public static void is(Object x, Object y) {
>>      if ( x == null ) return y == null;
>>      return x.equals(y);
>> }
>>
>> public static void is(Object x, int y) {
>>     // Yes, you'd need a few gazillion is methods to cover all the
>> primitives on either side, but 1 static import will grab all of them
>> }
>>
>> ... loads more is methods
>>
>>
>> Then same with lt, gt, le, ge.
>>
>> Then you could write:
>>
>> if ( is(a, b) ) {
>>    //doStuff
>> }
>>
>> Which is a small improvement.
>>
>> Add an ability to specify 'as infix' in static imports, and you have
>> pretty much what we all want without backwards compatibility issues:
>>
>> import static java.lang.Equality.is infix;
>>
>> if ( a is b ) {
>>    //do something
>> }
>>
>> I'd consider that a large improvement.
>
> Yes, in JDK 7 I'd like to see
>
>     6797535 Add shared two argument static equals method to the platform
>     http://bugs.sun.com/view_bug.do?bug_id=6797535
>
> addressed in the platform.  I was thinking putting it in a place like a
> new utility class java.util.Object.
>
> -Joe
>
>



More information about the coin-dev mailing list