001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 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 * 016 * Contributors: 017 * Florent Guillaume 018 */ 019 020package org.nuxeo.ecm.core.query.sql.model; 021 022import java.util.ArrayList; 023import java.util.Iterator; 024import java.util.List; 025 026/** 027 * An expression for an single operator with an arbitrary number of operands. 028 * <p> 029 * It extends {@link Predicate} but it's really not a real Predicate (some users of Predicate expect it to have lvalue 030 * and rvalue fields, which are null in this class). 031 * 032 * @author Florent Guillaume 033 */ 034public class MultiExpression extends Predicate { 035 036 private static final long serialVersionUID = 1L; 037 038 public final List<Predicate> predicates; 039 040 public MultiExpression(Operator operator, List<Predicate> predicates) { 041 super(null, operator, null); 042 this.predicates = predicates; 043 } 044 045 /** 046 * Copy constructor. 047 * 048 * @since 10.3 049 */ 050 public MultiExpression(MultiExpression other) { 051 this(other.operator, new ArrayList<>(other.predicates)); 052 } 053 054 @Override 055 public void accept(IVisitor visitor) { 056 visitor.visitMultiExpression(this); 057 } 058 059 @Override 060 public String toString() { 061 StringBuilder buf = new StringBuilder(); 062 buf.append(operator); 063 buf.append('('); 064 for (Iterator<Predicate> it = predicates.iterator(); it.hasNext();) { 065 Predicate predicate = it.next(); 066 buf.append(predicate.toString()); 067 if (it.hasNext()) { 068 buf.append(", "); 069 } 070 } 071 buf.append(')'); 072 return buf.toString(); 073 } 074 075 @Override 076 public boolean equals(Object other) { 077 if (other == this) { 078 return true; 079 } 080 if (other instanceof MultiExpression) { 081 return equals((MultiExpression) other); 082 } 083 return false; 084 } 085 086 protected boolean equals(MultiExpression other) { 087 return predicates.equals(other.predicates) && super.equals(other); 088 } 089 090 @Override 091 public int hashCode() { 092 return predicates.hashCode() + super.hashCode(); 093 } 094 095}