java.lang.Object
i2p.susi.util.Folder<O>
Type Parameters:
O - type of objects stored in this folder

public class Folder<O>extends Object
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.
  • Field Details

    • PAGESIZE

      public static final String PAGESIZE
      Property key for page size configuration.
      See Also:
    • DEFAULT_PAGESIZE

      public static final int DEFAULT_PAGESIZE
      Default page size for folder display.
      See Also:
    • pages

      private int pages
      How many pages of messages the folder has.
    • pageSize

      private int pageSize
      Messages shown on one page.
    • currentPage

      private int currentPage
      The page currently being shown.
    • elements

      private O[] elements
      The elements in the folder, in the current sort order.
    • sorter

      private final Map<String, Comparator<O>> sorter
      The available sorters, by id.
    • sortingDirection

      private Folder.SortOrder sortingDirection
      The direction elements are currently sorted in.
    • currentSorter

      private Comparator<O> currentSorter
      The sorter currently applied.
    • currentSortID

      private String 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 the PAGESIZE property value, or DEFAULT_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 by addSorter(String, Comparator) and setSortBy(String, SortOrder).
    • setElements

      public void setElements(O[] elements)
      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

      public void removeElement(O element)
      Remove an element from the folder.
      Parameters:
      element - the entry to drop, matched with equals()
    • removeElements

      public void removeElements(Collection<O> elems)
      Remove elements from the folder.
      Parameters:
      elems - the collection of elements to remove
    • addElement

      public boolean addElement(O element)
      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

      public int addElements(List<O> elems)
      Add elements only if they do not already exist. Re-sorts the array if a sorter is set and any elements are actually added.
      Parameters:
      elems - the list of elements to add
      Returns:
      number of elements added
    • currentPageIterator

      public Iterator<O> 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

      public void addSorter(String id, Comparator<O> sorter)
      Adds a new sorter to the folder. You can sort the folder by calling setSortBy(String, SortOrder) with the given id.
      Parameters:
      id - identifier for this comparator, used with setSortBy(String, SortOrder)
      sorter - a Comparator to sort the elements
    • setSortBy

      public void setSortBy(String id, Folder.SortOrder direction)
      Activates sorting by the chosen Comparator. The id must match the one stored with addSorter(String, Comparator). Set the sorting direction of the folder. Warning: this does not do the actual sort, only addElements(List) and setElements() do.
      Parameters:
      id - identifier for the Comparator
      direction - UP or DOWN. UP is reverse sort.
    • getCurrentSortBy

      public String getCurrentSortBy()
      Get the ID of the current sort comparator.
      Returns:
      the current sort ID, or null if none
    • getCurrentSortingDirection

      public Folder.SortOrder getCurrentSortingDirection()
      Get the current sorting direction.
      Returns:
      the current SortOrder (UP or DOWN)
    • getFirstElement

      public O getFirstElement()
      Returns the first element of the sorted folder.
      Returns:
      the first element in sort order, or null if the folder is empty
    • getLastElement

      public O getLastElement()
      Returns the last element of the sorted folder.
      Returns:
      the last element in sort order, or null if the folder is empty
    • getIndexOf

      private int getIndexOf(O element)
      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

      public O getNextElement(O element)
      Retrieves the next element in the sorted array.
      Parameters:
      element - the current element
      Returns:
      the next element, or null if this is the last
    • getPreviousElement

      public O getPreviousElement(O element)
      Retrieves the previous element in the sorted array.
      Parameters:
      element - the current element
      Returns:
      the previous element, or null if this is the first
    • getElement

      private O getElement(int i)
      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

      public boolean isLastElement(O element)
      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

      public boolean isFirstElement(O element)
      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

      public int getPageOf(O element)
      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)