KickJava   Java API By Example, From Geeks To Geeks.

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


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 // $Id: ChannelComparator.java,v 1.2 2003/09/23 20:34:05 niko_schmuck Exp $
27

28 package de.nava.informa.utils;
29
30 import java.util.Comparator JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import de.nava.informa.core.ChannelIF;
34
35 /**
36  * Custom comparator for ChannelIF objects. Not a traditional
37  * comparator as there are no order criteria for ChannelIF
38  * objects. However, there are three possible states:
39  * CHANNEL_MISMATCH: Two ChannelIF's objects are not equal
40  * according to their equals() method
41  * CHANNEL_IDENTICAL: Two ChannelIF's are equal according
42  * to their equals() method and contain the same ItemIF's
43  * CHANNEL_CHANGED: Two ChannelIF's are equal according to
44  * their equals() method, but contain different ItemIF's
45  *
46  * @author Jonathan Krebs (Jonathan.Krebs@ngc.com)
47  */

48 public final class ChannelComparator implements Comparator JavaDoc {
49
50   // ----- Valid channel comparison values
51

52   /** Channels are not the same */
53   public static final int CHANNEL_MISMATCH = -1;
54
55   /** Channels are the same but contain different items */
56   public static final int CHANNEL_IDENTICAL = 0;
57
58   /** Channels are the same and contain the same items */
59   public static final int CHANNEL_CHANGED = 1;
60
61   public ChannelComparator() {
62   }
63
64   public int compare(Object JavaDoc obj1, Object JavaDoc obj2) {
65
66     if (obj1 instanceof ChannelIF) {
67       ChannelIF channel1 = (ChannelIF) obj1;
68
69       if (obj2 instanceof ChannelIF) {
70         ChannelIF channel2 = (ChannelIF) obj2;
71
72         if (!channel1.equals(channel2)) {
73           return CHANNEL_MISMATCH;
74         }
75
76         if (channel1.getItems().size() != channel2.getItems().size()) {
77           return CHANNEL_CHANGED;
78         }
79
80         Iterator JavaDoc items = channel1.getItems().iterator();
81
82         while (items.hasNext()) {
83           if (!channel2.getItems().contains(items.next())) {
84             return CHANNEL_CHANGED;
85           }
86         }
87
88         return CHANNEL_IDENTICAL;
89
90       } else {
91         throw new IllegalArgumentException JavaDoc("Not instance of ChannelIF " + obj2);
92       }
93
94     } else {
95       throw new IllegalArgumentException JavaDoc("Not instance of ChannelIF " + obj1);
96     }
97   }
98
99 }
100
Popular Tags