Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Pager fixes

  • Loading branch information...
commit 2ab636e47a0a8cc4d6e552cf7d5145db6e8f5710 1 parent 88e6a4c
nk authored
View
62 src/main/java/gr/dsigned/springcrudutils/Pager.java
@@ -1,5 +1,7 @@
package gr.dsigned.springcrudutils;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
@@ -10,20 +12,20 @@
public class Pager {
String baseURL;
- Long totalItemCount;
- Long currentPage;
+ int totalItemCount;
+ int currentPage;
int perPage;
- private List<String> urls = new ArrayList<String>();
+ private List<String> urls;
- public Pager(String baseURL, Long totalItemCount) {
+ public Pager(String baseURL, int totalItemCount) {
this.baseURL = baseURL;
this.totalItemCount = totalItemCount;
- this.currentPage = 0L;
+ this.currentPage = 0;
this.perPage = 10;
init();
}
- public Pager(String baseURL, Long totalItemCount, Long currentPage) {
+ public Pager(String baseURL, int totalItemCount, int currentPage) {
this.baseURL = baseURL;
this.totalItemCount = totalItemCount;
this.currentPage = currentPage;
@@ -31,7 +33,7 @@ public Pager(String baseURL, Long totalItemCount, Long currentPage) {
init();
}
- public Pager(String baseURL, Long totalItemCount, Long currentPage, int perPage) {
+ public Pager(String baseURL, int totalItemCount, int currentPage, int perPage) {
this.baseURL = baseURL;
this.totalItemCount = totalItemCount;
this.currentPage = currentPage;
@@ -40,22 +42,38 @@ public Pager(String baseURL, Long totalItemCount, Long currentPage, int perPage)
}
private void init() {
+ List<String> list = new ArrayList<String>();
for (int i = 0; i < getTotalPageNumber(); i++) {
if (i != 0) {
String url = getBaseURL() + "?page=" + i + "&sizeNo=" + perPage;
- urls.add(url);
+ list.add(url);
} else {
String url = getBaseURL() + "?sizeNo=" + perPage;
- urls.add(url);
+ list.add(url);
}
-
}
+ urls = ImmutableList.copyOf(list);
}
public String getFirstPage() {
return baseURL + "?sizeNo=" + perPage;
}
+ public String getNextPage(){
+ if(currentPage + 1 > urls.size()){
+ return getLastPage();
+ } else {
+ return urls.get(currentPage+1);
+ }
+ }
+ public String getPreviousPage(){
+ if(currentPage-1 > urls.size()){
+ return getFirstPage();
+ } else {
+ return urls.get(currentPage-1);
+ }
+ }
+
public String getLastPage() {
return baseURL + "?page=" + getTotalPageNumber() + "&sizeNo=" + perPage;
}
@@ -68,15 +86,14 @@ public String getBaseURL() {
return baseURL;
}
- public void setBaseURL(String baseURL) {
- this.baseURL = baseURL;
- }
- public Long getCurrentPage() {
+
+ public int getCurrentPage() {
return currentPage;
}
- public void setCurrentPage(Long currentPage) {
+ public void setCurrentPage(int currentPage) {
+ Preconditions.checkPositionIndex(currentPage, getTotalPageNumber(), "Page number out of bounds.");
this.currentPage = currentPage;
}
@@ -84,20 +101,19 @@ public int getPerPage() {
return perPage;
}
- public void setPerPage(int perPage) {
- this.perPage = perPage;
- }
- public Long getTotalItemCount() {
+ public int getTotalItemCount() {
return totalItemCount;
}
- public void setTotalItemCount(Long totalItemCount) {
- this.totalItemCount = totalItemCount;
- }
public List<String> getUrls() {
return urls;
}
-
+
+ @Override
+ public String toString() {
+ return "Pager{" + "baseURL=" + baseURL + ", totalItemCount=" + totalItemCount + ", currentPage=" + currentPage + ", perPage=" + perPage + ", urls=" + urls + '}';
+ }
+
}
View
2  src/main/java/gr/dsigned/springcrudutils/ParameterizedCRUDController.java
@@ -32,7 +32,7 @@ public String list(Integer page, Integer size, Model model, HttpServletRequest r
int pageNo = page == null ? 0 : (page.intValue());
int offset = pageNo * sizeNo;
Long itemNo = dao.countAll(entityClass);
- Pager pager = new Pager(request.getRequestURI(), itemNo.longValue(), Integer.valueOf(pageNo).longValue(), sizeNo);
+ Pager pager = new Pager(request.getRequestURI(), itemNo.intValue(), pageNo, sizeNo);
model.addAttribute("itemNo", itemNo);
model.addAttribute("sizeNo", sizeNo);
model.addAttribute("pageNo", pageNo);
View
144 src/test/java/gr/dsigned/springcrudutils/PagerTest.java
@@ -0,0 +1,144 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package gr.dsigned.springcrudutils;
+
+import org.junit.Before;
+import java.util.List;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author nk
+ */
+public class PagerTest {
+
+ Pager instance = new Pager("/", 100, 2, 10);
+
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ /**
+ * Test of getFirstPage method, of class Pager.
+ */
+ @Test
+ public void testGetFirstPage() {
+ System.out.println("getFirstPage");
+ String expResult = "/?sizeNo=10";
+ String result = instance.getFirstPage();
+ assertEquals(expResult, result);
+ }
+
+ /**
+ * Test of getNextPage method, of class Pager.
+ */
+ @Test
+ public void testGetNextPage() {
+ System.out.println("getNextPage");
+ String expResult = "/?page=3&sizeNo=10";
+ String result = instance.getNextPage();
+ assertEquals(expResult, result);
+ }
+
+ /**
+ * Test of getLastPage method, of class Pager.
+ */
+ @Test
+ public void testGetLastPage() {
+ System.out.println("getLastPage");
+ String expResult = "/?page=10&sizeNo=10";
+ String result = instance.getLastPage();
+ assertEquals(expResult, result);
+ }
+
+ /**
+ * Test of getTotalPageNumber method, of class Pager.
+ */
+ @Test
+ public void testGetTotalPageNumber() {
+ System.out.println("getTotalPageNumber");
+ int expResult = 10;
+ int result = instance.getTotalPageNumber();
+ assertEquals(expResult, result);
+ }
+
+ /**
+ * Test of getBaseURL method, of class Pager.
+ */
+ @Test
+ public void testGetBaseURL() {
+ System.out.println("getBaseURL");
+ String expResult = "/";
+ String result = instance.getBaseURL();
+ assertEquals(expResult, result);
+ }
+
+
+ /**
+ * Test of getCurrentPage method, of class Pager.
+ */
+ @Test
+ public void testGetCurrentPage() {
+ System.out.println("getCurrentPage");
+ int expResult = 2;
+ int result = instance.getCurrentPage();
+ assertEquals(expResult, result);
+ }
+
+ /**
+ * Test of setCurrentPage method, of class Pager.
+ */
+ @Test(expected=java.lang.IndexOutOfBoundsException.class)
+ public void testSetCurrentPage() {
+ System.out.println("setCurrentPage");
+ int currentPage = 11;
+ instance.setCurrentPage(currentPage);
+ }
+
+ /**
+ * Test of getTotalItemCount method, of class Pager.
+ */
+ @Test
+ public void testGetTotalItemCount() {
+ System.out.println("getTotalItemCount");
+ int expResult = 100;
+ int result = instance.getTotalItemCount();
+ assertEquals(expResult, result);
+ }
+
+ /**
+ * Test of getUrls method, of class Pager.
+ */
+ @Test
+ public void testGetUrls() {
+ System.out.println("getUrls");
+ List result = instance.getUrls();
+ assertEquals(10, result.size());
+ System.out.println("Urls: " + instance.toString());
+ }
+
+ /**
+ * Test of getPreviousPage method, of class Pager.
+ */
+ @Test
+ public void testGetPreviousPage() {
+ System.out.println("getPreviousPage");
+ String expResult = "/?page=1&sizeNo=10";
+ String result = instance.getPreviousPage();
+ assertEquals(expResult, result);
+ }
+
+ /**
+ * Test of getPerPage method, of class Pager.
+ */
+ @Test
+ public void testGetPerPage() {
+ System.out.println("getPerPage");
+ int expResult = 10;
+ int result = instance.getPerPage();
+ assertEquals(expResult, result);
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.