java将字符串逻辑表达式转成布尔值
開發背景:
我們在開發過程中,可能需要將某數據動態從配置文件中讀取。且需要根據用戶的輸入,來動態判斷決定某布爾值。
例如:
我們有一個配置,稱為配置一,需要在配置文件中寫好,該配置有3個子配置(姑且這么叫吧),3個子配置是否選擇,需要根據用戶的頁面輸入。
我們又有一個配置,稱為配置二,需要在配置文件中寫好,該配置有2個子配置,2個子配置是否選擇,需要根據配置二中的其他配置項來決定是否需要選擇。
(可能以上例子表述并不完整,但并不影響本文的繼續進行)
有時候,我們需要在配置文件中添加一個字段,通過該字段來確認一個動態的布爾值,這個字段是一個邏輯的表達式。(例:a==b &&?student.id == stusdent .num)
但是,配置文件拿到的只能是字符串。因此需要解析字符串,返回布爾值。
輸入(參數):
除了要輸入字符串外,還需要輸入字符串中需要用到的數據集。
例如student.id == stusdent .num 這個表達式,就需要student對象
輸出(返回值):
布爾值:true 或者 false
代碼:
package com.dacheng.kyc.com.dacheng.kyc;import java.util.HashMap; import java.util.Map; import java.util.Stack;import org.springframework.util.StringUtils;public class getEnableService {private static final String AND ="&&";private static final String OR ="||";private static final String UNEQUAL = "!=";private static final String EQUAL = "==";private Map<String, String> map;private Map<String, String> params;public boolean isEnable(String enable, Map<String, String> map,Map<String,String> params){this.map = map;this.params = params;if(StringUtils.isEmpty(enable)){return true;}Stack<String> operation = new Stack<String>();int tmpEnd = enable.length();for(int i = enable.length();i>1 ; i--){String twoChatter = enable.substring(i-2,i);if (twoChatter.equals(AND)||twoChatter.equals(OR)) {operation.add(enable.substring(i, tmpEnd).trim().replaceAll(" ", ""));operation.add(twoChatter);tmpEnd = i-2;}}operation.add(enable.substring(0,tmpEnd).replaceAll(" ", ""));System.out.println(operation);return judge(operation);}private boolean judge(Stack<String> operation) {if(operation.size()==1) {return isTrue(operation.pop());}else {boolean value1 = isTrue(operation.pop());String releation = operation.pop();boolean value2 = isTrue(operation.pop());if (releation.equals(AND)) {boolean operationResult = value1 && value2;operation.add(String.valueOf(operationResult));return judge(operation);}else if(releation.equals(OR)) {boolean operationResult = value1 || value2;operation.add(String.valueOf(operationResult));return judge(operation);}else {//LOG.error("the logical is wrong")return true;}}}private boolean isTrue(String condition) {if(condition.toLowerCase().equals("true")) {return true;}else if(condition.toLowerCase().equals("false")) {return false;}else {if(condition.contains(EQUAL)) {int index = condition.indexOf(EQUAL);String stringOfValue1 = condition.substring(0, index);String stringOfValue2 = condition.substring(index+2,condition.length());Object value1 = getValuFromString(stringOfValue1,params,map);Object value2 = getValuFromString(stringOfValue2,params,map);System.out.println(stringOfValue1+":"+value1);System.out.println(stringOfValue2+":"+value2);if(value1==null || value2 ==null) {//LOG.error();return false;}return value1.equals(value2);}else if (condition.contains(UNEQUAL)) {int index = condition.indexOf(UNEQUAL);String stringOfValue1 = condition.substring(0, index);String stringOfValue2 = condition.substring(index+2,condition.length());Object value1 = getValuFromString(stringOfValue1,params,map);Object value2 = getValuFromString(stringOfValue2,params,map);System.out.println(stringOfValue1+":"+value1);System.out.println(stringOfValue2+":"+value2);if(value1==null || value2 ==null) {//LOG.error();System.out.println("wrong");return false;}return !value1.equals(value2);}else {System.out.println("wrong");return false;}}}private Object getValuFromString(String configString, Map<String, String> params, Map<String, String> map) {// TODO 這個函數主要是根據configString的知道,從其他參數里面獲取想要的ValueString POINT ="\\.";String PARAMS ="params";String MAP ="map";String result = null;if (!StringUtils.isEmpty(configString) && configString.split(POINT).length == 1) {result = configString;} else if (!StringUtils.isEmpty(configString) &&configString.split(POINT).length == 2) {String source = configString.split(POINT)[0];String value = configString.split(POINT)[1];if (source.equals(PARAMS)) {result = params.get(value);} else if (source.equals(MAP)) {result = map.get(value);} }return result;}public static void main(String[] args) {Map<String ,String> map =new HashMap<>();Map<String ,String> params =new HashMap<>();map.put("name", "kangyucheng");params.put("name", "kangyucheng");map.put("id", "https://blog.csdn.net/kangyucheng");params.put("id", "https://blog.csdn.net/kangyucheng");getEnableService getEnableService =new getEnableService();String string1 ="params.name == map.name || params.id != map.id";System.out.println(getEnableService.isEnable(string1,map,params));String string2 ="params.name == map.id || params.id != map.id";System.out.println(getEnableService.isEnable(string2,map,params));String string3 ="params.name == map.id || params.id != true";System.out.println(getEnableService.isEnable(string3,map,params));}}結果:
| [params.id!=map.id, ||, params.name==map.name] params.name:kangyucheng map.name:kangyucheng params.id:https://blog.csdn.net/kangyucheng map.id:https://blog.csdn.net/kangyucheng true [params.id!=map.id, ||, params.name==map.id] params.name:kangyucheng map.id:https://blog.csdn.net/kangyucheng params.id:https://blog.csdn.net/kangyucheng map.id:https://blog.csdn.net/kangyucheng false [params.id!=true, ||, params.name==map.id] params.name:kangyucheng map.id:https://blog.csdn.net/kangyucheng params.id:https://blog.csdn.net/kangyucheng true:true true |
?
分析:
該實現方式采用棧來實現,首先遍歷一次整個字符串,將字符串切分,將切分結果入棧。
然后開始對棧進行遍歷,每次拿出一個符號和兩個表達式,將結果計算好之后再放回棧中。如此遞歸到棧中只有一個元素。
缺陷:
1、并沒有實現對括號的處理。
2、沒有對>=? ,<= ,< ,>等等等關系進行處理,只處理了== 和 != 。
之所以沒有對上述兩個進行處理,是因為我覺得配置文件是自己來編寫,且無需太復雜,以上代碼可以完成需要。
(換一種說法:我有點懶)?
?
總結
以上是生活随笔為你收集整理的java将字符串逻辑表达式转成布尔值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java:LocalDate计算两个日期
- 下一篇: 百练OJ:4151:电影节