org.bisen.chatamari.service.JdbcPage.java Source code

Java tutorial

Introduction

Here is the source code for org.bisen.chatamari.service.JdbcPage.java

Source

/*
 * Copyright 2014 asikprad.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.bisen.chatamari.service;

import java.util.Iterator;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

/**
 *
 * @author asikprad
 * @param <T>
 */
public class JdbcPage<T> implements Page<T> {

    private final Pageable pageable;
    private final int totalPages;
    private final int totalElements;
    private final List<T> content;

    public JdbcPage(Pageable pageable, int totalPages, int totalElements, List<T> content) {
        super();
        this.pageable = pageable;
        this.totalPages = totalPages;
        this.totalElements = totalElements;
        this.content = content;
    }

    @Override
    public int getNumber() {
        return pageable.getPageNumber();
    }

    @Override
    public int getSize() {
        return pageable.getPageSize();
    }

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

    //number on current page
    @Override
    public int getNumberOfElements() {
        return this.getContent().size();
    }

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

    @Override
    public Iterator<T> iterator() {
        return content.iterator();
    }

    @Override
    public List<T> getContent() {
        return content;
    }

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

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

    @Override
    public boolean isFirst() {
        return pageable.getPageNumber() == 0;
    }

    @Override
    public boolean isLast() {
        return pageable.getPageNumber() == totalPages;
    }

    @Override
    public boolean hasNext() {
        return pageable.getPageNumber() != totalPages;
    }

    @Override
    public boolean hasPrevious() {
        return pageable.getPageNumber() != 0;
    }

    @Override
    public Pageable nextPageable() {
        return pageable.next();
    }

    @Override
    public Pageable previousPageable() {
        return pageable.previousOrFirst();
    }
}