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