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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to