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

Wednesday, March 13, 2013

Making ant targets driven by a property

The ant apache manual clearly defines on the the use of the "if" property when there's a need to have certain ant targets run only for some criteria.

 "A target also has the ability to perform its execution if (or unless) a property has been set. This allows, for example, better control on the building process depending on the state of the system (java version, OS, command-line property defines, etc.). To make a target sense this property, you should add the if (or unless) attribute with the name of the property that the target should react to. Note: In the most simple case Ant will only check whether the property has been set, the value doesn't matter, but using property expansions you can build more complex conditions. See the properties page for more details. For example:


Expanding on this need, here's how you pass the property on the command line so its picked up by the build script
$ant clean build -Dmodule-A-present=anyvalue

Ref: http://ant.apache.org/manual/targets.html