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    /** Arbitrary info associated to the expression. */
042    public Object info;
043
044    public Expression(Operand lvalue, Operator operator, Operand rvalue) {
045        this.lvalue = lvalue;
046        this.rvalue = rvalue;
047        this.operator = operator;
048    }
049
050    @Override
051    public void accept(IVisitor visitor) {
052        visitor.visitExpression(this);
053    }
054
055    /**
056     * Is the unary operator pretty-printed after the operand?
057     */
058    public boolean isSuffix() {
059        return operator == Operator.ISNULL || operator == Operator.ISNOTNULL;
060    }
061
062    public void setInfo(Object info) {
063        this.info = info;
064    }
065
066    public Object getInfo() {
067        return info;
068    }
069
070    @Override
071    public String toString() {
072        if (rvalue == null) {
073            if (isSuffix()) {
074                return lvalue.toString() + ' ' + operator.toString();
075            } else {
076                return operator.toString() + ' ' + lvalue.toString();
077            }
078        } else {
079            return '(' + lvalue.toString() + ' ' + operator.toString() + ' ' + rvalue.toString() + ')';
080        }
081    }
082
083    @Override
084    public boolean equals(Object obj) {
085        if (obj == this) {
086            return true;
087        }
088        if (obj instanceof Expression) {
089            Expression e = (Expression) obj;
090            if (operator.id != e.operator.id) {
091                return false;
092            }
093            if (!lvalue.equals(e.lvalue)) {
094                return false;
095            }
096            if (rvalue != null) {
097                if (!rvalue.equals(e.rvalue)) {
098                    return false;
099                }
100            } else if (e.rvalue != null) {
101                return false;
102            }
103            return true;
104        }
105        return false;
106    }
107
108    @Override
109    public int hashCode() {
110        int result = 17;
111        result = 37 * result + operator.hashCode();
112        result = 37 * result + (lvalue == null ? 0 : lvalue.hashCode());
113        result = 37 * result + (rvalue == null ? 0 : rvalue.hashCode());
114        return result;
115    }
116
117    public boolean isPathExpression() {
118        return (lvalue instanceof Reference) && NXQL.ECM_PATH.equals(((Reference) lvalue).name);
119    }
120
121}