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    @Override
045    public void accept(IVisitor visitor) {
046        visitor.visitMultiExpression(this);
047    }
048
049    @Override
050    public String toString() {
051        StringBuilder buf = new StringBuilder();
052        buf.append(operator);
053        buf.append('(');
054        for (Iterator<Operand> it = values.iterator(); it.hasNext();) {
055            Operand operand = it.next();
056            buf.append(operand.toString());
057            if (it.hasNext()) {
058                buf.append(", ");
059            }
060        }
061        buf.append(')');
062        return buf.toString();
063    }
064
065    @Override
066    public boolean equals(Object other) {
067        if (other == this) {
068            return true;
069        }
070        if (other instanceof MultiExpression) {
071            return equals((MultiExpression) other);
072        }
073        return false;
074    }
075
076    protected boolean equals(MultiExpression other) {
077        return values.equals(other.values) && super.equals(other);
078    }
079
080    @Override
081    public int hashCode() {
082        return values.hashCode() + super.hashCode();
083    }
084
085}