View Javadoc

1   /***
2    * DateOrderFilter.java
3    *
4    * This file is part of the creme library.
5    * The creme library intends to ease the development effort of large
6    * applications by providing easy to use support classes.
7    *
8    * Copyright (C) 2002 Denis Bregeon
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public
12   * License as published by the Free Software Foundation; either
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   * contact information: dbregeon@sourceforge.net
25   */
26  package org.jcreme.filters;
27  
28  import java.io.Serializable;
29  import java.text.DateFormat;
30  import java.util.Comparator;
31  import java.util.Date;
32  
33  /***
34   * A specialized OrderFilter that uses a date Comparator and only accepts Date
35   * objects as referent objects.
36   * 
37   * @author $Author: dbregeon $
38   * @version $Revision: 1.3 $
39   */
40  public class DateOrderFilter extends OrderFilter {
41  	static final long serialVersionUID = 7445268172553087045L;
42  
43  	/***
44  	 * A comparator that compares dates.
45  	 */
46  	protected static class DateComparator implements Comparator, Serializable {
47  		static final long serialVersionUID = 2204621158625903727L;
48  
49  		/***
50  		 * Compares the passed in parameters if they are both Date objects.
51  		 * 
52  		 * @param obj1
53  		 *            The first object to be used for comparison.
54  		 * @param obj2
55  		 *            The second object to be used for comparison.
56  		 * @return -2 if any of the passed in objects is not a Date object. 0,
57  		 *         -1 or 1 respectively if the first date is respectively equal
58  		 *         to, before or after the second one.
59  		 */
60  		public int compare(final Object obj1,final Object obj2) {
61  			int result = 0;
62  			if ((obj1 == null) || (obj2 == null)) {
63  				new IllegalArgumentException("Null is not a valid value.")
64  						.printStackTrace();
65  				return -2;
66  			}
67  			result = ((Date) obj1).compareTo(obj2);
68  			return result;
69  		}
70  	}
71  
72  	/***
73  	 * The date comparator used by every DateOrderFilter object.
74  	 */
75  	private transient static final Comparator dateComparator = new DateComparator();
76  
77  	/***
78  	 * Creates new DateOrderFilter
79  	 * 
80  	 * @param op
81  	 *            The OrderComparisonOperator to be used.
82  	 * @param date
83  	 *            The Date object to be used as the referent object.
84  	 * @throws IllegalArgumentException
85  	 *             In case any of the passed in parameters is null, which is not
86  	 *             wanted here.
87  	 */
88  	public DateOrderFilter(OrderComparisonOperator op, Date date)
89  			throws IllegalArgumentException {
90  		// Simply call super.
91  		super(op, dateComparator, date);
92  
93  	}
94  
95  	/***
96  	 * Gives an SQL-like string representation for this filter.
97  	 * 
98  	 * @return the string representation of this filter.
99  	 */
100 	public String toString() {
101 		return getOperator()
102 				+ " "
103 				+ DateFormat.getDateInstance(DateFormat.SHORT).format(
104 						getReferent());
105 	}
106 }