简体   繁体   中英

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...

You can use a switch statement

char operator = ...;
switch(operator) {
   case '<': return value1 < value2;
   case '=': return value1 == value2;
}

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.

Wrap the primitive value into the corresponding wrapper:

Float f = new Float(floatValue)

Then you can use the provided compareTo() method polymorphically.

EDIT: You can also have a look at full-featured implementations for expression parsing; besides others that have already been mentioned here, I would add Spring EL .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM