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 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.core.query.sql.model;
023
024import org.nuxeo.ecm.core.query.sql.NXQL;
025
026/**
027 * An infix expression.
028 *
029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
030 */
031public class Expression implements Operand {
032
033    private static final long serialVersionUID = 6007989243273673300L;
034
035    public final Operator operator;
036
037    public final Operand lvalue;
038
039    public final Operand rvalue;
040
041    public Expression(Operand lvalue, Operator operator, Operand rvalue) {
042        this.lvalue = lvalue;
043        this.rvalue = rvalue;
044        this.operator = operator;
045    }
046
047    @Override
048    public void accept(IVisitor visitor) {
049        visitor.visitExpression(this);
050    }
051
052    /**
053     * Is the unary operator pretty-printed after the operand?
054     */
055    public boolean isSuffix() {
056        return operator == Operator.ISNULL || operator == Operator.ISNOTNULL;
057    }
058
059    @Override
060    public String toString() {
061        if (rvalue == null) {
062            if (isSuffix()) {
063                return lvalue.toString() + ' ' + operator.toString();
064            } else {
065                return operator.toString() + ' ' + lvalue.toString();
066            }
067        } else {
068            return lvalue.toString() + ' ' + operator.toString() + ' ' + rvalue.toString();
069        }
070    }
071
072    @Override
073    public boolean equals(Object obj) {
074        if (obj == this) {
075            return true;
076        }
077        if (obj instanceof Expression) {
078            Expression e = (Expression) obj;
079            if (operator.id != e.operator.id) {
080                return false;
081            }
082            if (!lvalue.equals(e.lvalue)) {
083                return false;
084            }
085            if (rvalue != null) {
086                if (!rvalue.equals(e.rvalue)) {
087                    return false;
088                }
089            } else if (e.rvalue != null) {
090                return false;
091            }
092            return true;
093        }
094        return false;
095    }
096
097    @Override
098    public int hashCode() {
099        int result = 17;
100        result = 37 * result + operator.hashCode();
101        result = 37 * result + lvalue.hashCode();
102        result = 37 * result + (rvalue == null ? 0 : rvalue.hashCode());
103        return result;
104    }
105
106    public boolean isPathExpression() {
107        return (lvalue instanceof Reference) && NXQL.ECM_PATH.equals(((Reference) lvalue).name);
108    }
109
110}