📄 pageiterator.java~1~
字号:
package com.jdon.estore.catalog.dao;import java.util.*;public class PageIterator implements Iterator { private Integer [] elements; private int currentIndex = -1; private Object nextElement = null; private DatabaseObjectFactory objectFactory; private ServiceLocator serviceLocator = ServiceLocator.getInstance(); /** * Creates a new DatabaseObjectIterator. The type must be one of the * following: */ public PageIterator(int type, Integer [] elements, final Object extraObject) { this.elements = elements; // Load the appropriate proxy factory depending on the type of object // that we're iterating through. switch (type) { case SiteDefs.USER: // Create an objectFactory to load users. this.objectFactory = new DatabaseObjectFactory() { ReaderDAO reader = (ReaderDAO)extraObject; public Object loadObject(Integer id) { try { mysite.ejb.dao.model.User user = reader.getUser(id.intValue()); return user; }catch (Exception unfe) { } return null; } }; break; // Otherwise, an invalid value was passed in so throw an exception. default: throw new IllegalArgumentException("Illegal type specified"); } } /** * Returns true if there are more elements in the iteration. * * @return true if the iterator has more elements. */ public boolean hasNext() { // If we are at the end of the list, there can't be any more elements // to iterate through. if (currentIndex+1 >= elements.length && nextElement == null) { return false; } // Otherwise, see if nextElement is null. If so, try to load the next // element to make sure it exists. if (nextElement == null) { nextElement = getNextElement(); if (nextElement == null) { return false; } } return true; } /** * Returns the next element. * * @return the next element. * @throws NoSuchElementException if there are no more elements. */ public Object next() throws java.util.NoSuchElementException { Object element = null; if (nextElement != null) { element = nextElement; nextElement = null; } else { element = getNextElement(); if (element == null) { throw new java.util.NoSuchElementException(); } } return element; } /** * Not supported for security reasons. */ public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } /** * Returns the next available element, or null if there are no more * elements to return. * * @return the next available element. */ public Object getNextElement() { while (currentIndex+1 < elements.length) { currentIndex++; Object element = objectFactory.loadObject(elements[currentIndex]); if (element != null) { return element; } } return null; }}/** * An interface for loading Jive database objects. */interface DatabaseObjectFactory { /** * Returns the object associated with <code>id</code> or null if the * object could not be loaded. * * @param id the id of the object to load. * @return the object specified by <code>id</code> or null if it could not * be loaded. */ public Object loadObject(Integer id);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -