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: * DateSpanExpression.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.function.date;
030:
031: import java.util.Date;
032:
033: import org.jfree.report.function.AbstractExpression;
034:
035: /**
036: * Computes the difference date between the start and the end date.
037: *
038: * @author Thomas Morgner
039: */
040: public class DateSpanExpression extends AbstractExpression {
041: /**
042: * The field that contains the start-date.
043: */
044: private String startDateField;
045: /**
046: * The field that contains the end-date.
047: */
048: private String endDateField;
049:
050: /**
051: * Default Constructor.
052: */
053: public DateSpanExpression() {
054: }
055:
056: /**
057: * Returns the name of the field that contains the start-date.
058: *
059: * @return the start-date fieldname
060: */
061: public String getStartDateField() {
062: return startDateField;
063: }
064:
065: /**
066: * Defines the name of the field that contains the start-date.
067: *
068: * @param startDateField the start-date fieldname
069: */
070: public void setStartDateField(final String startDateField) {
071: this .startDateField = startDateField;
072: }
073:
074: /**
075: * Returns the name of the field that contains the end-date.
076: *
077: * @return the end-date fieldname
078: */
079: public String getEndDateField() {
080: return endDateField;
081: }
082:
083: /**
084: * Defines the name of the field that contains the end-date.
085: *
086: * @param endDateField the start-date fieldname
087: */
088: public void setEndDateField(final String endDateField) {
089: this .endDateField = endDateField;
090: }
091:
092: /**
093: * Computes the difference between the start and the end date. The start-field and end-field must contain either Date
094: * objects or Number objects. If the fields contain number objects, the number will be interpreted as milliseconds
095: * since 01-Jan-1970.
096: *
097: * @return the difference between start and end or null, if the difference could not be computed.
098: */
099: public Object getValue() {
100: if (startDateField == null) {
101: return null;
102: }
103: if (endDateField == null) {
104: return null;
105: }
106:
107: final Object startRaw = getDataRow().get(startDateField);
108: final long startTime;
109: if (startRaw instanceof Date) {
110: final Date start = (Date) startRaw;
111: startTime = start.getTime();
112: } else if (startRaw instanceof Number) {
113: final Number start = (Number) startRaw;
114: startTime = start.longValue();
115: } else {
116: return null;
117: }
118:
119: final Object endRaw = getDataRow().get(startDateField);
120: final long endTime;
121: if (endRaw instanceof Date) {
122: final Date end = (Date) endRaw;
123: endTime = end.getTime();
124: } else if (endRaw instanceof Number) {
125: final Number end = (Number) endRaw;
126: endTime = end.longValue();
127: } else {
128: return null;
129: }
130: return new Date(endTime - startTime);
131: }
132: }
|