01: //
02: // Informa -- RSS Library for Java
03: // Copyright (c) 2002 by Niko Schmuck
04: //
05: // Niko Schmuck
06: // http://sourceforge.net/projects/informa
07: // mailto:niko_schmuck@users.sourceforge.net
08: //
09: // This library is free software.
10: //
11: // You may redistribute it and/or modify it under the terms of the GNU
12: // Lesser General Public License as published by the Free Software Foundation.
13: //
14: // Version 2.1 of the license should be included with this distribution in
15: // the file LICENSE. If the license is not included with this distribution,
16: // you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
17: // or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
18: // MA 02139 USA.
19: //
20: // This library is distributed in the hope that it will be useful,
21: // but WITHOUT ANY WARRANTY; without even the implied waranty of
22: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23: // Lesser General Public License for more details.
24: //
25:
26: // $Id: ItemComparator.java,v 1.8 2006/12/04 23:43:27 italobb Exp $
27:
28: package de.nava.informa.utils;
29:
30: import java.util.Comparator;
31:
32: import de.nava.informa.core.ItemIF;
33:
34: /**
35: * Custom comparator for ItemIF objects, which takes the date the news
36: * item was found into account.
37: *
38: * @author Niko Schmuck (niko@nava.de)
39: */
40: public class ItemComparator implements Comparator<ItemIF> {
41:
42: private boolean reverseOrder;
43: private boolean useFoundDate;
44:
45: /**
46: * Default constructor using ascending dates (oldes first) and using
47: * the date specified by the item (as opposed to the date the item
48: * was found by retrieving).
49: */
50: public ItemComparator() {
51: this (false, false);
52: }
53:
54: public ItemComparator(boolean reverseOrder) {
55: this (reverseOrder, false);
56: }
57:
58: public ItemComparator(boolean reverseOrder, boolean useFoundDate) {
59: this .reverseOrder = reverseOrder;
60: this .useFoundDate = useFoundDate;
61: }
62:
63: public int compare(ItemIF item1, ItemIF item2) {
64: int cmp = 0;
65: if (useFoundDate) {
66: if (item1.getFound() != null && item2.getFound() != null) {
67: cmp = item1.getFound().compareTo(item2.getFound());
68: }
69: } else {
70: if (item1.getDate() != null && item2.getDate() != null) {
71: cmp = item1.getDate().compareTo(item2.getDate());
72: }
73: }
74: if (reverseOrder) {
75: return -1 * cmp;
76: } else {
77: return cmp;
78: }
79: }
80:
81: public boolean getReverseOrder() {
82: return reverseOrder;
83: }
84:
85: public void setReverseOrder(boolean reverseOrder) {
86: this .reverseOrder = reverseOrder;
87: }
88:
89: public boolean getUseFoundDate() {
90: return useFoundDate;
91: }
92:
93: public void setUseFoundDate(boolean useFoundDate) {
94: this.useFoundDate = useFoundDate;
95: }
96:
97: }
|