001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * AbstractCompareExpression.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.function;
030:
031: import java.math.BigDecimal;
032:
033: /**
034: * The base class for all expressions that compare a value read from the datarow with another value. This expression and
035: * all subclasses have been made obsolete by the Formula-Support.
036: * <p/>
037: * The given field name is always used to retrieve the left hand operand of the comparison. The value supplied by the
038: * expression implementation is always the right hand operand.
039: *
040: * @author Thomas Morgner
041: */
042: public abstract class AbstractCompareExpression extends
043: AbstractExpression {
044: /**
045: * A constant for an equals comparison.
046: */
047: public static final String EQUAL = "equal";
048: /**
049: * A constant for a not equals comparison.
050: */
051: public static final String NOT_EQUAL = "not-equal";
052: /**
053: * A constant for lower than comparison.
054: */
055: public static final String LOWER = "lower";
056: /**
057: * A constant for greater than comparison.
058: */
059: public static final String GREATER = "greater";
060: /**
061: * A constant for lower than or equal comparison.
062: */
063: public static final String LOWER_EQUAL = "lower-equal";
064: /**
065: * A constant for greater than or equal comparison.
066: */
067: public static final String GREATER_EQUAL = "greater-equal";
068:
069: /**
070: * A field holding the compare method that should be used.
071: */
072: private String compareMethod;
073: /**
074: * The name of the field that should be read.
075: */
076: private String field;
077:
078: /**
079: * The default constructor.
080: */
081: protected AbstractCompareExpression() {
082: }
083:
084: /**
085: * Compares the value read from the data-row with the value supplied by the expression itself. If the value
086: * is null or no comparable or cannot be compared for other reasons, this expression returns Boolean.FALSE.
087: *
088: * @return Boolean.TRUE or Boolean.FALSE.
089: */
090: public Object getValue() {
091: final Object o = getDataRow().get(getField());
092: if (o instanceof Comparable == false) {
093: return Boolean.FALSE;
094: }
095:
096: try {
097: final Comparable c = (Comparable) o;
098: final Comparable comparable = getComparable();
099: if (comparable == null) {
100: return Boolean.FALSE;
101: }
102:
103: int result;
104: try {
105: // Comparing the easy way: Both values are the same type ..
106: result = c.compareTo(comparable);
107: // this results in a class-cast exception if they are not the same type.
108: } catch (Exception e) {
109: try {
110: // invert it ..
111: // Comparing the easy way: Both values are the same type ..
112: result = -comparable.compareTo(c);
113: // this results in a class-cast exception if they are not the same type.
114: } catch (Exception e2) {
115: if (c instanceof Number
116: && comparable instanceof Number) {
117: final BigDecimal bd1 = new BigDecimal(String
118: .valueOf(c));
119: final BigDecimal bd2 = new BigDecimal(String
120: .valueOf(comparable));
121: result = bd1.compareTo(bd2);
122: } else {
123: // not comparable ..
124: return Boolean.FALSE;
125: }
126: }
127: }
128:
129: final String method = getCompareMethod();
130: if (EQUAL.equals(method)) {
131: if (result == 0) {
132: return Boolean.TRUE;
133: }
134: return Boolean.FALSE;
135: }
136: if (NOT_EQUAL.equals(method)) {
137: if (result != 0) {
138: return Boolean.TRUE;
139: }
140: return Boolean.FALSE;
141: }
142: if (LOWER.equals(method)) {
143: if (result < 0) {
144: return Boolean.TRUE;
145: }
146: return Boolean.FALSE;
147: }
148: if (LOWER_EQUAL.equals(method)) {
149: if (result <= 0) {
150: return Boolean.TRUE;
151: }
152: return Boolean.FALSE;
153: }
154: if (GREATER.equals(method)) {
155: if (result > 0) {
156: return Boolean.TRUE;
157: }
158: return Boolean.FALSE;
159: }
160: if (GREATER_EQUAL.equals(method)) {
161: if (result >= 0) {
162: return Boolean.TRUE;
163: }
164: return Boolean.FALSE;
165: }
166: } catch (Exception e) {
167: // ignore ...
168: }
169: return Boolean.FALSE;
170: }
171:
172: /**
173: * Returns the name of the field from where to read the first operand.
174: *
175: * @return the field name.
176: */
177: public String getField() {
178: return field;
179: }
180:
181: /**
182: * Defines the name of the field from where to read the first operand.
183: *
184: * @param field the field name.
185: */
186: public void setField(final String field) {
187: this .field = field;
188: }
189:
190: /**
191: * Returns the compare method that should be used in the expression evaluation. Must be one of the constants declared
192: * in this expression.
193: *
194: * @return the compare method.
195: */
196: public String getCompareMethod() {
197: return compareMethod;
198: }
199:
200: /**
201: * Defines the compare method that should be used in the expression evaluation. Must be one of the constants declared
202: * in this expression.
203: *
204: * @param compareMethod the compare method.
205: */
206: public void setCompareMethod(final String compareMethod) {
207: this .compareMethod = compareMethod;
208: }
209:
210: /**
211: * Returns the right-hand operand used in the comparison.
212: *
213: * @return the right-hand operand.
214: */
215: protected abstract Comparable getComparable();
216:
217: }
|