perf:【IoT 物联网】优化 CurrentTimeConditionMatcher 时间条件匹配器 switch 兼容 jdk8

This commit is contained in:
puhui999 2025-08-24 12:04:08 +08:00
parent 94abcdff00
commit 9c6c1584e7

View File

@ -123,7 +123,6 @@ public class CurrentTimeConditionMatcher implements IotSceneRuleConditionMatcher
isDateTimeOperator(operatorEnum); isDateTimeOperator(operatorEnum);
} }
// TODO @puhui999switch 兼容下 jdk8
/** /**
* 匹配日期时间时间戳 * 匹配日期时间时间戳
* 直接实现时间戳比较逻辑 * 直接实现时间戳比较逻辑
@ -131,15 +130,17 @@ public class CurrentTimeConditionMatcher implements IotSceneRuleConditionMatcher
private boolean matchDateTime(long currentTimestamp, IotSceneRuleConditionOperatorEnum operatorEnum, String param) { private boolean matchDateTime(long currentTimestamp, IotSceneRuleConditionOperatorEnum operatorEnum, String param) {
try { try {
long targetTimestamp = Long.parseLong(param); long targetTimestamp = Long.parseLong(param);
return switch (operatorEnum) { switch (operatorEnum) {
case DATE_TIME_GREATER_THAN -> currentTimestamp > targetTimestamp; case DATE_TIME_GREATER_THAN:
case DATE_TIME_LESS_THAN -> currentTimestamp < targetTimestamp; return currentTimestamp > targetTimestamp;
case DATE_TIME_BETWEEN -> matchDateTimeBetween(currentTimestamp, param); case DATE_TIME_LESS_THAN:
default -> { return currentTimestamp < targetTimestamp;
case DATE_TIME_BETWEEN:
return matchDateTimeBetween(currentTimestamp, param);
default:
log.warn("[matchDateTime][operatorEnum({}) 不支持的日期时间操作符]", operatorEnum); log.warn("[matchDateTime][operatorEnum({}) 不支持的日期时间操作符]", operatorEnum);
yield false; return false;
} }
};
} catch (Exception e) { } catch (Exception e) {
log.error("[matchDateTime][operatorEnum({}) param({}) 日期时间匹配异常]", operatorEnum, param, e); log.error("[matchDateTime][operatorEnum({}) param({}) 日期时间匹配异常]", operatorEnum, param, e);
return false; return false;
@ -167,15 +168,17 @@ public class CurrentTimeConditionMatcher implements IotSceneRuleConditionMatcher
private boolean matchTime(LocalTime currentTime, IotSceneRuleConditionOperatorEnum operatorEnum, String param) { private boolean matchTime(LocalTime currentTime, IotSceneRuleConditionOperatorEnum operatorEnum, String param) {
try { try {
LocalTime targetTime = parseTime(param); LocalTime targetTime = parseTime(param);
return switch (operatorEnum) { switch (operatorEnum) {
case TIME_GREATER_THAN -> currentTime.isAfter(targetTime); case TIME_GREATER_THAN:
case TIME_LESS_THAN -> currentTime.isBefore(targetTime); return currentTime.isAfter(targetTime);
case TIME_BETWEEN -> matchTimeBetween(currentTime, param); case TIME_LESS_THAN:
default -> { return currentTime.isBefore(targetTime);
case TIME_BETWEEN:
return matchTimeBetween(currentTime, param);
default:
log.warn("[matchTime][operatorEnum({}) 不支持的时间操作符]", operatorEnum); log.warn("[matchTime][operatorEnum({}) 不支持的时间操作符]", operatorEnum);
yield false; return false;
} }
};
} catch (Exception e) { } catch (Exception e) {
log.error("[matchTime][][operatorEnum({}) param({}) 时间解析异常]", operatorEnum, param, e); log.error("[matchTime][][operatorEnum({}) param({}) 时间解析异常]", operatorEnum, param, e);
return false; return false;