001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements.  See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License.  You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.granite.gravity.selector;
019
020/**
021 * An expression which performs an operation on two expression values.
022 *
023 * @version $Revision: 1.2 $
024 */
025abstract public class BinaryExpression implements Expression {
026    protected Expression left;
027    protected Expression right;
028
029    public BinaryExpression(Expression left, Expression right) {
030        this.left = left;
031        this.right = right;
032    }
033
034    public Expression getLeft() {
035        return left;
036    }
037
038    public Expression getRight() {
039        return right;
040    }
041
042
043    /**
044     * @see java.lang.Object#toString()
045     */
046    @Override
047    public String toString() {
048        return "(" + left.toString() + " " + getExpressionSymbol() + " " + right.toString() + ")";
049    }
050
051    /**
052     * TODO: more efficient hashCode()
053     *
054     * @see java.lang.Object#hashCode()
055     */
056    @Override
057    public int hashCode() {
058        return toString().hashCode();
059    }
060
061    /**
062     * TODO: more efficient hashCode()
063     *
064     * @see java.lang.Object#equals(java.lang.Object)
065     */
066    @Override
067    public boolean equals(Object o) {
068
069        if (o == null || !this.getClass().equals(o.getClass())) {
070            return false;
071        }
072        return toString().equals(o.toString());
073
074    }
075
076    /**
077     * Returns the symbol that represents this binary expression.  For example, addition is
078     * represented by "+"
079     *
080     * @return the symbol
081     */
082    abstract public String getExpressionSymbol();
083
084    /**
085     * @param expression
086     */
087    public void setRight(Expression expression) {
088        right = expression;
089    }
090
091    /**
092     * @param expression
093     */
094    public void setLeft(Expression expression) {
095        left = expression;
096    }
097
098}