001: /*
002: * NEMESIS-FORUM.
003: * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
004: *
005: * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
006: *
007: * Copyright (C) 2001 Yasna.com. All rights reserved.
008: *
009: * Copyright (C) 2000 CoolServlets.com. All rights reserved.
010: *
011: * NEMESIS-FORUM. is free software; you can redistribute it and/or
012: * modify it under the terms of the Apache Software License, Version 1.1,
013: * or (at your option) any later version.
014: *
015: * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
016: * application are parts of NEMESIS-FORUM and are distributed under
017: * same terms of licence.
018: *
019: *
020: * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
021: * and software developed by CoolServlets.com (http://www.coolservlets.com).
022: * and software developed by Yasna.com (http://www.yasna.com).
023: *
024: */
025: package org.nemesis.forum.search;
026:
027: import java.util.Iterator;
028: import java.util.NoSuchElementException;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.nemesis.forum.Forum;
033: import org.nemesis.forum.Message;
034:
035: /**
036: * Iterator for database query results.
037: */
038: public class QueryIterator implements Iterator {
039:
040: static protected Log log = LogFactory.getLog(QueryIterator.class);
041: //maintain an array of message ids to iterator through.
042: private int[] messages;
043: //points to the current message id that the user has iterated to.
044: private int currentIndex = -1;
045:
046: private int maxIndex;
047: private Forum forum;
048:
049: /**
050: * Creates a new query iterator using an array of message id's. The
051: * ForumFactory is used to lookup the actual message objects.
052: *
053: * @param messages the array of message id's to iterate through.
054: * @param factory a DbForumFactory to obtain actual message objects from.
055: */
056: public QueryIterator(Forum forum, int[] messages) {
057: this .messages = messages;
058: this .forum = forum;
059: maxIndex = messages.length;
060: }
061:
062: /**
063: * Creates a new query iterator using an array of message id's. The
064: * ForumFactory is used to lookup the actual message objects. A start index
065: * and number of results limit the scope of the Iterator to a subset of the
066: * message array. However, if the start index or number of results does
067: * not fall into the bounds of the message array, the Iterator may return
068: * fewer results than the number indicated by the numResults paramater.
069: *
070: * @param messages the array of message id's to iterate through.
071: * @param factory a DbForumFactory to obtain actual message objects from.
072: * @param startIndex a starting index in the messages array for the Iterator.
073: * @param numResults the max number of results the Iterator should provide.
074: */
075: public QueryIterator(Forum forum, int[] messages, int startIndex,
076: int numResults) {
077: this .messages = messages;
078: this .forum = forum;
079: currentIndex = startIndex - 1;
080: maxIndex = startIndex + numResults;
081: }
082:
083: /**
084: * Returns true if there are more messages left to iteratate through.
085: */
086: public boolean hasNext() {
087: return (currentIndex + 1 < messages.length && currentIndex + 1 < maxIndex);
088: }
089:
090: /**
091: * Returns the next message.
092: */
093: public Object next() throws NoSuchElementException {
094: Message message = null;
095: currentIndex++;
096: if (currentIndex >= messages.length) {
097: throw new NoSuchElementException();
098: }
099: 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 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();
117: }
118: }
|