Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6929899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:22:37+00:00 2026-05-27T11:22:37+00:00

I’m working on a pagination system for my java project, and I’d like to

  • 0

I’m working on a pagination system for my java project, and I’d like to make it generic for my various JPA Models.

The problem (as far as I know) is that if I use generics, I will have to somehow cast the returned final value to work on it. How can I avoid that ?

Here’s my code so far (absolutely not generic!) :

public interface Paginator {
    public void setLimit(Integer limit);
    public Page page(Integer page);
}


public class PicturesPaginator implements Paginator {
    private Integer limit = 10;
    private JPAQuery query;
    private Long quantity;

    public PicturesPaginator(String query, Object... params) {
        this.query = Picture.find(query, params);
        this.quantity = Picture.count(query, params);
    }

    @Override
    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    @Override
    public PicturesPage page(Integer page) {
        if (page == null)
            page = 1;

        List<Picture> pictures = query.fetch(page, limit);
        return new PicturesPage(pictures, quantity, page, limit);
    }
}


public abstract class Page {
    protected List<Picture> pictures;
    protected Long quantity;
    protected Integer page;
    protected Integer limit;

    public List<Picture> list() {
        return pictures;
    }

    public Long count() {
        return quantity;
    }

    public boolean hasNext() {
        return (page * limit > quantity);
    }

    public boolean hasPrevious() {
        return (page != 1);
    }

    public boolean hasOtherPages() {
        return (hasNext() || hasPrevious());
    }

    public Integer nextPageNumber() {
        if (!hasNext())
            return null;

        return (page + 1);
    }

    public Integer previousPageNumber() {
        if (!hasPrevious())
            return null;

        return (page - 1);
    }

    public Integer currentPageNumber() {
        return page;
    }
}


public class PicturesPage extends Page {
    public PicturesPage(List<Picture> pictures, Long quantity, Integer page, Integer limit) {
        this.pictures = pictures;
        this.quantity = quantity;
        this.page = page;
        this.limit = limit;
    }
}

I would like to get rid of those PicturesPaginator and PicturesPage and make it generic, but the list() method from the abstract class Page would return a generic List (List<T> or List<GenericModel> since I use Play here).
What I would expect is this list() method to return the correct List, aka List<Picture> in my case. Is this possible ?

Note: I now there is a module for pagination in Play! Framework, my question is mainly for understanding more about java too 🙂

Thank you very much for your help, I really appreciate!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:22:37+00:00Added an answer on May 27, 2026 at 11:22 am

    You can view my Play–Pagination module’s source code to see how I handle this type of thing. I put my source on github.

    What you want to do is make Page generic as well, and probably non-abstract:

    public class Page<T> {
      public List<T> list() {}
    }
    

    And instead of PicturesPage you could just do:

    new Page<Picture>()
    

    The Paginator interface would also need to be generified:

    public interface Paginator {
        public Page<T> page(Integer page);
    }
    

    Generifying PicturesPaginator would be harder since you invoke methods on the Picture class. Java’s generics implementation erases types at runtime, so you’ll have to deal with type tokens and reflection.

    public abstract class GenericPaginator<T> {
      public GenericPaginator() {
        Class<T> typeToken = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        // use reflection to invoke the finders methods
      }
    }
    public class PicturesPaginator extends GenericPaginator<Picture> {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.