/* * Geotoolkit - An Open Source Java GIS Toolkit * http://www.geotoolkit.org * * (C) 2002-2008, Open Source Geospatial Foundation (OSGeo) * (C) 2009, Geomatys * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; * version 2.1 of the License. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. */ package org.geotoolkit.filter.binaryexpression; import org.geotoolkit.util.StringUtilities; import org.opengis.filter.expression.Add; import org.opengis.filter.expression.Expression; import org.opengis.filter.expression.ExpressionVisitor; /** * Immutable "add" expression. * * @author Johann Sorel (Geomatys) * @module */ public class DefaultAdd extends AbstractBinaryExpression<Expression,Expression> implements Add{ public DefaultAdd(final Expression left, final Expression right) { super(left,right); } /** * {@inheritDoc } */ @Override public Object evaluate(final Object object) { final Double val1 = left.evaluate(object, Double.class); final Double val2 = right.evaluate(object, Double.class); if(val1 == null || val2 == null){ final Object obj1 = left.evaluate(object); final Object obj2= right.evaluate(object); return String.valueOf(obj1) + String.valueOf(obj2); } return val1 + val2; } /** * {@inheritDoc } */ @Override public Object accept(final ExpressionVisitor visitor, final Object extraData) { return visitor.visit(this, extraData); } /** * {@inheritDoc } */ @Override public String toString() { final StringBuilder sb = new StringBuilder("Add \n"); sb.append(StringUtilities.toStringTree(left,right)); return sb.toString(); } /** * {@inheritDoc } */ @Override public boolean equals(final Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final AbstractBinaryExpression other = (AbstractBinaryExpression) obj; if (this.left != other.left && !this.left.equals(other.left)) { return false; } if (this.right != other.right && !this.right.equals(other.right)) { return false; } return true; } /** * {@inheritDoc } */ @Override public int hashCode() { int hash = 5; hash = 53 * hash + this.left.hashCode() ; hash = 53 * hash + this.right.hashCode() ; return hash; } }