java.lang.Object
i2p.susi.util.Folder<O>
- Type Parameters:
O- type of objects stored in this folder
Manages an Object array with paging and sorting support. Create a folder, set contents with
setElements(O[]), add comparators with addSorter(), select a sort method with setSortBy(String, Folder.SortOrder), and retrieve the current page with currentPageIterator(). All public methods are synchronized.Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnumeration defining sort direction for folder elements.Field Summary
Modifier and TypeFieldDescriptionprivate intThe page currently being shown.private Comparator<O> The sorter currently applied.private StringId of the sorter currently applied.static final intDefault page size for folder display.private O[]The elements in the folder, in the current sort order.private intHow many pages of messages the folder has.private intMessages shown on one page.static final StringProperty key for page size configuration.private final Map<String, Comparator<O>> The available sorters, by id.private Folder.SortOrderThe direction elements are currently sorted in.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionbooleanaddElement(O element) Add an element only if it does not already exist.intaddElements(List<O> elems) Add elements only if they do not already exist.voidaddSorter(String id, Comparator<O> sorter) Adds a new sorter to the folder.Returns an iterator containing the elements on the current page.voidSets folder to display first page.intReturns the current page number.Get the ID of the current sort comparator.Get the current sorting direction.private OgetElement(int i) Retrieves element at index i.Returns the first element of the sorted folder.private intgetIndexOf(O element) Gets index of an element in the array regardless of sorting direction.Returns the last element of the sorted folder.getNextElement(O element) Retrieves the next element in the sorted array.intReturns the page this element is on, using the current sort, or 1 if not found.intgetPages()Returns the number of pages in the folder.intReturns page size.getPreviousElement(O element) Retrieves the previous element in the sorted array.intgetSize()Returns the number of elements in the folder.booleanisFirstElement(O element) Returns true if the element is the first in the sorted array.booleanReturns true if the current page is the first page.booleanisLastElement(O element) Returns true if the element is the last in the sorted array.booleanReturns true if the current page is the last page.voidlastPage()Sets folder to display last page.voidnextPage()Turns folder to next page.voidTurns folder to previous page.voidremoveElement(O element) Remove an element from the folder.voidremoveElements(Collection<O> elems) Remove elements from the folder.voidsetCurrentPage(int currentPage) Set the current page number.voidsetElements(O[] elements) Set the array of objects the folder should manage.voidsetPageSize(int pageSize) Set page size and recalculate page counts.voidsetSortBy(String id, Folder.SortOrder direction) Activates sorting by the chosen Comparator.voidsort()Sorts the elements according to the order given byaddSorter(String, Comparator)andsetSortBy(String, SortOrder).private voidupdate()Recalculates variables.
Field Details
PAGESIZE
Property key for page size configuration.- See Also:
DEFAULT_PAGESIZE
public static final int DEFAULT_PAGESIZEDefault page size for folder display.- See Also:
pages
private int pagesHow many pages of messages the folder has.pageSize
private int pageSizeMessages shown on one page.currentPage
private int currentPageThe page currently being shown.elements
The elements in the folder, in the current sort order.sorter
The available sorters, by id.sortingDirection
The direction elements are currently sorted in.currentSorter
The sorter currently applied.currentSortID
Id of the sorter currently applied.
Constructor Details
Folder
public Folder()Creates an empty folder.
Method Details
getCurrentPage
public int getCurrentPage()Returns the current page number. Starts at 1, even if empty.- Returns:
- the current page number
setCurrentPage
public void setCurrentPage(int currentPage) Set the current page number. Starts at 1.- Parameters:
currentPage- the page number to set (1-based)
getSize
public int getSize()Returns the number of elements in the folder.- Returns:
- the number of elements
getPages
public int getPages()Returns the number of pages in the folder. Minimum of 1 even if empty.- Returns:
- the number of pages
getPageSize
public int getPageSize()Returns page size. If no page size has been set, returns thePAGESIZEproperty value, orDEFAULT_PAGESIZE.- Returns:
- the page size
setPageSize
public void setPageSize(int pageSize) Set page size and recalculate page counts.- Parameters:
pageSize- the new page size
update
private void update()Recalculates variables.sort
public void sort()Sorts the elements according to the order given byaddSorter(String, Comparator)andsetSortBy(String, SortOrder).setElements
Set the array of objects the folder should manage. Does NOT copy the array. Sorts the array if a sorter set.- Parameters:
elements- Array of Os.
removeElement
Remove an element from the folder.- Parameters:
element- the entry to drop, matched with equals()
removeElements
Remove elements from the folder.- Parameters:
elems- the collection of elements to remove
addElement
Add an element only if it does not already exist.- Parameters:
element- the entry to insert unless an equal one is already present- Returns:
- true if added
addElements
currentPageIterator
Returns an iterator containing the elements on the current page. This iterator is over a copy of the current page, and so is thread safe w.r.t. other operations on this folder, but will not reflect subsequent changes, and iter.remove() will not change the folder.- Returns:
- Iterator containing the elements on the current page.
nextPage
public void nextPage()Turns folder to next page.previousPage
public void previousPage()Turns folder to previous page.firstPage
public void firstPage()Sets folder to display first page.lastPage
public void lastPage()Sets folder to display last page.addSorter
Adds a new sorter to the folder. You can sort the folder by callingsetSortBy(String, SortOrder)with the given id.- Parameters:
id- identifier for this comparator, used withsetSortBy(String, SortOrder)sorter- a Comparator to sort the elements
setSortBy
Activates sorting by the chosen Comparator. The id must match the one stored withaddSorter(String, Comparator). Set the sorting direction of the folder. Warning: this does not do the actual sort, onlyaddElements(List)and setElements() do.- Parameters:
id- identifier for the Comparatordirection- UP or DOWN. UP is reverse sort.
getCurrentSortBy
Get the ID of the current sort comparator.- Returns:
- the current sort ID, or null if none
getCurrentSortingDirection
Get the current sorting direction.- Returns:
- the current SortOrder (UP or DOWN)
getFirstElement
Returns the first element of the sorted folder.- Returns:
- the first element in sort order, or null if the folder is empty
getLastElement
Returns the last element of the sorted folder.- Returns:
- the last element in sort order, or null if the folder is empty
getIndexOf
Gets index of an element in the array regardless of sorting direction.- Parameters:
element- the entry to locate, compared with equals()- Returns:
- the index, or -1 if not found
getNextElement
getPreviousElement
getElement
Retrieves element at index i.- Parameters:
i- the index into the backing array, counting from zero- Returns:
- the element, or null if empty
isLastPage
public boolean isLastPage()Returns true if the current page is the last page.- Returns:
- true if on the last page
isFirstPage
public boolean isFirstPage()Returns true if the current page is the first page.- Returns:
- true if on the first page
isLastElement
Returns true if the element is the last in the sorted array.- Parameters:
element- the entry to compare against the last one- Returns:
- true if the element is the last
isFirstElement
Returns true if the element is the first in the sorted array.- Parameters:
element- the entry to compare against the first one- Returns:
- true if the element is the first
getPageOf
Returns the page this element is on, using the current sort, or 1 if not found.- Parameters:
element- the entry whose page is wanted- Returns:
- the page number (1-based)