01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2006, GeoTools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.filter.expression;
17:
18: import org.geotools.filter.Filters;
19: import org.geotools.filter.MathExpressionImpl;
20: import org.geotools.resources.Utilities;
21: import org.opengis.filter.expression.Add;
22: import org.opengis.filter.expression.Expression;
23: import org.opengis.filter.expression.ExpressionVisitor;
24:
25: /**
26: * Implementation of Add expression.
27: *
28: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
29: *
30: */
31: public class AddImpl extends MathExpressionImpl implements Add {
32:
33: public AddImpl(Expression expr1, Expression expr2) {
34: super (expr1, expr2);
35: //backwards compatability with old type system
36: expressionType = MATH_ADD;
37: }
38:
39: public Object evaluate(Object feature)
40: throws IllegalArgumentException {
41: ensureOperandsSet();
42:
43: double leftDouble = Filters.number(getExpression1().evaluate(
44: feature));
45: double rightDouble = Filters.number(getExpression2().evaluate(
46: feature));
47:
48: return number(leftDouble + rightDouble);
49: }
50:
51: public Object accept(ExpressionVisitor visitor, Object extraData) {
52: return visitor.visit(this , extraData);
53: }
54:
55: /**
56: * Compares this expression to the specified object. Returns true if the
57: *
58: * @param obj - the object to compare this expression against.
59: *
60: * @return true if specified object is equal to this expression; false
61: * otherwise.
62: */
63: public boolean equals(Object obj) {
64: if (obj instanceof AddImpl) {
65: AddImpl other = (AddImpl) obj;
66:
67: return Utilities.equals(getExpression1(), other
68: .getExpression1())
69: && Utilities.equals(getExpression2(), other
70: .getExpression2());
71: } else {
72: return false;
73: }
74: }
75:
76: /**
77: * Override of hashCode method.
78: *
79: * @return a hash code value for this add expression.
80: */
81: public int hashCode() {
82: int result = 23;
83:
84: result = (37 * result) + getExpression1().hashCode();
85: result = (37 * result) + getExpression2().hashCode();
86:
87: return result;
88: }
89:
90: public String toString() {
91: return "(" + getExpression1().toString() + "+"
92: + getExpression2().toString() + ")";
93: }
94: }
|