<commit_before>package org.pm4j.core.pm.pageable;

import java.awt.print.Pageable;
import java.util.List;

/**
 * Some common algorithms that should not be implemented redundantly for each
 * kind of {@link Pageable}.
 *
 * @author <NAME>
 */
public final class PageableCollectionUtil {

  /**
   * @param pageable
   *          The set to get the result for.
   * @return The index of the first item on the current page.
   *         <p>
   *         Starts with one.<br>
   *         Is zero if there are no items in this set.
   */
  public static int getIdxOfFirstItemOnPage(PageableCollection<?> pageable) {
    int pageSize = pageable.getPageSize();
    return (pageSize > 0)
            ? (pageSize * (pageable.getCurrentPageIdx()-1)) + 1
            : 0;
  }

  /**
   * @param pageable
   *          The set to get the result for.
   * @return The index of the last item on the current page.
   *         <p>
   *         Starts with one.<br>
   *         Is zero if there are no items in this set.
   */
  public static int getIdxOfLastItemOnPage(PageableCollection<?> pageable) {
    int pageSize = pageable.getPageSize();
    return (pageSize > 0)
            ? Math.min(
                    getIdxOfFirstItemOnPage(pageable) + pageSize - 1,
                    pageable.getNumOfItems())
            : 0;
  }

  /**
   *
   * @param pageable The {@link PageableCollection} to evaluate.
   * @param itemPos The position to get the page number for. Starts with <i>One</i>.
   * @return The page number for the given item position. <i>One</i> for the first page.
   */
  public static int getPageForItemPos(PageableCollection<?> pageable, int itemPos) {
    int pageSize = pageable.getPageSize();
    return ((itemPos-1) / pageSize) + 1;<commit_msg>bugfix: index of first item for an empty collection should be zero.<commit_after>package org.pm4j.core.pm.pageable;

import java.awt.print.Pageable;
import java.util.List;

/**
 * Some common algorithms that should not be implemented redundantly for each
 * kind of {@link Pageable}.
 *
 * @author <NAME>
 */
public final class PageableCollectionUtil {

  /**
   * @param pageable
   *          The set to get the result for.
   * @return The index of the first item on the current page.
   *         <p>
   *         Starts with one.<br>
   *         Is zero if there are no items in this set.
   */
  public static int getIdxOfFirstItemOnPage(PageableCollection<?> pageable) {
    int pageSize = pageable.getPageSize();
    return (pageable.getNumOfItems() > 0)
            ? (pageSize * (pageable.getCurrentPageIdx()-1)) + 1
            : 0;
  }

  /**
   * @param pageable
   *          The set to get the result for.
   * @return The index of the last item on the current page.
   *         <p>
   *         Starts with one.<br>
   *         Is zero if there are no items in this set.
   */
  public static int getIdxOfLastItemOnPage(PageableCollection<?> pageable) {
    int pageSize = pageable.getPageSize();
    return (pageable.getNumOfItems() > 0)
            ? Math.min(
                    getIdxOfFirstItemOnPage(pageable) + pageSize - 1,
                    pageable.getNumOfItems())
            : 0;
  }

  /**
   *
   * @param pageable The {@link PageableCollection} to evaluate.
   * @param itemPos The position to get the page number for. Starts with <i>One</i>.
   * @return The page number for the given item position. <i>One</i> for the first page.
   */
  public static int getPageForItemPos(PageableCollection<?> pageable, int itemPos) {
    int pageSize = pageable.getPageSize();
    return ((itemPos-1) / pageSize) + 1;
{'fraction_non_alphanumeric': 0.10272071071626875, 'fraction_numerical': 0.00555247084952804, 'mean_word_length': 2.3704396632366698, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6745659', 'n_tokens_mistral': 1100, 'n_tokens_neox': 1080, 'n_words': 360}