001/* 002 * Copyright (c) 2023-2026, Agents-Flex (fuhai999@gmail.com). 003 * <p> 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 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 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 com.agentsflex.core.store.condition; 017 018import com.agentsflex.core.util.StringUtil; 019 020public class Group extends Condition { 021 022 private String prevOperand = ""; 023 private final Condition childCondition; 024 025 public Group(Condition condition) { 026 this.childCondition = condition; 027 } 028 029 public Group(String prevOperand, Condition childCondition) { 030 this.prevOperand = prevOperand; 031 this.childCondition = childCondition; 032 } 033 034 035 @Override 036 public boolean checkEffective() { 037 boolean effective = super.checkEffective(); 038 if (!effective) { 039 return false; 040 } 041 Condition condition = this.childCondition; 042 while (condition != null) { 043 if (condition.checkEffective()) { 044 return true; 045 } 046 condition = condition.next; 047 } 048 return false; 049 } 050 051 052 @Override 053 public String toExpression(ExpressionAdaptor adaptor) { 054 StringBuilder expr = new StringBuilder(); 055 if (checkEffective()) { 056 String childExpr = childCondition.toExpression(adaptor); 057 Condition prevEffectiveCondition = getPrevEffectiveCondition(); 058 if (prevEffectiveCondition != null && this.connector != null) { 059 childExpr = adaptor.toConnector(this.connector) + this.prevOperand + adaptor.toGroupStart(this) + childExpr + adaptor.toGroupEnd(this); 060 } else if (StringUtil.hasText(childExpr)) { 061 childExpr = this.prevOperand + adaptor.toGroupStart(this) + childExpr + adaptor.toGroupEnd(this); 062 } 063 expr.append(childExpr); 064 } 065 066 if (this.next != null) { 067 expr.append(next.toExpression(adaptor)); 068 } 069 return expr.toString(); 070 } 071}