pageiterator.java
来自「一个非常好的FRAMWRK!是一个外国组织做的!不!」· Java 代码 · 共 227 行
JAVA
227 行
/**
* Copyright 2003-2005 the original author or authors.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jdon.controller.model;
import java.util.*;
import java.io.Serializable;
/**
* All model's ID collection of every page that will be displayed
* it carry the model's ID collection from persistence lay
* to presentation lay
*
* com.jdon.model.query.PageIteratorSolver supply a factory that
* create this class in persistence lay
*
* the class is stateful.
*
* <p>@author <a href="mailto:banqiao@jdon.com">banq</a></p>
*
* @see ModelListAction ModelListForm PageIteratorSolver
*/
public class PageIterator implements Iterator, Serializable {
public final static Object[] EMPTY = new Object[0];
/**
* the count of all models that fit for query condition
*/
private int allCount = 0;
/**
* all model's ID colletion of current page
*/
private Object[] keys;
/**
* when iterating current page, current record position.
*/
private int currentIndex = -1;
/**
* next record (model)
*/
private Object nextElement = null;
/**
* current page start sequence
*/
private int start;
/**
* has next page?
*/
boolean hasNext;
/**
* full construtor
* @param allCount int the count of all models that fit for query condition
* @param keys Object[] all model's ID colletion of current page
* @param start int current page start sequence
* @param hasNext boolean has next page?
*/
public PageIterator(int allCount, Object[] keys, int start, boolean hasNext) {
this.allCount = allCount;
this.keys = keys;
this.start = start;
this.hasNext = hasNext;
}
/**
* Every page construtor
*
* allCount must be enter later by setAllcount
*
*/
public PageIterator(Object[] keys, int start, boolean hasNext) {
this.allCount = 0;
this.keys = keys;
this.start = start;
this.hasNext = hasNext;
}
/**
* empty construtor
* this construtor can ensure the jsp view page don't happened nullException!
*
*/
public PageIterator() {
this.allCount = 0;
this.keys = EMPTY;
this.start = 0;
this.hasNext = false;
}
/**
* clone
* new is faster than clone:
* http://forums.java.net/jive/thread.jspa?forumID=23&threadID=743&messageID=16571
*/
public Object clone() {
PageIterator pi = new PageIterator(0, this.keys, this.start, this.hasNext);
pi.setAllCount(this.allCount);
return pi;
}
public int getAllCount() {
return allCount;
}
public void setAllCount(int allCount) {
this.allCount = allCount;
}
/**
* reset
*
*/
public void reset() {
currentIndex = -1;
nextElement = null;
}
/**
* 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 >= keys.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 < keys.length) {
currentIndex++;
Object element = keys[currentIndex];
if (element != null) {
return element;
}
}
return null;
}
public boolean isNextPageAvailable() {
return hasNext;
}
public boolean isPreviousPageAvailable() {
return start > 0;
}
public int getStartOfNextPage() {
return start + keys.length;
}
public int getStartOfPreviousPage() {
return Math.max(start - keys.length, 0);
}
public int getSize() {
return keys.length;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?