📄 pageiterator.java
字号:
/**
* Copyright 2005 Jdon.com
* 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 IDs of eache page that will be displayed
*
* <p>@author <a href="mailto:banqiao@jdon.com">banq</a></p>
* <p>@version JdonFramework 2005 v1.0</p>
*/
public class PageIterator implements Iterator, Serializable {
public final static Object[] EMPTY = new Object[0];
private int allCount = 0;
//一个页面中的所有元素
private Object[] keys;
private int currentIndex = -1;
private Object nextElement = null;
//以上参数是对keys内所有元素实现遍历
int start;
boolean hasNext;
//以上两个参数是有关页为单位的信息
/**
*
* allCount 是PageIterator重要参数
* @param keys Object[]
* @param start int
* @param hasNext boolean
*/
public PageIterator(int allCount, Object[] keys, int start, boolean hasNext) {
this.allCount = allCount;
this.keys = keys;
this.start = start;
this.hasNext = hasNext;
}
/**
*
* allCount 是PageIterator重要参数
* 因为其赋值比较灵活,可能拖后,故本构造函数未纳入。
*
* @param keys Object[]
* @param start int
* @param hasNext boolean
*/
public PageIterator(Object[] keys, int start, boolean hasNext) {
this.allCount = 0;
this.keys = keys;
this.start = start;
this.hasNext = hasNext;
}
public PageIterator() {
this.allCount = 0;
this.keys = EMPTY;
this.start = 0;
this.hasNext = false;
}
/**
* 复制自身 因为本类有指针,多用户处理时会冲突。
* @return Object
*/
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;
}
//复位 以便再次使用
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -