001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.filter;
017:
018: /**
019: * Holds a mathematical relationship between two expressions. Note that the sub
020: * expressions must be math expressions. In other words, they must be a math
021: * literal, another math expression, or a feature attribute with a declared
022: * math type. You may create math expressions of arbitrary complexity by
023: * nesting other math expressions as sub expressions in one or more math
024: * expressions. This filter defines left and right values to clarify the sub
025: * expression precedence for non-associative operations, such as subtraction
026: * and division. For example, the left value is the numerator and the right is
027: * the denominator in an ExpressionMath division operation.
028: *
029: * @author Rob Hranac, Vision for New York
030: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/filter/MathExpressionImpl.java $
031: * @version $Id: MathExpressionImpl.java 24280 2007-02-08 16:40:54Z groldan $
032: */
033: public abstract class MathExpressionImpl extends DefaultExpression
034: implements MathExpression {
035:
036: /** Holds the 'left' value of this math expression. */
037: private org.opengis.filter.expression.Expression leftValue = null;
038:
039: /** Holds the 'right' value of this math expression. */
040: private org.opengis.filter.expression.Expression rightValue = null;
041:
042: /**
043: * No argument constructor.
044: */
045: protected MathExpressionImpl() {
046:
047: }
048:
049: protected MathExpressionImpl(
050: org.opengis.filter.expression.Expression e1,
051: org.opengis.filter.expression.Expression e2) {
052: this .leftValue = e1;
053: this .rightValue = e2;
054: }
055:
056: /**
057: * Adds the 'left' value to this expression.
058: *
059: * @param leftValue Expression to add to this expression.
060: *
061: * @throws IllegalFilterException Attempting to add non-math expression.
062: *
063: * @deprecated use {@link #setExpression1(org.opengis.filter.expression.Expression)}
064: */
065: public final void addLeftValue(Expression leftValue)
066: throws IllegalFilterException {
067:
068: setExpression1(leftValue);
069: }
070:
071: /**
072: * Adds the 'right' value to this expression.
073: *
074: * @param rightValue Expression to add to this expression.
075: *
076: * @throws IllegalFilterException Attempting to add non-math expression.
077: *
078: * @deprecated use {@link #setExpression2(org.opengis.filter.expression.Expression)}
079: */
080: public final void addRightValue(Expression rightValue)
081: throws IllegalFilterException {
082:
083: setExpression2(rightValue);
084: }
085:
086: /**
087: * Gets the left or first expression.
088: *
089: * @deprecated use {@link #getExpression1()}.
090: */
091: public final Expression getLeftValue() {
092: return (Expression) getExpression1();
093: }
094:
095: /**
096: *
097: * Gets the left or first expression.
098: *
099: * @return the expression on the first side of the comparison.
100: */
101: public org.opengis.filter.expression.Expression getExpression1() {
102: return leftValue;
103: }
104:
105: /**
106: *
107: * Gets the left or first expression.
108: *
109: * @throws IllegalFilterException
110: */
111: public void setExpression1(
112: org.opengis.filter.expression.Expression expression) {
113: Expression leftValue = (Expression) expression;
114: if (isGeometryExpression(leftValue.getType())) {
115: throw new IllegalFilterException(
116: "Attempted to add Geometry expression to math expression.");
117: }
118: this .leftValue = leftValue;
119: }
120:
121: /**
122: * Gets the right expression.
123: *
124: * @return the expression on the right of the comparison.
125: *
126: * @deprecated use {@link #getExpression2()}.
127: */
128: public final Expression getRightValue() {
129: return (Expression) getExpression2();
130: }
131:
132: /**
133: * Gets the second expression.
134: *
135: * @return the expression on the second side of the comparison.
136: */
137: public org.opengis.filter.expression.Expression getExpression2() {
138: return rightValue;
139: }
140:
141: /**
142: * Gets the second expression.
143: *
144: * @throws IllegalFilterException
145: */
146: public void setExpression2(
147: org.opengis.filter.expression.Expression expression) {
148: Expression rightValue = (Expression) expression;
149: //Check to see if this is a valid math expression before adding.
150: if (isGeometryExpression(rightValue.getType())) {
151: throw new IllegalFilterException(
152: "Attempted to add Geometry expression to math expression.");
153: }
154: this .rightValue = rightValue;
155: }
156:
157: /**
158: * Gets the type of this expression.
159: *
160: * @return Expression type.
161: */
162: public short getType() {
163: return expressionType;
164: }
165:
166: /**
167: * Convenience method which ensures that both expressions have been
168: * set. If any of operands not set an exception is thrown.
169: */
170: protected void ensureOperandsSet() throws IllegalArgumentException {
171: // Checks to make sure both sub expressions exist.
172: if ((leftValue == null) || (rightValue == null)) {
173: throw new IllegalArgumentException(
174: "Attempted read math expression with missing sub expressions.");
175: }
176: }
177:
178: protected Object number(double number) {
179: //return Filters.puts( number ); // non strongly typed
180: return new Double(number); // Getools 2.1 style
181: }
182:
183: }
|