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

com.appslandia.plum.base.PagerModel Maven / Gradle / Ivy

// The MIT License (MIT)
// Copyright © 2015 AppsLandia. All rights reserved.

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package com.appslandia.plum.base;

import java.util.ArrayList;
import java.util.List;

import com.appslandia.common.utils.Asserts;
import com.appslandia.common.utils.ValueUtils;

/**
 *
 * @author Loc Ha
 *
 */
public class PagerModel {

  public static final String REQUEST_ATTRIBUTE_ID = "pagerModel";

  final int pageIndex;
  final int recordCount;

  final int pageCount;
  final List items;

  public PagerModel(int pageIndex, int recordCount, int pageSize) {
    this(pageIndex, recordCount, pageSize, PagerSize.SIZE_9);
  }

  public PagerModel(int pageIndex, int recordCount, int pageSize, PagerSize pagerSize) {
    Asserts.isTrue(pageIndex >= 1);
    Asserts.isTrue(recordCount >= 0);
    Asserts.isTrue(pageSize > 0);
    Asserts.notNull(pagerSize);

    this.pageIndex = pageIndex;
    this.recordCount = recordCount;
    this.pageCount = (recordCount > 0) ? (int) Math.ceil(recordCount / (double) pageSize) : 1;

    // Why 6? PREV + 1L + dotL & dotR + 1R + NEXT
    int beginIndex = ValueUtils.valueOrMin(pageIndex - ((pagerSize.size - 6) / 2), 1);
    int endIndex = ValueUtils.valueOrMax(pageIndex + ((pagerSize.size - 6) / 2), this.pageCount);

    // PREV
    List items = new ArrayList<>();
    if (pageIndex > 1) {
      items.add(new PagerItem(PagerItem.TYPE_PREV, pageIndex - 1));
    }
    boolean dotL = false, dotR = false;

    // 1 ...
    if (beginIndex == 2) {
      items.add(new PagerItem(PagerItem.TYPE_INDEX, 1));
    } else if (beginIndex > 2) {
      items.add(new PagerItem(PagerItem.TYPE_INDEX, 1));
      items.add(new PagerItem(PagerItem.TYPE_DOT, -1));
      dotL = true;
    }

    // Indexes
    for (int index = beginIndex; index <= endIndex; index++) {
      items.add(new PagerItem(PagerItem.TYPE_INDEX, index));
    }

    // ... N
    if (endIndex == this.pageCount - 1) {
      items.add(new PagerItem(PagerItem.TYPE_INDEX, this.pageCount));
    } else if (endIndex < this.pageCount - 1) {
      items.add(new PagerItem(PagerItem.TYPE_DOT, -1));
      items.add(new PagerItem(PagerItem.TYPE_INDEX, this.pageCount));
      dotR = true;
    }

    // NEXT
    if (pageIndex < this.pageCount) {
      items.add(new PagerItem(PagerItem.TYPE_NEXT, pageIndex + 1));
    }

    // Add more items
    if (dotR) {
      int addIndex = endIndex + 1;
      while ((items.size() < pagerSize.size) && (addIndex + 1 < this.pageCount)) {
        items.add(items.indexOf(new PagerItem(PagerItem.TYPE_INDEX, addIndex - 1)) + 1,
            new PagerItem(PagerItem.TYPE_INDEX, addIndex));
        addIndex++;
      }
    }
    if (dotL) {
      int addIndex = beginIndex - 1;
      while ((items.size() < pagerSize.size) && (addIndex - 1 > 1)) {
        items.add(items.indexOf(new PagerItem(PagerItem.TYPE_INDEX, addIndex + 1)),
            new PagerItem(PagerItem.TYPE_INDEX, addIndex));
        addIndex--;
      }
    }

    // Replace dots
    for (int idx = 0; idx < items.size(); idx++) {
      PagerItem curItem = items.get(idx);
      if (curItem.isDotType()) {

        PagerItem before = items.get(idx - 1);
        PagerItem after = items.get(idx + 1);

        if (before.getIndex() + 2 == after.getIndex()) {
          items.set(idx, new PagerItem(PagerItem.TYPE_INDEX, before.getIndex() + 1));
        }
      }
    }
    this.items = items;
  }

  public int getPageIndex() {
    return this.pageIndex;
  }

  public int getRecordCount() {
    return this.recordCount;
  }

  public int getPageCount() {
    return this.pageCount;
  }

  public List getItems() {
    return this.items;
  }

  public enum PagerSize {
    SIZE_9(9), SIZE_11(11);

    final int size;

    private PagerSize(int size) {
      this.size = size;
    }

    public int size() {
      return this.size;
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy