📄 abstractpagertool.java
字号:
public List getItems()
{
if (items == null)
{
items = getStoredItems();
}
return (items != null) ? items : Collections.EMPTY_LIST;
}
/**
* Returns the index of the last item on the current page of results
* (as determined by the current index, items per page, and
* the number of items). If there is no current page, then null is
* returned.
*
* @return index for the last item on this page or <code>null</code>
* if none exists
* @since VelocityTools 1.3
*/
public Integer getLastIndex()
{
if (!hasItems())
{
return null;
}
return new Integer(Math.min(getTotal() - 1, index + itemsPerPage - 1));
}
/**
* Returns the index for the next page of items
* (as determined by the current index, items per page, and
* the number of items). If no "next page" exists, then null is
* returned.
*
* @return index for the next page or <code>null</code> if none exists
*/
public Integer getNextIndex()
{
int next = index + itemsPerPage;
if (next < getTotal())
{
return new Integer(next);
}
return null;
}
/**
* Returns the index of the first item on the current page of results
* (as determined by the current index, items per page, and
* the number of items). If there is no current page, then null is
* returned. This is different than {@link #getIndex()} in that it
* is adjusted to fit the reality of the items available and is not a
* mere accessor for the current, user-set index value.
*
* @return index for the first item on this page or <code>null</code>
* if none exists
* @since VelocityTools 1.3
*/
public Integer getFirstIndex()
{
if (!hasItems())
{
return null;
}
return new Integer(Math.min(getTotal() - 1, index));
}
/**
* Return the index for the previous page of items
* (as determined by the current index, items per page, and
* the number of items). If no "next page" exists, then null is
* returned.
*
* @return index for the previous page or <code>null</code> if none exists
*/
public Integer getPrevIndex()
{
int prev = Math.min(index, getTotal()) - itemsPerPage;
if (index > 0)
{
return new Integer(Math.max(0, prev));
}
return null;
}
/**
* Returns the number of pages that can be made from this list
* given the set number of items per page.
*/
public int getPagesAvailable()
{
return (int)Math.ceil(getTotal() / (double)itemsPerPage);
}
/**
* Returns the current "page" of search items.
*
* @return a {@link List} of items for the "current page"
*/
public List getPage()
{
/* return null if we have no items */
if (!hasItems())
{
return null;
}
/* quietly keep the page indices to legal values for robustness' sake */
int start = getFirstIndex().intValue();
int end = getLastIndex().intValue() + 1;
return getItems().subList(start, end);
}
/**
* Returns the "page number" for the specified index. Because the page
* number is used for the user interface, the page numbers are 1-based.
*
* @param i the index that you want the page number for
* @return the approximate "page number" for the specified index or
* <code>null</code> if there are no items
*/
public Integer getPageNumber(int i)
{
if (!hasItems())
{
return null;
}
return new Integer(1 + i / itemsPerPage);
}
/**
* Returns the "page number" for the current index. Because the page
* number is used for the user interface, the page numbers are 1-based.
*
* @return the approximate "page number" for the current index or
* <code>null</code> if there are no items
*/
public Integer getPageNumber()
{
return getPageNumber(index);
}
/**
* Returns the total number of items available.
* @since VelocityTools 1.3
*/
public int getTotal()
{
if (!hasItems())
{
return 0;
}
return getItems().size();
}
/**
* <p>Returns a description of the current page. This implementation
* displays a 1-based range of result indices and the total number
* of items. (e.g. "1 - 10 of 42" or "7 of 7") If there are no items,
* this will return "0 of 0".</p>
*
* <p>Sub-classes may override this to provide a customized
* description (such as one in another language).</p>
*
* @return a description of the current page
*/
public String getPageDescription()
{
if (!hasItems())
{
return "0 of 0";
}
StringBuffer out = new StringBuffer();
int first = getFirstIndex().intValue() + 1;
int total = getTotal();
if (first >= total)
{
out.append(total);
out.append(" of ");
out.append(total);
}
else
{
int last = getLastIndex().intValue() + 1;
out.append(first);
out.append(" - ");
out.append(last);
out.append(" of ");
out.append(total);
}
return out.toString();
}
/**
* Returns a <b>S</b>liding <b>L</b>ist of <b>I</b>ndices for <b>P</b>ages
* of items.
*
* <p>Essentially, this returns a list of item indices that correspond
* to available pages of items (as based on the set items-per-page).
* This makes it relativly easy to do a google-ish set of links to
* available pages.</p>
*
* <p>Note that this list of Integers is 0-based to correspond with the
* underlying result indices and not the displayed page numbers (see
* {@link #getPageNumber}).</p>
*
* @return {@link List} of Integers representing the indices of result
* pages or empty list if there's one or less pages available
*/
public List getSlip()
{
/* return an empty list if there's no pages to list */
int totalPgs = getPagesAvailable();
if (totalPgs <= 1)
{
return Collections.EMPTY_LIST;
}
/* page number is 1-based so decrement it */
int curPg = getPageNumber().intValue() - 1;
/* start at zero or just under half of max slip size
* this keeps "forward" and "back" pages about even
* but gives preference to "forward" pages */
int slipStart = Math.max(0, (curPg - (slipSize / 2)));
/* push slip end as far as possible */
int slipEnd = Math.min(totalPgs, (slipStart + slipSize));
/* if we're out of "forward" pages, then push the
* slip start toward zero to maintain slip size */
if (slipEnd - slipStart < slipSize)
{
slipStart = Math.max(0, slipEnd - slipSize);
}
/* convert 0-based page numbers to indices and create list */
List slip = new ArrayList(slipEnd - slipStart);
for (int i=slipStart; i < slipEnd; i++)
{
slip.add(new Integer(i * itemsPerPage));
}
return slip;
}
/* ---------------------- protected methods ------------------------ */
/**
* Retrieves stored search items (if any) from the user's
* session attributes.
*
* @return the {@link List} retrieved from memory
*/
protected List getStoredItems()
{
if (session != null)
{
return (List)session.getAttribute(STORED_ITEMS_KEY);
}
return null;
}
/**
* Stores current search items in the user's session attributes
* (if one currently exists) in order to do efficient result pagination.
*
* <p>Override this to store search items somewhere besides the
* HttpSession or to prevent storage of items across requests. In
* the former situation, you must also override getStoredItems().</p>
*
* @param items the {@link List} to be stored
*/
protected void setStoredItems(List items)
{
if (session != null)
{
session.setAttribute(STORED_ITEMS_KEY, items);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -