Java - dynamic comparison with primitive data types

Friends,

We are writing a framework for checking ...

We have a configuration file as shown 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 using the value of the <valueType> element ...

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

Now I need to apply the condition that was specified as condition=">" .

I do not want to do this on IF ELSEIF .... ELSE.

Is there any other way to convert "<" to a < operator, or use the comparison operator on a String?

Thus, my code will be simple and useful for future operators.

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

Thanks to everyone for the suggestions and answers ...

I decided to use BeanShell bsh.Interpreter. It does the job for me ...

sample code for all of you ...

  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.

Thank you and good luck...

+6
source share
3 answers

You can use the switch statement

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

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 sample code taken from the 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 specific example, you can change your validation XML document 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 create 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, because it allows you to create complex validation expressions in only one line of code.

+2
source

Wrap the primitive value in the appropriate wrapper:

 Float f = new Float(floatValue) 

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

EDIT: You can also take a look at full-featured implementations for parsing expressions; besides the others already mentioned here, I would add Spring EL .

0
source

Source: https://habr.com/ru/post/913514/


All Articles