Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Dynamic Comparison with Primitive Data Types

Friends,

We are writing a framework for a validation...

We do have a config file like below...

<root>
<property name="Premium">
    <xmlTag>//Message/Request/Product/Benefit/Premium/amount</xmlTag>
    <valueType>float</valueType>
    <validation condition=">" value="0">Premium Amount cannot be less than Zero.</validation>
</property>

I get the XML Value using XPath and convert it to float by <valueType> element value...

No, I do have value="0" also been converted to float.

Now, I do have to apply the condition which has been specified as condition=">".

I don't want to do this on IF ELSEIF....ELSE loop.

Is there any other way to convert "<" in to an operator < or use compare operator on a String?

In this way, my code will be simple and useful for future more operators.

=============================================================================

Thanks all for the suggestions and answers...

I have decided to use the BeanShell's bsh.Interpreter. It does the work for me...

sample code for you all...

        System.out.println(new bsh.Interpreter().eval("1 < 0"));
        System.out.println(new bsh.Interpreter().eval("1 > 0"));
        System.out.println(new bsh.Interpreter().eval("1 >= 0"));
        System.out.println(new bsh.Interpreter().eval("0 >= 0"));
        System.out.println(new bsh.Interpreter().eval("1 != 0"));
        System.out.println(new bsh.Interpreter().eval("0 != 0"));
        System.out.println(new bsh.Interpreter().eval("1 == 0"));
        System.out.println(new bsh.Interpreter().eval("0 == 0"));

returned me true/false.

Thanks & Good luck...

like image 976
SANSSAN Avatar asked Apr 18 '12 11:04

SANSSAN


2 Answers

You can use a switch statement

char operator = ...;
switch(operator) {
   case '<': return value1 < value2;
   case '=': return value1 == value2;
}
like image 159
Angelo Fuchs Avatar answered Nov 03 '22 18:11

Angelo Fuchs


I would recommend using an expression language such as Java EL or even better Apache Commons Jexl, since it is much easier to integrate. Here is a code sample taken from JEXL website:

    // Assuming we have a JexlEngine instance initialized in our class named 'jexl':
    // Create an expression object for our calculation
    String calculateTax = "((G1 + G2 + G3) * 0.1) + G4";
    Expression e = jexl.createExpression( calculateTax );

    // populate the context
    JexlContext context = new MapContext();
    context.set("G1", businessObject.getTotalSales());
    context.set("G2", taxManager.getTaxCredit(businessObject.getYear()));
    context.set("G3", businessObject.getIntercompanyPayments());
    context.set("G4", -taxManager.getAllowances());
    // ...

    // work it out
    Float result = (Float)e.evaluate(context);

In your particular example you could change your validation XML to something like:

<property name="Premium">
    <xmlTag>//Message/Request/Product/Benefit/Premium/amount</xmlTag>
    <valueType>float</valueType>
    <validation expression="Premium> 0">Premium Amount cannot be less than Zero.</validation>
</property>

and then build up your own JEXL context:

JexlContext context = new MapContext();
context.set("PREMIUM", <Premium value fetched from XML>);

In my opinion this is the most scalable solution as it allows you to build complex validation expressions in just one line of code.

like image 25
Funky coder Avatar answered Nov 03 '22 18:11

Funky coder