Home » Software Development » A Code Kata: Pagination

About Ashley Frieze

Ashley Frieze
Software developer, stand-up comedian, musician, writer, jolly big cheer-monkey, skeptical thinker, Doctor Who fan, lover of fine sounds

A Code Kata: Pagination

Many frameworks provide support for pagination of results that come from a database. However, sometimes we need to implement this fairly common pattern.

Using TDD construct the pagination logic.

  1. Pages are numbered 1-based – e.g. 1, 2, 3, 4
  2. The user gets to choose the page size
  3. There is a default page size of 25
  4. Depending on the page we’re on, we need to know how many rows to skip in the database
  5. A default request is assumed to be for page 1
  6. We call into the pagination logic with – total records in the database, requested page number, requested page size – the latter of these can be blank to mean defaults
  7. The pagination logic returns page 1 if there are no records at all
  8. The pagination logic corrects the requested page number to bring it into range, if the selected page is out of range
  9. The pagination logic returns the definition of the page that the request implies – a page in range with its own number, page size, and number of rows to skip in the database

Example:

  • Select defaults when there are ten records – returns “Page 1, Page Size 25, Rows To Skip 0”
  • Select page 2 with page size 20 when there are 100 records – returns “Page 2, Page Size 20, Rows To Skip 20”
  • Select page 10 with page size 10 when there are 20 records – returns “Page 2, Page Size 10, Rows To Skip 10”

If you solve this, please leave a solution, in a language of your choice, including the tests, in the comments.

Published on Java Code Geeks with permission by Ashley Frieze, partner at our JCG program. See the original article here: A Code Kata: Pagination

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments