简体   繁体   English

Java - 与原始数据类型的动态比较

[英]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... 我使用XPath获取XML值并通过<valueType>元素值将其转换为float ...

No, I do have value="0" also been converted to float. 不,我确实有value="0"也被转换为浮动。

Now, I do have to apply the condition which has been specified as condition=">" . 现在,我必须应用已指定为condition=">"

I don't want to do this on IF ELSEIF....ELSE loop. 我不想在IF ELSEIF .... ELSE循环上执行此操作。

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. 我决定使用BeanShell的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 您可以使用switch语句

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. 我建议使用Java EL等表达式语言,甚至更好的Apache Commons Jexl ,因为它更容易集成。 Here is a code sample taken from JEXL website : 以下是从JEXL网站获取的代码示例:

    // 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: 在您的特定示例中,您可以将验证XML更改为:

<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: 然后构建自己的JEXL上下文:

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. 然后,您可以多态地使用提供的compareTo()方法。

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 . 除了这里已经提到过的其他内容之外,我还会添加Spring EL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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