Thursday, September 12, 2013

use of isAssignableFrom

Instead of
 @Override
 public boolean equals(Object obj)
 {
  if (this == obj) return true;
  if (obj == null) return false;
  if (getClass() != obj.getClass()) return false;
  final Location other = (Location) obj;
  if (getId() != other.getId()) return false;
  return true;
 }
use
 @Override
 public boolean equals(Object obj)
 {
  if (this == obj) return true;
  if (obj == null) return false;
  if (! getClass().isAssignableFrom( obj.getClass() ) ) return false;
  final Location other = (Location) obj;
  if (getId() != other.getId()) return false;
  return true;
 }
Ref: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#isAssignableFrom(java.lang.Class)

Long vs long

If you are using Long for variable that cannot be null, its best to use the type long instead of Long That way you do not need to check for null Only use wrapper classes when you need to support the concept of undefined