You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Julius Davies <ju...@gmail.com> on 2008/05/27 22:35:26 UTC

[lang] NullSafe (a lightweight EqualsBuilder)

Hi,

I'd like to introduce a class called NullSafe.java, and another called
Primitive.java to commons-lang.  They would provide the following
static methods:

NullSafe.equals(Object o1, Object o2)
NullSafe.compare(Object o1, Object o2)
NullSafe.compare(Object o1, Object o2, Comparator c)

Primitive.compare(boolean b1, boolean b2)
Primitive.compare(long l1, long l2)
[etc....]


Here are two examples of how one would use NullSafe and Primitive:


public boolean equals(Object other) {
  if (o instanceof MyClass) {
    MyClass o = (MyClass) other;
    return this == o
      || NullSafe.equals(dateTime, o.dateTime)
      && NullSafe.equals(name, o.name)
      && NullSafe.equals(amount, o.amount)
      && age == o.age
      && someFlag == o.someFlag;
  } else {
    return false;
  }
}


public int compareTo(Object other) {
  if (this == other) {
    return 0;
  }

  MyClass o = (MyClass) other;
  int c = NullSafe.compare(dateTime, o.dateTime);
  if (c == 0) {
    c = NullSafe.compare(name, o.name);
    if (c == 0) {
      c = NullSafe.compare(amount, o.amount)
      if (c == 0) {
        c = Primitive.compare(age, o.age);
        if (c == 0) {
          c = Primitive.compare(someFlag, o.someFlag);
        }
      }
    }
  }
  return c;
}




It certainly has some overlap with EqualsBuilder and friends.  But the
performance is probably better, the code is just as easy to read, and
the amount of typing is the same with equals() and only slightly more
with compare().

This might help regarding LANG-340 - "performance problem with
EqualsBuilder.append()".


-- 
yours,

Julius Davies
250-592-2284 (Home)
250-893-4579 (Mobile)
http://juliusdavies.ca/

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [lang] NullSafe (a lightweight EqualsBuilder)

Posted by Emmanuel Bourg <eb...@apache.org>.
I think you can achieve the same result with ComparatorUtils from 
Commons Collections:

Comparator comp = ComparatorUtils.nullHighComparator(comparator);
comp.compare(o1, o2);

But a similar method in ObjectUtils would be useful.

Emmanuel Bourg



Julius Davies a écrit :
> ObjectUtils.equals() is just what I'm looking for.  Thanks!
> 
> Is there any interest in an ObjectUtils.compare() method?  I would be
> happy to create a JIRA ticket and upload a patch.
> 
> yours,
> 
> Julius
> 
> 
> On Tue, May 27, 2008 at 3:05 PM, Stephen Colebourne
> <sc...@btopenworld.com> wrote:
>> Julius Davies wrote:
>>> NullSafe.equals(Object o1, Object o2)
>>> NullSafe.compare(Object o1, Object o2)
>>> NullSafe.compare(Object o1, Object o2, Comparator c)
>> I think these are on ObjectUtils already.
>>
>> Stephen
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>> For additional commands, e-mail: dev-help@commons.apache.org
>>
>>
> 
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [lang] NullSafe (a lightweight EqualsBuilder)

Posted by Julius Davies <ju...@gmail.com>.
ObjectUtils.equals() is just what I'm looking for.  Thanks!

Is there any interest in an ObjectUtils.compare() method?  I would be
happy to create a JIRA ticket and upload a patch.

yours,

Julius


On Tue, May 27, 2008 at 3:05 PM, Stephen Colebourne
<sc...@btopenworld.com> wrote:
> Julius Davies wrote:
>>
>> NullSafe.equals(Object o1, Object o2)
>> NullSafe.compare(Object o1, Object o2)
>> NullSafe.compare(Object o1, Object o2, Comparator c)
>
> I think these are on ObjectUtils already.
>
> Stephen
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>



-- 
yours,

Julius Davies
250-592-2284 (Home)
250-893-4579 (Mobile)
http://juliusdavies.ca/

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [lang] NullSafe (a lightweight EqualsBuilder)

Posted by Stephen Colebourne <sc...@btopenworld.com>.
Julius Davies wrote:
> NullSafe.equals(Object o1, Object o2)
> NullSafe.compare(Object o1, Object o2)
> NullSafe.compare(Object o1, Object o2, Comparator c)
I think these are on ObjectUtils already.

Stephen

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org