All Downloads are FREE. Search and download functionalities are using the official Maven repository.

generator.server.pagination.domain.main.AppPage.mustache Maven / Gradle / Ivy

There is a newer version: 1.22.0
Show newest version
package {{packageName}}.shared.pagination.domain;

import java.util.List;
import java.util.function.Function;
import {{packageName}}.shared.collection.domain.{{baseName}}Collections;
import {{packageName}}.shared.error.domain.Assert;

public final class {{baseName}}Page {

  private static final int MINIMAL_PAGE_COUNT = 1;

  private final List content;
  private final int currentPage;
  private final int pageSize;
  private final long totalElementsCount;

  private {{baseName}}Page({{baseName}}PageBuilder builder) {
    content = {{baseName}}Collections.immutable(builder.content);
    currentPage = builder.currentPage;
    pageSize = buildPageSize(builder.pageSize);
    totalElementsCount = buildTotalElementsCount(builder.totalElementsCount);
  }

  private int buildPageSize(int pageSize) {
    if (pageSize == -1) {
      return content.size();
    }

    return pageSize;
  }

  private long buildTotalElementsCount(long totalElementsCount) {
    if (totalElementsCount == -1) {
      return content.size();
    }

    return totalElementsCount;
  }

  public static  {{baseName}}Page singlePage(List content) {
    return builder(content).build();
  }

  public static  {{baseName}}PageBuilder builder(List content) {
    return new {{baseName}}PageBuilder<>(content);
  }

  public static  {{baseName}}Page of(List elements, {{baseName}}Pageable pagination) {
    Assert.notNull("elements", elements);
    Assert.notNull("pagination", pagination);

    List content = elements.subList(
      Math.min(pagination.offset(), elements.size()),
      Math.min(pagination.offset() + pagination.pageSize(), elements.size())
    );

    return builder(content).currentPage(pagination.page()).pageSize(pagination.pageSize()).totalElementsCount(elements.size()).build();
  }

  public List content() {
    return content;
  }

  public int currentPage() {
    return currentPage;
  }

  public int pageSize() {
    return pageSize;
  }

  public long totalElementsCount() {
    return totalElementsCount;
  }

  public int pageCount() {
    if (totalElementsCount > 0) {
      return (int) Math.ceil(totalElementsCount / (float) pageSize);
    }

    return MINIMAL_PAGE_COUNT;
  }

  public boolean hasPrevious() {
    return currentPage > 0;
  }

  public boolean hasNext() {
    return isNotLast();
  }

  public boolean isNotLast() {
    return currentPage + 1 < pageCount();
  }

  public  {{baseName}}Page map(Function mapper) {
    Assert.notNull("mapper", mapper);

    return builder(content().stream().map(mapper).toList())
      .currentPage(currentPage)
      .pageSize(pageSize)
      .totalElementsCount(totalElementsCount)
      .build();
  }

  public static final class {{baseName}}PageBuilder {

    private final List content;
    private int currentPage;
    private int pageSize = -1;
    private long totalElementsCount = -1;

    private {{baseName}}PageBuilder(List content) {
      this.content = content;
    }

    public {{baseName}}PageBuilder pageSize(int pageSize) {
      this.pageSize = pageSize;

      return this;
    }

    public {{baseName}}PageBuilder currentPage(int currentPage) {
      this.currentPage = currentPage;

      return this;
    }

    public {{baseName}}PageBuilder totalElementsCount(long totalElementsCount) {
      this.totalElementsCount = totalElementsCount;

      return this;
    }

    public {{baseName}}Page build() {
      return new {{baseName}}Page<>(this);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy