001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 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: import org.geotools.filter.expression.Value;
019: import org.opengis.filter.BinaryComparisonOperator;
020: import org.opengis.filter.expression.Expression;
021:
022: /**
023: * Abstract implemention for binary filters.
024: *
025: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
026: */
027: public abstract class BinaryComparisonAbstract extends AbstractFilter
028: implements BinaryComparisonOperator {
029:
030: protected Expression expression1;
031: protected Expression expression2;
032:
033: boolean matchingCase;
034:
035: protected BinaryComparisonAbstract(
036: org.opengis.filter.FilterFactory factory) {
037: this (factory, null, null);
038: }
039:
040: protected BinaryComparisonAbstract(
041: org.opengis.filter.FilterFactory factory,
042: Expression expression1, Expression expression2) {
043: this (factory, expression1, expression2, true);
044: }
045:
046: protected BinaryComparisonAbstract(
047: org.opengis.filter.FilterFactory factory,
048: Expression expression1, Expression expression2,
049: boolean matchingCase) {
050: super (factory);
051: this .expression1 = expression1;
052: this .expression2 = expression2;
053: this .matchingCase = matchingCase;
054: }
055:
056: public Expression getExpression1() {
057: return expression1;
058: }
059:
060: public void setExpression1(Expression expression) {
061: this .expression1 = expression;
062: }
063:
064: public Expression getExpression2() {
065: return expression2;
066: }
067:
068: public void setExpression2(Expression expression) {
069: this .expression2 = expression;
070: }
071:
072: public boolean isMatchingCase() {
073: return matchingCase;
074: }
075:
076: public Filter and(org.opengis.filter.Filter filter) {
077: return (Filter) factory.and(this , filter);
078: }
079:
080: public Filter or(org.opengis.filter.Filter filter) {
081: return (Filter) factory.or(this , filter);
082: }
083:
084: public Filter not() {
085: return (Filter) factory.not(this );
086: }
087:
088: /**
089: * Convenience method which evaluates the expressions and trys to align the values
090: * to be of the same type.
091: * <p>
092: * If the values can not be aligned, the original values are returned.
093: * </p>
094: *
095: * @return
096: */
097: protected Object[] eval(Object object) {
098: Value v1 = new Value(eval(getExpression1(), object));
099: Value v2 = new Value(eval(getExpression2(), object));
100:
101: if (v1.getValue() != null && v2.getValue() != null) {
102: //try to convert so that values are of same type
103: if (v1.getValue().getClass().equals(
104: v2.getValue().getClass())) {
105: //nothing to do
106: return new Object[] { v1.getValue(), v2.getValue() };
107: }
108:
109: Object o = v2.value(v1.getValue().getClass());
110: if (o != null) {
111: return new Object[] { v1.getValue(), o };
112: }
113:
114: //try the other way
115: o = v1.value(v2.getValue().getClass());
116: if (o != null) {
117: return new Object[] { o, v2.getValue() };
118: }
119: }
120:
121: return new Object[] { v1.getValue(), v2.getValue() };
122: }
123:
124: /**
125: * Wraps an object in a Comparable.
126: * @param value The original value.
127: * @return A comparable
128: */
129: protected Comparable comparable(Object value) {
130: if (value == null) {
131: return null;
132: }
133:
134: if (value instanceof Comparable) {
135: return (Comparable) value;
136: } else {
137: return String.valueOf(value);
138: }
139: }
140: }
|