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

de.knightsoftnet.gwtp.spring.shared.data.DeserializeablePage Maven / Gradle / Ivy

There is a newer version: 2.2.2
Show newest version
package de.knightsoftnet.gwtp.spring.shared.data;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Deserializeable {@code Page} implementation.
 *
 * @param  the type of which the page consists.
 */
public class DeserializeablePage implements Page {

  private int number;
  private int size;
  private int totalPages;
  private int numberOfElements;
  private long totalElements;
  private boolean empty;
  private boolean first;
  private boolean last;
  private List content;
  private Sort sort;
  // private Pageable pageable;

  /**
   * constructor.
   *
   * @param content content list
   * @param pageable pageable element
   * @param total total elements
   */
  @SuppressWarnings("PMD.UnusedFormalParameter")
  public DeserializeablePage(final List content, final Pageable pageable, final long total) {
    this.content = content;
    // this.pageable = pageable;
    this.totalElements = total;
  }

  public DeserializeablePage(final List content) {
    this(content, Pageable.unpaged(), null == content ? 0 : content.size());
  }

  public DeserializeablePage() {
    this(new ArrayList());
  }

  public PageImpl pageImpl() {
    return new PageImpl<>(getContent(), PageRequest.of(getNumber(), getSize(), getSort()),
        getTotalElements());
  }

  @Override
  public final int getNumber() {
    return this.number;
  }

  public final void setNumber(final int number) {
    this.number = number;
  }

  @Override
  public final int getSize() {
    return this.size;
  }

  public final void setSize(final int size) {
    this.size = size;
  }

  @Override
  public final int getTotalPages() {
    return this.totalPages;
  }

  public final void setTotalPages(final int totalPages) {
    this.totalPages = totalPages;
  }

  @Override
  public final int getNumberOfElements() {
    return this.numberOfElements;
  }

  public final void setNumberOfElements(final int numberOfElements) {
    this.numberOfElements = numberOfElements;
  }

  @Override
  public final long getTotalElements() {
    return this.totalElements;
  }

  public final void setTotalElements(final long totalElements) {
    this.totalElements = totalElements;
  }

  @Override
  public boolean isEmpty() {
    return empty;
  }

  public void setEmpty(final boolean empty) {
    this.empty = empty;
  }

  @Override
  public final boolean isFirst() {
    return this.first;
  }

  public final void setFirst(final boolean first) {
    this.first = first;
  }

  @Override
  public final boolean isLast() {
    return this.last;
  }

  public final void setLast(final boolean last) {
    this.last = last;
  }

  @Override
  public final List getContent() {
    return this.content;
  }

  public final void setContent(final List pcontent) {
    this.content = pcontent;
  }

  @Override
  public final Sort getSort() {
    return this.sort;
  }

  public final void setSort(final Sort sort) {
    this.sort = sort;
  }

  // public final Pageable getPageable() {
  // return this.pageable;
  // }
  //
  // public final void setPageable(Pageable ppageable) {
  // this.pageable = ppageable;
  // }

  @Override
  public boolean hasContent() {
    return !CollectionUtils.isEmpty(content);
  }

  @Override
  public boolean hasNext() {
    return getNumber() + 1 < this.getTotalPages();
  }

  @Override
  public boolean hasPrevious() {
    return getNumber() > 0;
  }

  @Override
  public Pageable nextPageable() {
    // return hasNext() ? this.pageable.next() : Pageable.unpaged();
    return null;
  }

  @Override
  public Pageable previousPageable() {
    // return this.hasPrevious() ? this.pageable.previousOrFirst() : Pageable.unpaged();
    return null;
  }

  @Override
  public Iterator iterator() {
    return this.content.iterator();
  }

  @Override
  public  Page map(final Function converter) {
    return new DeserializeablePage<>(this.getConvertedContent(converter), getPageable(),
        this.totalElements);
  }

  /**
   * Applies the given {@link Function} to the content of the Chunk.
   *
   * @param converter must not be {@literal null}.
   * @param  type of the entries
   * @return list of converted entries
   */
  protected  List getConvertedContent(final Function converter) {

    Assert.notNull(converter, "Function must not be null!");

    return stream().map(converter::apply).collect(Collectors.toList());
  }
}