KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > utils > ItemComparator


1 //
2
// Informa -- RSS Library for Java
3
// Copyright (c) 2002 by Niko Schmuck
4
//
5
// Niko Schmuck
6
// http://sourceforge.net/projects/informa
7
// mailto:niko_schmuck@users.sourceforge.net
8
//
9
// 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
27 // $Id: ItemComparator.java,v 1.7 2003/04/06 00:03:27 niko_schmuck Exp $
28

29 package de.nava.informa.utils;
30
31 import java.util.Comparator JavaDoc;
32
33 import de.nava.informa.core.ItemIF;
34
35 /**
36  * Custom comparator for ItemIF objects, which takes the date the news
37  * item was found into account.
38  *
39  * @author Niko Schmuck (niko@nava.de)
40  */

41 public class ItemComparator implements Comparator JavaDoc {
42
43   private boolean reverseOrder;
44   private boolean useFoundDate;
45
46   /**
47    * Default constructor using ascending dates (oldes first) and using
48    * the date specified by the item (as opposed to the date the item
49    * was found by retrieving).
50    */

51   public ItemComparator() {
52     this(false, false);
53   }
54
55   public ItemComparator(boolean reverseOrder) {
56     this(reverseOrder, false);
57   }
58
59   public ItemComparator(boolean reverseOrder, boolean useFoundDate) {
60     this.reverseOrder = reverseOrder;
61     this.useFoundDate = useFoundDate;
62   }
63   
64   public int compare(Object JavaDoc obj1, Object JavaDoc obj2) {
65     if (obj1 instanceof ItemIF) {
66       ItemIF item1 = (ItemIF) obj1;
67       if (obj2 instanceof ItemIF) {
68         ItemIF item2 = (ItemIF) obj2;
69         int cmp = 0;
70         if (useFoundDate) {
71           if (item1.getFound() != null && item2.getFound() != null) {
72             cmp = item1.getFound().compareTo(item2.getFound());
73           }
74         } else {
75           if (item1.getDate() != null && item2.getDate() != null) {
76             cmp = item1.getDate().compareTo(item2.getDate());
77           }
78         }
79         if (reverseOrder) {
80           return -1 * cmp;
81         } else {
82           return cmp;
83         }
84       } else {
85         throw new IllegalArgumentException JavaDoc("Not instance of ItemIF " + obj2);
86       }
87     } else {
88       throw new IllegalArgumentException JavaDoc("Not instance of ItemIF " + obj1);
89     }
90   }
91
92   public boolean getReverseOrder() {
93     return reverseOrder;
94   }
95
96   public void setReverseOrder(boolean reverseOrder) {
97     this.reverseOrder = reverseOrder;
98   }
99   
100   public boolean getUseFoundDate() {
101     return useFoundDate;
102   }
103
104   public void setUseFoundDate(boolean useFoundDate) {
105     this.useFoundDate = useFoundDate;
106   }
107
108 }
109
Popular Tags