You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by "John J. Allison" <jo...@joss.ucar.edu> on 2003/06/26 22:00:32 UTC

Velocity Numerics : TemplateNumber

I too like the TemplateNumber deal.
And the < > etc for Comparable's is a good idea and
could be done fast.
String coercion to Number might be neat but it's not
a big issue for me, and I agree it could wait even
if it were a big issue.

Back to TemplateNumber.
For ASTEQNode/ASTNENode (==/!=) should the test for the
same class come before the test for instanceof TemplateNumber ?
Presumably equals() is cheaper than 2 getAsNumber() followed
by 2 convertToBigDecimal() and finally a compareTo().
It also bypasses conversions when comparing two Integer's, Double's,
whatever. It just needs to be explicitly documented to
TemplateNumber implementors what's happening.

        if (left.getClass().equals( right.getClass() ) )
        {
            return left.equals( right );
        }

        /*
         *  convert to Number if applicable
         */
         if (left instanceof TemplateNumber)
         	left = ( (TemplateNumber) left).getAsNumber();
         if (right instanceof TemplateNumber)
         	right = ( (TemplateNumber) right).getAsNumber();

         if (left instanceof Number && right instanceof Number) {
         ...
         }

         /* error code ... */
         rsvc.error(...);
         return false;

John Allison
john@joss.ucar.edu

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


Re: Velocity Numerics : TemplateNumber

Posted by Peter Romianowski <an...@gmx.de>.
Nathan Bubna wrote:

> Peter Romianowski said:
> ..
> 
>>Try new Float(1.0).equals(new Integer(1)). (Hint: The result is 'false').
> 
> 
> right, and since it returned false, you move on to class checking and
> conversions.  i'm thinking you didn't read my post carefully.

Sorry. You're right, I apologize. It's really late here. Then it makes sence.

Peter


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


Re: Velocity Numerics : TemplateNumber

Posted by Nathan Bubna <na...@esha.com>.
Peter Romianowski said:
..
> Try new Float(1.0).equals(new Integer(1)). (Hint: The result is 'false').

right, and since it returned false, you move on to class checking and
conversions.  i'm thinking you didn't read my post carefully.

> I think .equals should be the last test. Numbers first, then Comparable and
> then .equals (which only applies  to == and != anyway).

highly inefficient for the case where equals() returns true.  (and that
applies to both == and !=).

Nathan Bubna
nathan@esha.com


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


Re: Velocity Numerics : TemplateNumber

Posted by Peter Romianowski <an...@gmx.de>.
Nathan Bubna wrote:
> John J. Allison
> ...
> 
>>        if (left.getClass().equals( right.getClass() ) )
>>        {
>>            return left.equals( right );
>>        }
> 
> 
> shouldn't calling equals() itself be done before *anything* else?  is there

Try new Float(1.0).equals(new Integer(1)). (Hint: The result is 'false').
I think .equals should be the last test. Numbers first, then Comparable and
then .equals (which only applies  to == and != anyway).

Peter

> any real case where that would true, yet we'd want to return a different
> result?
> 
> e.g.:
> 
>     if (left.equals(right))
>     {
>         return true;
>     }
>     else
> ...
> 
> it seems to me you only need to look at the class of the objects and/or do
> conversions if equals() is false.
> 
> Nathan Bubna
> nathan@esha.com
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
> 
> 


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


Re: Velocity Numerics : TemplateNumber

Posted by Nathan Bubna <na...@esha.com>.
John J. Allison
...
>         if (left.getClass().equals( right.getClass() ) )
>         {
>             return left.equals( right );
>         }

shouldn't calling equals() itself be done before *anything* else?  is there
any real case where that would true, yet we'd want to return a different
result?

e.g.:

    if (left.equals(right))
    {
        return true;
    }
    else
...

it seems to me you only need to look at the class of the objects and/or do
conversions if equals() is false.

Nathan Bubna
nathan@esha.com


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