Question

I have class Pager. And I need to put in lists in various types and I need to returns the same list types.

Code of Pager There is only constructor and getData method, that only get a subset and returns it.

public class Pager<E> {
private int page;
private int amount;
private List<E> list;

/**
 * Constructor
 * @param amount 
 * @param page 
 * @param list 
 */
public Pager(int page, int amount, List<E> list) {
    this.page = page;
    this.amount = amount;
    this.list = list;
}

/**
 * Returns data to counter
 * @return
 */
public List<E> getData() {
    List<E> result = new ArrayList<E>();

    int from = this.page * this.amount;
    int to = (this.page * this.amount) + this.amount;

    for (int i = from; i < to; i++) {
        result.add(this.list.get(i));
    }

    return result;
}

Method call I call pager with lists and then I need to put results back to the lists.

List<MyType1> list1 = ArrayList<Mytype1>();
List<MyType2> list2 = ArrayList<Mytype2>();
Pager pager = new Pager(
                    page,
                    amount,
                    list1;
                  );
list1 = pager.getData();


Pager pager = new Pager(
                    page,
                    amount,
                    list2
                  );

list2 = pager.getData();

So how can I make this pager generic to process various types of list?

Thanks for the help.

Was it helpful?

Solution

Add generic type parameters to your Pager variables:

List<MyType1> list1 = new ArrayList<Mytype1>();
List<MyType2> list2 = new ArrayList<Mytype2>();
Pager<MyType1> pager1 = new Pager<MyType1>(
                    page,
                    amount,
                    list1;
                  );
list1 = pager1.getData();


Pager<MyType2> pager2 = new Pager<MyType2>(
                    page,
                    amount,
                    list2
                  );

list2 = pager2.getData();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top