java逻辑表达式计算与判断[通俗易懂]

java逻辑表达式计算与判断[通俗易懂]packagecom.cyb.regex;importjava.text.MessageFormat;importjava.util.ArrayList;importjava.util.List;importjava.util.Locale;importjavax.script.ScriptEngine;importjavax.script.ScriptEngine…

大家好,欢迎来到IT知识分享网。

package com.cyb.regex;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cyb.collection.utils.RandomUtils;
import com.cyb.context.TimeContext;

import sun.tools.tree.Expression;

/**
 * 作者 : iechenyb<br>
 * 类描述: 说点啥<br>
 * 创建时间: 2018年9月11日
 */
public class 逻辑表达式 {
	Log log = LogFactory.getLog(逻辑表达式.class);

	public static void test() {
		MessageFormat mf = new MessageFormat("aaa{0}{0}", Locale.US);
		Object arr[] = { "he" };
		System.out.println(mf.format(arr));

		String booleanString = "{0}>1 && {0}<=5";
		System.out.println(Boolean.valueOf(MessageFormat.format(booleanString, 6)));
		System.out.println(Boolean.valueOf(MessageFormat.format(booleanString, 2)));

		String expression = "value>1 && value<=5";
		Boolean rs = assertExpressTrue("2", expression);
		for (int i = 1; i < 8; i++) {
			rs = assertExpressTrue(i + "", expression);
			System.out.println(i + ",rs=" + rs);
		}
	}
    static List<String> expressions = new ArrayList<String>();
    static{
    	//expressions.add("1<value<5");//必须拆分 不合法
    	//expressions.add("value>10% && value<60%");//异常
    	//expressions.add("value>0.01 && value<0.65");
    	//expressions.add("value>1 && value<=5");
    	//expressions.add("value>1 || value<=5");
    	//expressions.add("(value>1 && value<=5)||value > 10");
    }
    
    static List<String> arithmetics = new ArrayList<String>();
    static{
    	arithmetics.add("10 * 2 + 6 / (3 - 1)");
    	arithmetics.add("1/100+10");
    	arithmetics.add("2*3+5*2/100");
    }
    
	public static void main(String[] args) throws InterruptedException {
		TimeContext.recordTimeStart();
		test();
		int i=0;
		while(i<10){
			i++;
			for(String ex:expressions){
				String param = String.valueOf(RandomUtils.getNum(1, 100)*1.0/100);
				boolean res = assertExpressTrue(String.
						valueOf(param),
						ex);
				System.out.println("表达式:"+ex+"\t参数:"+param+"\t"+res);
			}
		}
		for(String ex:arithmetics){
			String result = calStringExpression(ex);
			System.out.println("表达式:"+ex+"\t结果:"+result);
		}
		TimeContext.calExecuteTime();
	}

	/**
	 * 
	 * @author: Longjun
	 * @Description: 将${money>=2000&&money<=4000}字符串截取成
	 *               "money>=2000&&money<=4000", 然后判断一个数值字符串是否在此区间内
	 * @date:2016年3月21日 上午11:25:32
	 */
	public static Boolean assertExpressTrue(String value, String expression) {
		String el = expression;
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("js");
		engine.put("value", value);
		boolean eval = false;
		try {
			eval = (boolean) engine.eval(el);
		} catch (ScriptException e) {
			e.printStackTrace();
		}
		return eval;
	}

	public static String calStringExpression(String expression) {
		ScriptEngineManager scriptEngineManager 
		= new ScriptEngineManager();
		ScriptEngine scriptEngine 
		= scriptEngineManager.getEngineByName("js");//nashorn
		try {
			String result = String.valueOf(scriptEngine.eval(expression));
			return result;
		} catch (ScriptException e) {
			e.printStackTrace();
			return "";
		}
	}
}

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/25170.html

(0)
上一篇 2023-08-01 19:00
下一篇 2023-08-02 21:33

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信