2 Star 0 Fork 0

jiagou/SpringBoot-master

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Main.java 4.11 KB
一键复制 编辑 原始数据 按行查看 历史
jiagou 提交于 2024-03-12 09:40 . 1
package com.hk.xieyi.dubbo;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String ruleString = "{\"wendu\":\" >= 4.001\",\"shidu\":\" <= 100.001\",\"rain\":\" = 0\",\"wind\":\" > 11\",\"ss\": \"1.00,2.00,3.00,4\",\"qq\": \"1,2,3.02,4\"}";
String parameterString = "{\"wendu\":\"4.001\",\"qq\": \" 3.02\",\"ss\": \" 2.00\"}";
System.out.println(System.getProperty("java.library.path"));
String filteredParams = filterParameters(ruleString, parameterString);
System.out.println(filteredParams);
}
public static String filterParameters(String ruleString, String parameterString) {
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode ruleJson = mapper.readTree(ruleString);
JsonNode parameterJson = mapper.readTree(parameterString);
Map<String, Object> filteredParams = filterParameters(ruleJson, parameterJson);
return mapper.writeValueAsString(filteredParams);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static Map<String, Object> filterParameters(JsonNode ruleJson, JsonNode parameterJson) {
Map<String, Object> filteredParams = new HashMap<>();
ruleJson.fields().forEachRemaining(entry -> {
String key = entry.getKey();
JsonNode ruleNode = entry.getValue();
String rule = ruleNode.isTextual() ? ruleNode.asText() : ruleNode.toString();
if (parameterJson.has(key)) {
JsonNode valueNode = parameterJson.get(key);
if (evaluateRule(rule, valueNode)) {
// 只有满足规则的参数才会被添加到输出中
filteredParams.put(key, valueNode.asText());
}
} else {
System.err.println("Parameter key not found: " + key);
}
});
return filteredParams;
}
// 判断规则是否满足参数值
private static boolean evaluateRule(String rule, JsonNode valueNode) {
if (rule.contains(",")) {
String[] split = rule.split(",");
double value = valueNode.asDouble();
for (String ss : split) {
double v = Double.parseDouble(ss);
if (v == value) {
return true;
}
}
} else {
// 使用正则表达式匹配运算符和阈值
Pattern pattern = Pattern.compile("([><=!]+)\\s*([\\d.]+)");
Matcher matcher = pattern.matcher(rule);
if (matcher.find()) {
String operator = matcher.group(1);
try {
double threshold = Double.parseDouble(matcher.group(2));
double value = valueNode.asDouble();
switch (operator) {
case ">=":
return value >= threshold;
case "=":
return value == threshold;
case "<=":
return value <= threshold;
case "!=":
return value != threshold;
case ">":
return value > threshold;
case "<":
return value < threshold;
default:
return false;
}
} catch (NumberFormatException e) {
System.err.println("Invalid threshold format: " + matcher.group(2));
return false;
}
} else {
System.err.println("Invalid rule format: " + rule);
return false;
}
}
return false;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jiagoushiluxian/spring-boot-master.git
[email protected]:jiagoushiluxian/spring-boot-master.git
jiagoushiluxian
spring-boot-master
SpringBoot-master
master

搜索帮助