01: /*
02: JSPWiki - a JSP-based WikiWiki clone.
03:
04: Copyright (C) 2001 Janne Jalkanen (Janne.Jalkanen@iki.fi)
05:
06: This program is free software; you can redistribute it and/or modify
07: it under the terms of the GNU Lesser General Public License as published by
08: the Free Software Foundation; either version 2.1 of the License, or
09: (at your option) any later version.
10:
11: This program is distributed in the hope that it will be useful,
12: but WITHOUT ANY WARRANTY; without even the implied warranty of
13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: GNU Lesser General Public License for more details.
15:
16: You should have received a copy of the GNU Lesser General Public License
17: along with this program; if not, write to the Free Software
18: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: */
20: package com.ecyrd.jspwiki;
21:
22: import java.io.Serializable;
23: import java.util.*;
24: import org.apache.log4j.Logger;
25:
26: /**
27: * Compares the lastModified date of its arguments. Both o1 and o2 MUST
28: * be WikiPage objects, or else you will receive a ClassCastException.
29: * <p>
30: * If the lastModified date is the same, then the next key is the page name.
31: * If the page name is also equal, then returns 0 for equality.
32: *
33: * @author jalkanen
34: */
35: // FIXME: Does not implement equals().
36: // FIXME3.0: move to util package
37: public class PageTimeComparator implements Comparator, Serializable {
38: private static final long serialVersionUID = 0L;
39:
40: static Logger log = Logger.getLogger(PageTimeComparator.class);
41:
42: /**
43: * {@inheritDoc}
44: */
45: public int compare(Object o1, Object o2) {
46: WikiPage w1 = (WikiPage) o1;
47: WikiPage w2 = (WikiPage) o2;
48:
49: if (w1 == null || w2 == null) {
50: log.error("W1 or W2 is NULL in PageTimeComparator!");
51: return 0; // FIXME: Is this correct?
52: }
53:
54: Date w1LastMod = w1.getLastModified();
55: Date w2LastMod = w2.getLastModified();
56:
57: if (w1LastMod == null) {
58: log.error("NULL MODIFY DATE WITH " + w1.getName());
59: return 0;
60: } else if (w2LastMod == null) {
61: log.error("NULL MODIFY DATE WITH " + w2.getName());
62: return 0;
63: }
64:
65: // This gets most recent on top
66: int timecomparison = w2LastMod.compareTo(w1LastMod);
67:
68: if (timecomparison == 0) {
69: return w1.getName().compareTo(w2.getName());
70: }
71:
72: return timecomparison;
73: }
74: }
|