KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > search > QueryIterator


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25 package org.nemesis.forum.search;
26
27 import java.util.Iterator JavaDoc;
28 import java.util.NoSuchElementException JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.nemesis.forum.Forum;
33 import org.nemesis.forum.Message;
34
35 /**
36  * Iterator for database query results.
37  */

38 public class QueryIterator implements Iterator JavaDoc {
39
40     static protected Log log = LogFactory.getLog(QueryIterator.class);
41     //maintain an array of message ids to iterator through.
42
private int[] messages;
43     //points to the current message id that the user has iterated to.
44
private int currentIndex = -1;
45
46     private int maxIndex;
47     private Forum forum;
48
49
50     /**
51      * Creates a new query iterator using an array of message id's. The
52      * ForumFactory is used to lookup the actual message objects.
53      *
54      * @param messages the array of message id's to iterate through.
55      * @param factory a DbForumFactory to obtain actual message objects from.
56      */

57     public QueryIterator(Forum forum,int[] messages) {
58         this.messages = messages;
59         this.forum=forum;
60         maxIndex = messages.length;
61     }
62
63     /**
64      * Creates a new query iterator using an array of message id's. The
65      * ForumFactory is used to lookup the actual message objects. A start index
66      * and number of results limit the scope of the Iterator to a subset of the
67      * message array. However, if the start index or number of results does
68      * not fall into the bounds of the message array, the Iterator may return
69      * fewer results than the number indicated by the numResults paramater.
70      *
71      * @param messages the array of message id's to iterate through.
72      * @param factory a DbForumFactory to obtain actual message objects from.
73      * @param startIndex a starting index in the messages array for the Iterator.
74      * @param numResults the max number of results the Iterator should provide.
75      */

76     public QueryIterator(Forum forum,int[] messages, int startIndex, int numResults) {
77         this.messages = messages;
78         this.forum=forum;
79         currentIndex = startIndex - 1;
80         maxIndex = startIndex + numResults;
81     }
82
83     /**
84      * Returns true if there are more messages left to iteratate through.
85      */

86     public boolean hasNext() {
87         return (currentIndex + 1 < messages.length && currentIndex + 1 < maxIndex);
88     }
89
90     /**
91      * Returns the next message.
92      */

93     public Object JavaDoc next() throws NoSuchElementException JavaDoc {
94         Message message = null;
95         currentIndex++;
96         if (currentIndex >= messages.length) {
97             throw new NoSuchElementException JavaDoc();
98         }
99         try {
100             int messageID = messages[currentIndex];
101             message = forum.getMessage(messageID);
102             //Now, get the message from the it's thread so that filters are
103
//applied to the message. This may seem a bit convuluted, but is
104
//necessary.
105
message = message.getForumThread().getMessage(messageID);
106         } catch (Exception JavaDoc e) {
107             log.error("", e);
108         }
109         return message;
110     }
111
112     /**
113      * For security reasons, the remove operation is not supported.
114      */

115     public void remove() {
116         throw new UnsupportedOperationException JavaDoc();
117     }
118 }
119
Popular Tags