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.Expression;
22: import org.opengis.filter.expression.ExpressionVisitor;
23: import org.opengis.filter.expression.Subtract;
24:
25: /**
26: * Implementation of Subtract expression.
27: *
28: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
29: *
30: */
31: public class SubtractImpl extends MathExpressionImpl implements
32: Subtract {
33:
34: public SubtractImpl(Expression expr1, Expression expr2) {
35: super (expr1, expr2);
36:
37: //backwards compatability with old type system
38: expressionType = MATH_SUBTRACT;
39: }
40:
41: public Object evaluate(Object feature)
42: throws IllegalArgumentException {
43: ensureOperandsSet();
44:
45: double leftDouble = Filters.number(getExpression1().evaluate(
46: feature));
47: double rightDouble = Filters.number(getExpression2().evaluate(
48: feature));
49:
50: return number(leftDouble - rightDouble);
51: }
52:
53: public Object accept(ExpressionVisitor visitor, Object extraData) {
54: return visitor.visit(this , extraData);
55: }
56:
57: /**
58: * Compares this expression to the specified object. Returns true if the
59: *
60: * @param obj - the object to compare this expression against.
61: *
62: * @return true if specified object is equal to this expression; false
63: * otherwise.
64: */
65: public boolean equals(Object obj) {
66: if (obj instanceof SubtractImpl) {
67: SubtractImpl other = (SubtractImpl) obj;
68:
69: return Utilities.equals(getExpression1(), other
70: .getExpression1())
71: && Utilities.equals(getExpression2(), other
72: .getExpression2());
73: } else {
74: return false;
75: }
76: }
77:
78: /**
79: * Override of hashCode method.
80: *
81: * @return a hash code value for this subtract expression.
82: */
83: public int hashCode() {
84: int result = 23;
85:
86: result = (37 * result) + getExpression1().hashCode();
87: result = (37 * result) + getExpression2().hashCode();
88:
89: return result;
90: }
91:
92: public String toString() {
93: return "(" + getExpression1().toString() + "-"
94: + getExpression2().toString() + ")";
95: }
96:
97: }
|