001    /*
002     * Copyright (c) 2005 Einar Pehrson <einar@pehrson.nu>.
003     *
004     * This file is part of
005     * CleanSheets - a spreadsheet application for the Java platform.
006     *
007     * CleanSheets is free software; you can redistribute it and/or modify
008     * it under the terms of the GNU General Public License as published by
009     * the Free Software Foundation; either version 2 of the License, or
010     * (at your option) any later version.
011     *
012     * CleanSheets is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with CleanSheets; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
020     */
021    package csheets.core.formula;
022    
023    import csheets.core.IllegalValueTypeException;
024    import csheets.core.Value;
025    import csheets.core.formula.util.ExpressionVisitor;
026    
027    /**
028     * A binary operation in a formula.
029     * @author Einar Pehrson
030     */
031    public class BinaryOperation extends Operation<BinaryOperator> {
032    
033            /** The unique version identifier used for serialization */
034            private static final long serialVersionUID = 2326739272985753461L;
035    
036            /** The left(first) operand */
037            private Expression leftOperand;
038    
039            /** The right(second) operand */
040            private Expression rightOperand;
041    
042            /**
043             * Creates a new binary operation.
044             * @param leftOperand the left(first) operand
045             * @param operator the binary operator
046             * @param rightOperand the right(second) operand
047             */
048            public BinaryOperation(Expression leftOperand, BinaryOperator operator, Expression rightOperand) {
049                    super(operator);
050                    this.leftOperand = leftOperand;
051                    this.rightOperand = rightOperand;
052            }
053    
054            public Value evaluate() throws IllegalValueTypeException {
055                    return operator.applyTo(leftOperand, rightOperand);
056            }
057    
058            /** 
059             * Returns the left(first) operand.
060             * @return an expression tree representing the operand
061             */
062            public Expression getLeftOperand() {
063                    return leftOperand;
064            }
065    
066            /** 
067             * Returns the right(second) operand.
068             * @return an expression tree representing the operand
069             */
070            public Expression getRightOperand() {
071                    return rightOperand;
072            }
073    
074            public Object accept(ExpressionVisitor visitor) {
075                    return visitor.visitBinaryOperation(this);
076            }
077    
078            public String toString() {
079                    return leftOperand + operator.toString() + rightOperand;
080            }
081    }