//
//  PagerHelper.java
//  blogwt
//
//  Created by William Shakour (billy1380) on 28 Apr 2015.
//  Copyright © 2015 WillShex Limited. All rights reserved.
//
package com.willshex.blogwt.shared.helper;

import java.util.List;

import com.willshex.blogwt.shared.api.Pager;
import com.willshex.blogwt.shared.api.SortDirectionType;

/**
 * @author William Shakour (billy1380)
 *
 */
public class PagerHelper {

	public static Integer DEFAULT_START = Integer.valueOf(0);
	public static Integer DEFAULT_COUNT = Integer.valueOf(10);
	public static String DEFAULT_SORT_BY = null;

	public static void updatePager (Pager pager, List<?> list) {
		updatePager(pager, list, null);
	}

	public static void updatePager (Pager pager, List<?> list, Integer total) {
		if (list != null) {
			pager.start = Integer.valueOf(pager.start.intValue()
					+ (list.size()));
		} else {
			// list is null so do nothing
		}

		if (total != null) {
			pager.totalCount = total;
		}
	}

	public static Pager createInfinitePager () {
		return new Pager().start(DEFAULT_START).count(
				Integer.valueOf(Integer.MAX_VALUE));
	}

	public static Pager createDefaultPager () {
		return new Pager().start(DEFAULT_START).count(DEFAULT_COUNT)
				.sortBy(DEFAULT_SORT_BY)
				.sortDirection(SortDirectionType.SortDirectionTypeDescending);
	}

	public static Pager moveForward (Pager pager) {
		if (pager != null) {
			if (pager.start == null) {
				pager.start = DEFAULT_START;
			}

			if (pager.count == null) {
				pager.count = DEFAULT_COUNT;
			}

			pager.start = Integer.valueOf(pager.start.intValue()
					+ pager.count.intValue());

			if (pager.totalCount != null
					&& pager.totalCount.intValue() < pager.start.intValue()) {
				pager.start = Integer.valueOf(pager.totalCount.intValue()
						- pager.count.intValue());
			}

			if (pager.start < 0) {
				pager.start = DEFAULT_START;
			}
		}

		return pager;
	}

	public static Pager moveBackward (Pager pager) {
		if (pager != null) {
			if (pager.start == null) {
				pager.start = DEFAULT_START;
			}

			if (pager.count == null) {
				pager.count = DEFAULT_COUNT;
			}

			if (pager.start.intValue() > pager.count.intValue()) {
				pager.start = Integer.valueOf(pager.start.intValue()
						- pager.count.intValue());
			} else {
				pager.start = DEFAULT_START;
			}
		}

		return pager;
	}

	public static Pager reset (Pager pager) {
		if (pager != null) {
			pager.start(DEFAULT_START)
					.count(DEFAULT_COUNT)
					.sortBy(DEFAULT_SORT_BY)
					.sortDirection(
							SortDirectionType.SortDirectionTypeDescending);
		}

		return pager;
	}
}

@@ -566,16 +566,93 @@
  = null;
+%0A%09private static Integer CAP = Integer.valueOf(DEFAULT_COUNT.intValue() * 4);
 %0A%0A%09publi
@@ -860,32 +860,38 @@
 .start = Integer
+%0A%09%09%09%09%09
 .valueOf(pager.s
@@ -905,22 +905,17 @@
 tValue()
-%0A%09%09%09%09%09
+ 
 + (list.
@@ -1124,20 +1124,20 @@
 ART)
+%0A%09%09%09%09
 .count(
-%0A%09%09%09%09
 Inte
@@ -1607,32 +1607,38 @@
 .start = Integer
+%0A%09%09%09%09%09
 .valueOf(pager.s
@@ -1652,22 +1652,17 @@
 tValue()
-%0A%09%09%09%09%09
+ 
 + pager.
@@ -1807,24 +1807,31 @@
 ger.valueOf(
+%0A%09%09%09%09%09%09
 pager.totalC
@@ -1837,39 +1837,33 @@
 Count.intValue()
-%0A%09%09%09%09%09%09
+ 
 - pager.count.in
@@ -2263,32 +2263,39 @@
 Integer.valueOf(
+%0A%09%09%09%09%09%09
 pager.start.intV
@@ -2300,23 +2300,17 @@
 tValue()
-%0A%09%09%09%09%09%09
+ 
 - pager.
@@ -2498,22 +2498,16 @@
 T_START)
-%0A%09%09%09%09%09
 .count(D
@@ -2549,22 +2549,16 @@
 SORT_BY)
-%0A%09%09%09%09%09
 .sortDir
@@ -2621,24 +2621,170 @@
 ding);%0A%09%09%7D%0A%0A
+%09%09return pager;%0A%09%7D%0A%0A%09public static Pager capIfTooLarge (Pager pager) %7B%0A%09%09if (pager.count.intValue() %3E CAP.intValue()) %7B%0A%09%09%09pager.count(CAP);%0A%09%09%7D%0A%0A
 %09%09return pag

add cap to pager if needed