i2p.susi.util

## Class Folder<O>

- 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(), add comparators with addSorter(), select a sort method with setSortBy(), and retrieve the current page with currentPageIterator(). All public methods are synchronized.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `Folder.SortOrder` Enumeration defining sort direction for folder elements.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `DEFAULT_PAGESIZE`
    `static String` | `PAGESIZE`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `Folder()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `addElement(O element)` Add an element only if it does not already exist
    `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.
    `void` | `addSorter(String id, Comparator<O> sorter)` Adds a new sorter to the folder.
    `Iterator<O>` | `currentPageIterator()` Returns an iterator containing the elements on the current page.
    `void` | `firstPage()` Sets folder to display first page.
    `int` | `getCurrentPage()` Returns the current page.
    `String` | `getCurrentSortBy()`
    `Folder.SortOrder` | `getCurrentSortingDirection()`
    `O` | `getFirstElement()` Returns the first element of the sorted folder.
    `O` | `getLastElement()` Returns the last element of the sorted folder.
    `O` | `getNextElement(O element)` Retrieves the next element in the sorted array.
    `int` | `getPageOf(O element)` Returns the page this element is on, using the current sort, or 1 if not found
    `int` | `getPages()` Returns the number of pages in the folder.
    `int` | `getPageSize()` Returns page size.
    `O` | `getPreviousElement(O element)` Retrieves the previous element in the sorted array.
    `int` | `getSize()` Returns the size of the folder.
    `boolean` | `isFirstElement(O element)` Returns true, if elements.equals( firstElementOfTheSortedArray ).
    `boolean` | `isFirstPage()` Returns true, if folder shows points to the first page.
    `boolean` | `isLastElement(O element)` Returns true, if elements.equals( lastElementOfTheSortedArray ).
    `boolean` | `isLastPage()` Returns true, if folder shows points to the last page.
    `void` | `lastPage()` Sets folder to display last page.
    `void` | `nextPage()` Turns folder to next page.
    `void` | `previousPage()` Turns folder to previous page.
    `void` | `removeElement(O element)` Remove an element
    `void` | `removeElements(Collection<O> elems)` Remove elements
    `void` | `setCurrentPage(int currentPage)` Sets the current page to the given parameter.
    `void` | `setElements(O[] elements)` Set the array of objects the folder should manage.
    `void` | `setPageSize(int pageSize)` Set page size.
    `void` | `setSortBy(String id, Folder.SortOrder direction)` Activates sorting by the choosen Comparator.
    `void` | `sort()` Sorts the elements according the order given by @link addSorter() and @link setSortBy().

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### DEFAULT_PAGESIZE

          public static final int DEFAULT_PAGESIZE

      See Also:
        Constant Field Values

    - #### PAGESIZE

          public static final String PAGESIZE

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### Folder

          public Folder()

  - ### Method Detail

    - #### addElement

          public boolean addElement(O element)

      Add an element only if it does not already exist
      Parameters:
        `element` - to add
      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` - to add
      Returns:
        number added

    - #### addSorter

          public void addSorter(String id,
                                Comparator<O> sorter)

      Adds a new sorter to the folder. You can sort the folder by calling setSortBy() and choose the given id there.
      Parameters:
        `id` - ID to identify the Comparator with @link setSortBy()
        `sorter` - a Comparator to sort the Array given by @link setElements()

    - #### 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.

    - #### firstPage

          public void firstPage()

      Sets folder to display first page.

    - #### getCurrentPage

          public int getCurrentPage()

      Returns the current page. Starts at 1, even if empty.
      Returns:
        Returns the current page.

    - #### getCurrentSortBy

          public String getCurrentSortBy()

    - #### getCurrentSortingDirection

          public Folder.SortOrder getCurrentSortingDirection()

    - #### getFirstElement

          public O getFirstElement()

      Returns the first element of the sorted folder.
      Returns:
        First element.

    - #### getLastElement

          public O getLastElement()

      Returns the last element of the sorted folder.
      Returns:
        Last element.

    - #### getNextElement

          public O getNextElement(O element)

      Retrieves the next element in the sorted array.
      Parameters:
        `element` -
      Returns:
        The next element

    - #### getPageOf

          public int getPageOf(O element)

      Returns the page this element is on, using the current sort, or 1 if not found
      Parameters:
        `element` -

    - #### getPages

          public int getPages()

      Returns the number of pages in the folder. Minimum of 1 even if empty.
      Returns:
        Returns the number of pages.

    - #### getPageSize

          public int getPageSize()

      Returns page size. If no page size has been set, it returns property @link PAGESIZE. If no property is set @link DEFAULT_PAGESIZE is returned.
      Returns:
        Returns the pageSize.

    - #### getPreviousElement

          public O getPreviousElement(O element)

      Retrieves the previous element in the sorted array.
      Parameters:
        `element` -
      Returns:
        The previous element

    - #### getSize

          public int getSize()

      Returns the size of the folder.
      Returns:
        Returns the size of the folder.

    - #### isFirstElement

          public boolean isFirstElement(O element)

      Returns true, if elements.equals( firstElementOfTheSortedArray ).
      Parameters:
        `element` -

    - #### isFirstPage

          public boolean isFirstPage()

      Returns true, if folder shows points to the first page.

    - #### isLastElement

          public boolean isLastElement(O element)

      Returns true, if elements.equals( lastElementOfTheSortedArray ).
      Parameters:
        `element` -

    - #### isLastPage

          public boolean isLastPage()

      Returns true, if folder shows points to the last page.

    - #### lastPage

          public void lastPage()

      Sets folder to display last page.

    - #### nextPage

          public void nextPage()

      Turns folder to next page.

    - #### previousPage

          public void previousPage()

      Turns folder to previous page.

    - #### removeElement

          public void removeElement(O element)

      Remove an element
      Parameters:
        `element` - to remove

    - #### removeElements

          public void removeElements(Collection<O> elems)

      Remove elements
      Parameters:
        `elems` - to remove

    - #### setCurrentPage

          public void setCurrentPage(int currentPage)

      Sets the current page to the given parameter. Starts at 1.
      Parameters:
        `currentPage` - The current page to set.

    - #### 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.

    - #### setPageSize

          public void setPageSize(int pageSize)

      Set page size.
      Parameters:
        `pageSize` - The page size to set.

    - #### setSortBy

          public void setSortBy(String id,
                                Folder.SortOrder direction)

      Activates sorting by the choosen Comparator. The id must match the one, which the Comparator has been stored in the folder with @link addSorter(). Sets the sorting direction of the folder. Warning, this does not do the actual sort, only addElements() and setElements() does a sort.
      Parameters:
        `id` - ID to identify the Comparator stored with @link addSorter()
        `direction` - UP or DOWN. UP is reverse sort.

    - #### sort

          public void sort()

      Sorts the elements according the order given by @link addSorter() and @link setSortBy().