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