-
Notifications
You must be signed in to change notification settings - Fork 454
Expand file tree
/
Copy pathResourceSet.java
More file actions
138 lines (111 loc) · 3.77 KB
/
ResourceSet.java
File metadata and controls
138 lines (111 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.twilio.base;
import com.twilio.http.TwilioRestClient;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* A collection of resources.
*
* @param <E> type of the resource
*/
public class ResourceSet<E extends Resource> implements Iterable<E> {
private final Reader<E> reader;
private final TwilioRestClient client;
private final Page<E> firstPage; // Store reference to first page to enable multiple iterations
private boolean autoPaging;
private long pages = 1;
private long pageLimit = Long.MAX_VALUE;
private long processed = 0;
private Page<E> page;
private Iterator<E> iterator;
/**
* Initialize the resource set.
*
* @param reader reader used to fetch next page
* @param client client used to make requests
* @param page page of data
*/
public ResourceSet(final Reader<E> reader, final TwilioRestClient client, final Page<E> page) {
this.reader = reader;
this.client = client;
this.firstPage = page; // Save first page to allow resetting iterator state
this.page = page;
this.iterator = page.getRecords().iterator();
this.autoPaging = true;
if (reader.getLimit() != null) {
this.pageLimit = (long)(Math.ceil((double)reader.getLimit() / (double)page.getPageSize()));
}
}
public boolean isAutoPaging() {
return autoPaging;
}
public ResourceSet setAutoPaging(final boolean autoPaging) {
this.autoPaging = autoPaging;
return this;
}
public Integer getPageSize() {
return page.getPageSize();
}
public ResourceSet<E> setPageSize(final int pageSize) {
reader.pageSize(pageSize);
return this;
}
public Long getLimit() {
return reader.getLimit();
}
public ResourceSet<E> setLimit(final long limit) {
reader.limit(limit);
return this;
}
public long getPageLimit() {
return pageLimit;
}
@Override
public Iterator<E> iterator() {
// Reset state to allow multiple iterations
this.processed = 0;
this.pages = 1;
this.page = this.firstPage; // Reset to first page for new iteration
this.iterator = this.firstPage.getRecords().iterator(); // Reset iterator to start of first page
return new ResourceSetIterator<>(this);
}
private void fetchNextPage() {
if (!page.hasNextPage() || pages >= pageLimit) {
return;
}
pages++;
page = reader.nextPage(page, client);
iterator = page.getRecords().iterator();
}
private class ResourceSetIterator<E extends Resource> implements Iterator<E> {
private final ResourceSet<E> resourceSet;
public ResourceSetIterator(final ResourceSet<E> resourceSet) {
this.resourceSet = resourceSet;
}
@Override
public boolean hasNext() {
if (resourceSet.getLimit() != null && resourceSet.processed >= resourceSet.getLimit()) {
return false;
}
return resourceSet.iterator.hasNext();
}
@Override
public E next() {
if (resourceSet == null || resourceSet.iterator == null) {
throw new NoSuchElementException();
}
E element = resourceSet.iterator.next();
if (resourceSet.isAutoPaging() && !resourceSet.iterator.hasNext()) {
resourceSet.fetchNextPage();
}
resourceSet.processed++;
return element;
}
@Override
public void remove() {
if (resourceSet.iterator != null) {
resourceSet.processed++;
resourceSet.iterator.remove();
}
}
}
}