001/* 002 * Copyright 2024-2025, Warm-Flow (290631660@qq.com). 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.dromara.warm.flow.core.utils; 017 018import org.dromara.warm.flow.core.constant.ExceptionCons; 019import org.dromara.warm.flow.core.exception.FlowException; 020import org.dromara.warm.flow.core.expression.*; 021 022import java.util.HashMap; 023import java.util.Map; 024import java.util.concurrent.atomic.AtomicBoolean; 025 026/** 027 * 条件表达式工具类 028 * 029 * @author warm 030 */ 031public class ExpressionUtil { 032 033 private static final Map<String, ExpressionStrategy> map = new HashMap<>(); 034 035 private ExpressionUtil() { 036 } 037 038 static { 039 setExpression(new ExpressionStrategyEq()); 040 setExpression(new ExpressionStrategyGe()); 041 setExpression(new ExpressionStrategyGt()); 042 setExpression(new ExpressionStrategyLe()); 043 setExpression(new ExpressionStrategyLike()); 044 setExpression(new ExpressionStrategyLt()); 045 setExpression(new ExpressionStrategyNe()); 046 setExpression(new ExpressionStrategyNotLike()); 047 } 048 049 public static void setExpression(ExpressionStrategy expressionStrategy) { 050 map.put(expressionStrategy.getType(), expressionStrategy); 051 } 052 053 /** 054 * @param expression 条件表达式,比如“@@eq@@|flag@@eq@@4” ,或者自定义策略 055 * @param variable 056 * @return 057 */ 058 public static boolean eval(String expression, Map<String, Object> variable) { 059 if (StringUtils.isEmpty(expression)) { 060 return true; 061 } 062 AtomicBoolean flag = new AtomicBoolean(false); 063 map.forEach((k, v) -> { 064 if (expression.startsWith(k + "|")) { 065 if (v == null) { 066 throw new FlowException(ExceptionCons.NULL_EXPRESSION_STRATEGY); 067 } 068 flag.set(v.eval(expression.replace(k + "|", ""), variable)); 069 } 070 }); 071 return flag.get(); 072 } 073 074}