commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject [beanutils] Question about BeanComparator
Date Thu, 26 Sep 2002 21:24:12 GMT

This question is mostly focused for Henri, but anyone can comment.  I am
working on a testcase for BeanComparator, and one that comes up is if I try
and compare two Boolean objects.  Because Boolean doesn't support
Comparable, they crash.  I change BeanComparator to have a makeComparable

    private Comparable makeComparable( Object o )
      throws ClassCastException {
      Comparable value = null;
      if ( o == null ) {
        value = new String( "" );
      else if ( o instanceof Boolean ) {
        Boolean b = (Boolean) o;
        if ( b.booleanValue() ) {
          value = new Integer( "1" );
        else {
          value = new Integer( "0" );
      else if ( o instanceof Comparable ) {
        value = (Comparable) o;
      else {
        throw new ClassCastException( "Object " + o.getClass().getName() + "
is not a comparable datatype, or convertable." );
      return value;

Now, I know that this means that as various datatypes are introduced, then
this needs to be changed.  What I am wondering though is that being able to
compare booleans is pretty basic.  However, I was also thinking that I would
create SafeComparator that would be smart enough to make nulls into blank
strings, booleans into 1 and 0 etc...?  Heck, if you got crazy, it could be
pluggable similar to the transformer stuff so that any datatype could be
made a comparable object.  What are your opinions?  I at least want to get
my first cut of unit tests into CVS.

Eric Pugh

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message