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.Iterator;
023import java.util.List;
024
025/**
026 * An expression for an single operator with an arbitrary number of operands.
027 * <p>
028 * It extends {@link Predicate} but it's really not a real Predicate (some users of Predicate expect it to have lvalue
029 * and rvalue fields, which are null in this class).
030 *
031 * @author Florent Guillaume
032 */
033public class MultiExpression extends Predicate {
034
035    private static final long serialVersionUID = 1L;
036
037    public final List<Operand> values;
038
039    public MultiExpression(Operator operator, List<Operand> values) {
040        super(null, operator, null);
041        this.values = values;
042    }
043
044    @SuppressWarnings("unchecked")
045    public static MultiExpression fromExpressionList(Operator operator, List<Expression> list) {
046        // bypass variance, as we know we won't modify the list by putting arbitrary Operands in it
047        return new MultiExpression(operator, (List<Operand>) ((List<?>) list));
048    }
049
050    @Override
051    public void accept(IVisitor visitor) {
052        visitor.visitMultiExpression(this);
053    }
054
055    @Override
056    public String toString() {
057        StringBuilder buf = new StringBuilder();
058        buf.append(operator);
059        buf.append('(');
060        for (Iterator<Operand> it = values.iterator(); it.hasNext();) {
061            Operand operand = it.next();
062            buf.append(operand.toString());
063            if (it.hasNext()) {
064                buf.append(", ");
065            }
066        }
067        buf.append(')');
068        return buf.toString();
069    }
070
071    @Override
072    public boolean equals(Object other) {
073        if (other == this) {
074            return true;
075        }
076        if (other instanceof MultiExpression) {
077            return equals((MultiExpression) other);
078        }
079        return false;
080    }
081
082    protected boolean equals(MultiExpression other) {
083        return values.equals(other.values) && super.equals(other);
084    }
085
086    @Override
087    public int hashCode() {
088        return values.hashCode() + super.hashCode();
089    }
090
091}