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

com.labs64.netlicensing.domain.vo.PageImpl Maven / Gradle / Ivy

There is a newer version: 2.10.0-jdk17
Show newest version
/* 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 com.labs64.netlicensing.domain.vo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * Basic {@code Page} implementation.
 * 
 * @param 
 *            the type of which the page consists.
 */
public class PageImpl implements Page, Serializable {

    private static final long serialVersionUID = 867755906494344406L;

    private final List content = new ArrayList();
    private final int pageNumber;
    private final int itemsNumber;
    private final int totalPages;
    private final long totalItems;
    private final boolean hasNext;

    /**
     * Constructor of {@code PageImpl}.
     * 
     * @param content
     *            the content of this page, must not be {@literal null}.
     * @param pageNumber
     *            the number of the current page
     * @param itemsNumber
     *            the number of elements on the page
     * @param totalPages
     *            the number of total pages
     * @param totalItems
     *            the total amount of elements
     * @param hasNext
     *            is there a next page exists
     */
    public PageImpl(final List content, final int pageNumber, final int itemsNumber, final int totalPages,
            final long totalItems, final boolean hasNext) {
        assert content != null : "Content must not be null!";

        this.content.addAll(content);

        this.pageNumber = pageNumber;
        this.itemsNumber = itemsNumber;
        this.totalPages = totalPages;
        this.totalItems = totalItems;
        this.hasNext = hasNext;
    }

    /**
     * Safe create instance of {@code Page}.
     * 
     * @param content
     *            the content of this page, must not be {@literal null}.
     * @param pageNumber
     *            the number of the current page
     * @param itemsNumber
     *            the number of elements on the page
     * @param totalPages
     *            the number of total pages
     * @param totalItems
     *            the total amount of elements
     * @param hasNext
     *            is there a next page exists
     * @param 
     *            type of page entity
     */
    public static  PageImpl createInstance(final List content,
            final String pageNumber, final String itemsNumber,
            final String totalPages, final String totalItems, final String hasNext) {
        try {
            return new PageImpl(content,
                    Integer.valueOf(pageNumber),
                    Integer.valueOf(itemsNumber),
                    Integer.valueOf(totalPages),
                    Long.valueOf(totalItems),
                    Boolean.valueOf(hasNext));
        } catch (Exception e) {
            return new PageImpl(content, 0, 0, 0, 0, false);
        }
    }

    @Override
    public int getPageNumber() {
        return pageNumber;
    }

    @Override
    public int getItemsNumber() {
        return itemsNumber;
    }

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

    @Override
    public long getTotalItems() {
        return totalItems;
    }

    @Override
    public boolean hasNext() {
        return hasNext;
    }

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

    @Override
    public List getContent() {
        return Collections.unmodifiableList(content);
    }

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

    @Override
    public String toString() {
        String contentType = "UNKNOWN";
        List content = getContent();

        if (hasContent()) {
            contentType = content.get(0).getClass().getName();
        }

        return String.format("Page %s of %d containing %s instances", getPageNumber(), getTotalPages(), contentType);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy