Skip to main content
added possible approaches
Source Link
AgentX
  • 131
  • 1
  • 1
  • 4
  1. The one described above. Where the PaginatedList would expose a way to fetch next pages (batch of elements or List<>)
  2. Let the Library expose 2 API's - PaginatedList fetchData() -> client would call this one initially PaginatedList fetchNextPage(PaginatedList) -> client would call this for sub-sequent pages
  3. This one is slightly modified version of approach 2. Let library expose a modified fetchData() PaginatedList fetchData(PaginatedList list) -> initially client would need to create a new PaginatedList(), which would initialize it's state like pageToProcess = 0; This API kind of takes in the metadata associated with the service call to identify which page to get next
  1. The one described above. Where the PaginatedList would expose a way to fetch next pages (batch of elements or List<>)
  2. Let the Library expose 2 API's -

PaginatedList fetchData() -> client would call this one initially
PaginatedList fetchNextPage(PaginatedList) -> client would call this for sub-sequent pages

  1. This one is slightly modified version of approach 2. Let library expose a modified fetchData()

    PaginatedList fetchData(PaginatedList list) -> initially client would need to create a new PaginatedList(), which would initialize it's state like pageToProcess = 0; This API kind of takes in the metadata associated with the service call to identify which page to get next

  1. The one described above. Where the PaginatedList would expose a way to fetch next pages (batch of elements or List<>)
  2. Let the Library expose 2 API's - PaginatedList fetchData() -> client would call this one initially PaginatedList fetchNextPage(PaginatedList) -> client would call this for sub-sequent pages
  3. This one is slightly modified version of approach 2. Let library expose a modified fetchData() PaginatedList fetchData(PaginatedList list) -> initially client would need to create a new PaginatedList(), which would initialize it's state like pageToProcess = 0; This API kind of takes in the metadata associated with the service call to identify which page to get next
  1. The one described above. Where the PaginatedList would expose a way to fetch next pages (batch of elements or List<>)
  2. Let the Library expose 2 API's -

PaginatedList fetchData() -> client would call this one initially
PaginatedList fetchNextPage(PaginatedList) -> client would call this for sub-sequent pages

  1. This one is slightly modified version of approach 2. Let library expose a modified fetchData()

    PaginatedList fetchData(PaginatedList list) -> initially client would need to create a new PaginatedList(), which would initialize it's state like pageToProcess = 0; This API kind of takes in the metadata associated with the service call to identify which page to get next

added 794 characters in body
Source Link
AgentX
  • 131
  • 1
  • 1
  • 4

Is this is okay,Edit:-

3 straightforward approaches come to my mind -

  1. The one described above. Where the PaginatedList would expose a way to fetch next pages (batch of elements or List<>)
  2. Let the Library expose 2 API's - PaginatedList fetchData() -> client would call this one initially PaginatedList fetchNextPage(PaginatedList) -> client would call this for sub-sequent pages
  3. This one is slightly modified version of approach 2. Let library expose a modified fetchData() PaginatedList fetchData(PaginatedList list) -> initially client would need to create a new PaginatedList(), which would initialize it's state like pageToProcess = 0; This API kind of takes in the metadata associated with the service call to identify which page to get next

Any more suggestions or is there a better way?variations would be appreciated!

Is this is okay, or is there a better way?

Edit:-

3 straightforward approaches come to my mind -

  1. The one described above. Where the PaginatedList would expose a way to fetch next pages (batch of elements or List<>)
  2. Let the Library expose 2 API's - PaginatedList fetchData() -> client would call this one initially PaginatedList fetchNextPage(PaginatedList) -> client would call this for sub-sequent pages
  3. This one is slightly modified version of approach 2. Let library expose a modified fetchData() PaginatedList fetchData(PaginatedList list) -> initially client would need to create a new PaginatedList(), which would initialize it's state like pageToProcess = 0; This API kind of takes in the metadata associated with the service call to identify which page to get next

Any more suggestions or variations would be appreciated!

added 4 characters in body
Source Link
AgentX
  • 131
  • 1
  • 1
  • 4

I need to create a class which acts as a paginated list. My use case is like this:

I am writing a library which makes a service call to fetch a List<obj>. Since the List<> can be large, the service expects me to call with a PAGE_SIZE. The service has also provided me with a TPS of x which I need call it with.

I need to do two things:

  • Rate Limit my calls to the Service
  • Expose a PaginatedList<> from the library so that any client can use the Library and rate limit as per their TPS.

I was wondering whether the way I am implementing this PaginatedList is correct or if there is a better way.

public MyPaginatedList {
  private static final PAGE_SIZE = 50;
  private ServiceClient client;
  private int pageToProcess = 0; //to keep track of which page we need to process next
  private List<Data> data; // this is what the library exposes to outside world
  public MyPaginatedList(ServiceClient client) {
    this.client = client;
  }

  public MyPaginatedList getDataList(){
    Servicerequest request = new ServiceRequest();
    request.setIndex(PAGE_SIZE*pageToProcess++); // fetch the next set of items

    this.data = client.APICall(request);
    return this; // we return the current object
  }
}

The public API exposed by this Library would use MyPaginatedList like this:

class Library {
  private ServiceClient client;

  public Library(ServiceClient client) {
    this.client = client;
  }
  public MyPaginatedList fetchData() {
    return new MyPaginatedList(client).getDataList();
  }
}

Any client of this library can use MyPaginatedList and make Rate Limited Calls like this.

MyPaginatedList resultData = new Library().fetchData();
while(true) {
  for(Data d: resultData.getData()) // loop over the list exposed by MyPaginatedlist
  ...
  }
  RateLimiter.acquire(); // use guava rate limiter, would block till a token is available
  resultData = resultData.getDataList();
  if(resultData.getData().isempty()) {
    break;
  }
}

Is this is okay, or is there a better way?

I need to create a class which acts as a paginated list. My use case is like this:

I am writing a library which makes a service call to fetch a List<obj>. Since the List<> can be large, the service expects me to call with a PAGE_SIZE. The service has also provided me with a TPS of x which I need call it with.

I need to do two things:

  • Rate Limit my calls to the Service
  • Expose a PaginatedList<> from the library so that any client can use the Library and rate limit as per their TPS.

I wondering whether the way I am implementing this PaginatedList is correct or if there is a better way.

public MyPaginatedList {
  private static final PAGE_SIZE = 50;
  private ServiceClient client;
  private int pageToProcess = 0; //to keep track of which page we need to process next
  private List<Data> data; // this is what the library exposes to outside world
  public MyPaginatedList(ServiceClient client) {
    this.client = client;
  }

  public MyPaginatedList getDataList(){
    Servicerequest request = new ServiceRequest();
    request.setIndex(PAGE_SIZE*pageToProcess++); // fetch the next set of items

    this.data = client.APICall(request);
    return this; // we return the current object
  }
}

The public API exposed by this Library would use MyPaginatedList like this:

class Library {
  private ServiceClient client;

  public Library(ServiceClient client) {
    this.client = client;
  }
  public MyPaginatedList fetchData() {
    return new MyPaginatedList(client).getDataList();
  }
}

Any client of this library can use MyPaginatedList and make Rate Limited Calls like this.

MyPaginatedList resultData = new Library().fetchData();
while(true) {
  for(Data d: resultData.getData()) // loop over the list exposed by MyPaginatedlist
  ...
  }
  RateLimiter.acquire(); // use guava rate limiter, would block till a token is available
  resultData = resultData.getDataList();
  if(resultData.getData().isempty()) {
    break;
  }
}

Is this is okay, or is there a better way?

I need to create a class which acts as a paginated list. My use case is like this:

I am writing a library which makes a service call to fetch a List<obj>. Since the List<> can be large, the service expects me to call with a PAGE_SIZE. The service has also provided me with a TPS of x which I need call it with.

I need to do two things:

  • Rate Limit my calls to the Service
  • Expose a PaginatedList<> from the library so that any client can use the Library and rate limit as per their TPS.

I was wondering whether the way I am implementing this PaginatedList is correct or if there is a better way.

public MyPaginatedList {
  private static final PAGE_SIZE = 50;
  private ServiceClient client;
  private int pageToProcess = 0; //to keep track of which page we need to process next
  private List<Data> data; // this is what the library exposes to outside world
  public MyPaginatedList(ServiceClient client) {
    this.client = client;
  }

  public MyPaginatedList getDataList(){
    Servicerequest request = new ServiceRequest();
    request.setIndex(PAGE_SIZE*pageToProcess++); // fetch the next set of items

    this.data = client.APICall(request);
    return this; // we return the current object
  }
}

The public API exposed by this Library would use MyPaginatedList like this:

class Library {
  private ServiceClient client;

  public Library(ServiceClient client) {
    this.client = client;
  }
  public MyPaginatedList fetchData() {
    return new MyPaginatedList(client).getDataList();
  }
}

Any client of this library can use MyPaginatedList and make Rate Limited Calls like this.

MyPaginatedList resultData = new Library().fetchData();
while(true) {
  for(Data d: resultData.getData()) // loop over the list exposed by MyPaginatedlist
  ...
  }
  RateLimiter.acquire(); // use guava rate limiter, would block till a token is available
  resultData = resultData.getDataList();
  if(resultData.getData().isempty()) {
    break;
  }
}

Is this is okay, or is there a better way?

deleted 34 characters in body
Source Link
Ethan Bierlein
  • 15.8k
  • 4
  • 59
  • 146
Loading
added 20 characters in body
Source Link
Phrancis
  • 20.4k
  • 6
  • 68
  • 154
Loading
Source Link
AgentX
  • 131
  • 1
  • 1
  • 4
Loading