001/* 002 * Copyright 2010 Anyware Services 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 * http://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.ametys.plugins.repository.query.expression; 017 018import org.apache.commons.lang.StringUtils; 019 020/** 021 * Constructs an {@link Expression} corresponding to the logical "and" 022 * between two others {@link Expression}. 023 */ 024public class AndExpression implements Expression 025{ 026 private Expression[] _exprs; 027 028 /** 029 * Creates the "and" Expression. 030 * @param exprs the operands 031 */ 032 public AndExpression(Expression ... exprs) 033 { 034 _exprs = exprs; 035 } 036 037 public String build() 038 { 039 if (_exprs.length == 1 && _exprs[0] != null) 040 { 041 return _exprs[0].build(); 042 } 043 044 boolean isFirst = true; 045 StringBuilder sb = new StringBuilder("("); 046 047 for (Expression expr : _exprs) 048 { 049 if (expr != null) 050 { 051 String exprAsString = expr.build(); 052 if (StringUtils.isNotBlank(exprAsString)) 053 { 054 if (isFirst) 055 { 056 isFirst = false; 057 } 058 else 059 { 060 sb.append(" and "); 061 } 062 sb.append(exprAsString); 063 } 064 065 } 066 } 067 068 if (isFirst) 069 { 070 return ""; 071 } 072 else 073 { 074 return sb.append(")").toString(); 075 } 076 } 077}