001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Florent Guillaume
011 */
012
013package org.nuxeo.ecm.core.query.sql.model;
014
015import java.util.Iterator;
016import java.util.List;
017
018/**
019 * An expression for an single operator with an arbitrary number of operands.
020 * <p>
021 * It extends {@link Predicate} but it's really not a real Predicate (some users of Predicate expect it to have lvalue
022 * and rvalue fields, which are null in this class).
023 *
024 * @author Florent Guillaume
025 */
026public class MultiExpression extends Predicate {
027
028    private static final long serialVersionUID = 1L;
029
030    public final List<Operand> values;
031
032    public MultiExpression(Operator operator, List<Operand> values) {
033        super(null, operator, null);
034        this.values = values;
035    }
036
037    @Override
038    public void accept(IVisitor visitor) {
039        visitor.visitMultiExpression(this);
040    }
041
042    @Override
043    public String toString() {
044        StringBuilder buf = new StringBuilder();
045        buf.append(operator);
046        buf.append('(');
047        for (Iterator<Operand> it = values.iterator(); it.hasNext();) {
048            Operand operand = it.next();
049            buf.append(operand.toString());
050            if (it.hasNext()) {
051                buf.append(", ");
052            }
053        }
054        buf.append(')');
055        return buf.toString();
056    }
057
058    @Override
059    public boolean equals(Object other) {
060        if (other == this) {
061            return true;
062        }
063        if (other instanceof MultiExpression) {
064            return equals((MultiExpression) other);
065        }
066        return false;
067    }
068
069    protected boolean equals(MultiExpression other) {
070        return values.equals(other.values) && super.equals(other);
071    }
072
073    @Override
074    public int hashCode() {
075        return values.hashCode() + super.hashCode();
076    }
077
078}