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: * DateCutExpression.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.util.Date;
032:
033: /**
034: * Prunes a date in a calendar-unware way. This method can be used to zero the milli-seconds or seconds and so on from a
035: * date-object. For more complex operations, the {@link org.jfree.report.function.date.VariableDateExpression} should be
036: * used instead.
037: * <p/>
038: * This expression simply executes a integer division followed by a integer multiplication on the milliseconds since
039: * 01-01-1970. For a factor of 1000, this sets the milliseconds to zero.
040: *
041: * @author Martin Schmid
042: * @deprecated The VariableDateExpression is much better suited for this purpose.
043: */
044: public class DateCutExpression extends AbstractExpression {
045: /**
046: * The name of the data-row field from where to read the date that should be modified.
047: */
048: private String field;
049: /**
050: * The factor by which the date should be pruned.
051: */
052: private long factor;
053:
054: /**
055: * Default Constructor. The factor defaults to 1000.
056: */
057: public DateCutExpression() {
058: factor = 1000;
059: }
060:
061: /**
062: * Returns the name of the data-row field from where to read the date that should be modified.
063: *
064: * @return a field name.
065: */
066: public String getField() {
067: return field;
068: }
069:
070: /**
071: * Defines the name of the data-row field from where to read the date that should be modified.
072: *
073: * @param field a field name.
074: */
075: public void setField(final String field) {
076: this .field = field;
077: }
078:
079: /**
080: * Returns the factor by which the date should be pruned.
081: *
082: * @return a factor.
083: */
084: public long getFactor() {
085: return factor;
086: }
087:
088: /**
089: * Defines the factor by which the date should be pruned.
090: *
091: * @param factor a factor.
092: */
093: public void setFactor(final long factor) {
094: this .factor = factor;
095: }
096:
097: /**
098: * Computes the pruned date.
099: *
100: * @return the value of the function.
101: */
102: public Object getValue() {
103: final Object date = getDataRow().get(getField());
104: if (date instanceof Date == false) {
105: return null;
106: }
107: if (factor == 0 || factor == 1) {
108: return date;
109: }
110: final Date d = (Date) date;
111: return new Date((d.getTime() / factor) * factor);
112: }
113: }
|