1 | /*
|
---|
2 | * GeoTools - The Open Source Java GIS Toolkit
|
---|
3 | * http://geotools.org
|
---|
4 | *
|
---|
5 | * (C) 2006-2008, Open Source Geospatial Foundation (OSGeo)
|
---|
6 | *
|
---|
7 | * This library is free software; you can redistribute it and/or
|
---|
8 | * modify it under the terms of the GNU Lesser General Public
|
---|
9 | * License as published by the Free Software Foundation;
|
---|
10 | * version 2.1 of the License.
|
---|
11 | *
|
---|
12 | * This library is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
15 | * Lesser General Public License for more details.
|
---|
16 | */
|
---|
17 | package org.geotools.filter.expression;
|
---|
18 |
|
---|
19 | import org.geotools.filter.Filters;
|
---|
20 | import org.geotools.filter.MathExpressionImpl;
|
---|
21 | import org.geotools.util.Utilities;
|
---|
22 | import org.opengis.filter.expression.Expression;
|
---|
23 | import org.opengis.filter.expression.ExpressionVisitor;
|
---|
24 | import org.opengis.filter.expression.Subtract;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Implementation of Subtract expression.
|
---|
28 | *
|
---|
29 | * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
|
---|
30 | *
|
---|
31 | *
|
---|
32 | *
|
---|
33 | * @source $URL: http://svn.osgeo.org/geotools/branches/2.7.x/modules/library/main/src/main/java/org/geotools/filter/expression/SubtractImpl.java $
|
---|
34 | */
|
---|
35 | public class SubtractImpl extends MathExpressionImpl implements Subtract {
|
---|
36 |
|
---|
37 | public SubtractImpl(Expression expr1, Expression expr2) {
|
---|
38 | super(expr1,expr2);
|
---|
39 |
|
---|
40 | //backwards compatability with old type system
|
---|
41 | expressionType = MATH_SUBTRACT;
|
---|
42 | }
|
---|
43 |
|
---|
44 | public Object evaluate(Object feature) throws IllegalArgumentException {
|
---|
45 | ensureOperandsSet();
|
---|
46 |
|
---|
47 | double leftDouble = Filters.number( getExpression1().evaluate(feature) );
|
---|
48 | double rightDouble = Filters.number( getExpression2().evaluate(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.getExpression1())
|
---|
70 | && Utilities.equals(getExpression2(),other.getExpression2());
|
---|
71 | } else {
|
---|
72 | return false;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | /**
|
---|
76 | * Override of hashCode method.
|
---|
77 | *
|
---|
78 | * @return a hash code value for this subtract expression.
|
---|
79 | */
|
---|
80 | public int hashCode() {
|
---|
81 | int result = 23;
|
---|
82 |
|
---|
83 | result = (37 * result) + getExpression1().hashCode();
|
---|
84 | result = (37 * result) + getExpression2().hashCode();
|
---|
85 |
|
---|
86 | return result;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public String toString() {
|
---|
90 | return "(" + getExpression1().toString() + "-" + getExpression2().toString()+ ")";
|
---|
91 | }
|
---|
92 |
|
---|
93 | }
|
---|