forumblockiterator.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 253 行
JAVA
253 行
/** * $RCSfile: ForumBlockIterator.java,v $ * $Revision: 1.3 $ * $Date: 2002/07/26 00:04:03 $ * * Copyright (C) 1999-2001 CoolServlets, Inc. All rights reserved. * * This software is the proprietary information of CoolServlets, Inc. * Use is subject to license terms. */package com.jivesoftware.forum.database;import com.jivesoftware.forum.*;import java.util.*;/** * Iterates through a set of forums. */public class ForumBlockIterator implements Iterator { private long [] forumBlock; private int blockID; private int blockStart; private String query; private int startIndex; private int currentIndex; private int endIndex; private long categoryID; private DbForumFactory factory; private Object previousElement = null; private Object nextElement = null; /** * Constructs a new ForumBlockIterator. * * @param forumBlock the starting forumBlock of elements to iterate * through. * @param query the SQL query corresponding to this iteration. * @param startIndex the starting index of the iteration. * @param endIndex the ending index of the iteration. * @param categoryID the category to load forum blocks from. * @param factory a ForumFactory to load data from. */ protected ForumBlockIterator(long [] forumBlock, String query, int startIndex, int endIndex, long categoryID, DbForumFactory factory) { this.forumBlock = forumBlock; this.blockID = startIndex / DbForumCategory.BLOCK_SIZE; this.blockStart = blockID * DbForumCategory.BLOCK_SIZE; this.query = query; this.currentIndex = startIndex - 1; this.startIndex = startIndex; this.endIndex = endIndex; this.categoryID = categoryID; this.factory = factory; } public boolean hasNext() { // If we are at the end of the list there are no more elements. if (currentIndex == endIndex) { return false; } // Otherwise, see if nextElement is null. If so, try to load the next // element to make sure it exists. If nextElement isn't null, that // means we've already checked to make sure it exists. if (nextElement == null) { nextElement = getNextElement(); // If getting the next element failed, return false. if (nextElement == null) { return false; } } return true; } public boolean hasPrevious() { // If we are at the start of the list there are no previous elements. if (currentIndex == startIndex) { return false; } // Otherwise, see if previousElement is null. If so, try to load the // previous element to make sure it exists. if (previousElement == null) { previousElement = getPreviousElement(); // If getting the previous element failed, return false. if (previousElement == null) { return false; } } return true; } public Object next() throws java.util.NoSuchElementException { Object element = null; if (nextElement != null) { element = nextElement; this.nextElement = null; } else { element = getNextElement(); if (element == null) { throw new java.util.NoSuchElementException(); } } return element; } public Object previous() { Object element = null; if (previousElement != null) { element = previousElement; previousElement = null; } else { element = getPreviousElement(); if (element == null) { throw new java.util.NoSuchElementException(); } } return element; } public void remove() { throw new UnsupportedOperationException(); } public void setIndex(Forum forum) { // Set nextElement and previousElement to null since we may be moving // the index. nextElement = null; previousElement = null; // Get the forum object for loading thread blocks try { DbForumCategory category = factory.cacheManager.getForumCategory(categoryID); // Scan through all blocks looking for forum. long forumID = forum.getID(); long [] currentBlock; for (int i=startIndex; i<endIndex; i++) { currentBlock = category.getBlock(query, i); if (currentBlock.length == 0) { throw new NoSuchElementException("Forum with id " + forumID + " is not a valid index in the iteration."); } int blockID = i / DbForumCategory.BLOCK_SIZE; int blockEnd = blockID * DbForumCategory.BLOCK_SIZE + DbForumCategory.BLOCK_SIZE; // If in first block of threads if (startIndex < blockEnd) { // If we are in the first block, j should start at the // start index instead of the beginning of the block. for (int j=startIndex%DbForumCategory.BLOCK_SIZE; j<currentBlock.length; j++, i++) { if (currentBlock[j] == forumID) { this.currentIndex = i; return; } } } // Otherwise, not in first block so start looking at beginning else { for (int j=0; j<currentBlock.length; j++, i++) { if (currentBlock[j] == forumID) { this.currentIndex = i; return; } } } } } catch (ForumCategoryNotFoundException e) { } // Got this far, so we never found the the element. throw new NoSuchElementException("Forum with id " + forum.getID() + " is not a valid index in the iteration."); } /** * Returns the next element, or null if there are no more * elements to return. * * @return the next element. */ private Object getNextElement() { previousElement = null; Object element = null; while (currentIndex+1 < endIndex && element == null) { currentIndex++; element = getElement(currentIndex); } return element; } /** * Returns the previous element, or null if there are no more elements * to return. * * @return the previous element. */ private Object getPreviousElement() { nextElement = null; Object element = null; while (currentIndex >= startIndex && element == null) { currentIndex--; element = getElement(currentIndex); } return element; } private Object getElement(int index) { if (index < 0) { return null; } // See if element isn't in the current thread block if (index < blockStart || index >= blockStart + DbForumCategory.BLOCK_SIZE) { // Then load up the appropriate block try { DbForumCategory category = factory.cacheManager.getForumCategory(categoryID); this.forumBlock = category.getBlock(query, index); this.blockID = index / DbForumCategory.BLOCK_SIZE; this.blockStart = blockID * DbForumCategory.BLOCK_SIZE; } catch (ForumCategoryNotFoundException fnfe) { // If we get this exception, it probably means the entire category // has been deleted, which means we should return null. return null; } } Object element = null; // Compute the relative index of the element, which is the index in the // current forum block. int relativeIndex = index % DbForumCategory.BLOCK_SIZE; // Make sure index isn't too large if (relativeIndex < forumBlock.length) { try { // Load the actual forum object element = factory.cacheManager.getForum(forumBlock[relativeIndex]); } catch (ForumNotFoundException fnfe) { } } return element; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?