KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > utils > cleaner > CleanerWorkerThread


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
// $Id: CleanerWorkerThread.java,v 1.1 2004/08/24 18:07:54 spyromus Exp $
26
//
27

28 package de.nava.informa.utils.cleaner;
29
30 import de.nava.informa.utils.toolkit.WorkerThread;
31 import de.nava.informa.utils.toolkit.ChannelRecord;
32 import de.nava.informa.core.ChannelIF;
33 import de.nava.informa.core.ItemIF;
34
35 /**
36  * Worker thread performing cleaning operations over channels.
37  * <p>
38  * Processing of channel consists of checking every item with registered matcher.
39  * If checker says that the item matches the rule then there will be event thrown
40  * to the observer.</p>
41  *
42  * @author Aleksey Gureev (spyromus@noizeramp.com)
43  *
44  * @see WorkerThread
45  */

46 public class CleanerWorkerThread extends WorkerThread {
47
48   private CleanerObserverIF observer;
49   private CleanerMatcherIF matcher;
50
51   /**
52    * Creates new worker thread object.
53    *
54    * @param observer observer of the cleaner.
55    * @param matcher matcher of the cleaner.
56    */

57   public CleanerWorkerThread(CleanerObserverIF observer, CleanerMatcherIF matcher) {
58     super("Cleaner - Worker thread");
59
60     this.observer = observer;
61     this.matcher = matcher;
62   }
63
64   /**
65    * Processes record.
66    *
67    * @param record record to process.
68    */

69   protected final void processRecord(ChannelRecord record) {
70     if (matcher != null && observer != null) {
71       final ChannelIF channel = record.getChannel();
72
73       observer.cleaningStarted(channel);
74
75       final ItemIF[] items = (ItemIF[]) channel.getItems().toArray(new ItemIF[0]);
76       for (int i = 0; i < items.length; i++) {
77         checkItem(items[i], channel);
78       }
79
80       observer.cleaningFinished(channel);
81     }
82   }
83
84   /**
85    * Checks single item in matcher and notifies observer if it's unwanted.
86    *
87    * @param item item to check.
88    * @param channel channel where the item resides.
89    */

90   private void checkItem(ItemIF item, ChannelIF channel) {
91     if (matcher.isMatching(item, channel)) {
92       try {
93         observer.unwantedItem(item, channel);
94       } catch (Exception JavaDoc e) {
95         // Take care of unexpected exceptions in observers.
96
}
97     }
98   }
99 }
100
Popular Tags