| 1 | 2 | |
| 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 | |
package com.jcabi.github; |
| 31 | |
|
| 32 | |
import com.jcabi.aspects.Immutable; |
| 33 | |
import com.jcabi.http.Request; |
| 34 | |
import com.jcabi.http.response.JsonResponse; |
| 35 | |
import com.jcabi.http.response.RestResponse; |
| 36 | |
import com.jcabi.http.response.WebLinkingResponse; |
| 37 | |
import java.io.IOException; |
| 38 | |
import java.net.HttpURLConnection; |
| 39 | |
import java.util.Iterator; |
| 40 | |
import java.util.LinkedList; |
| 41 | |
import java.util.NoSuchElementException; |
| 42 | |
import java.util.Queue; |
| 43 | |
import javax.json.JsonArray; |
| 44 | |
import javax.json.JsonValue; |
| 45 | |
import lombok.EqualsAndHashCode; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
@Immutable |
| 58 | 0 | @EqualsAndHashCode(of = { "entry", "map" }) |
| 59 | |
public final class RtValuePagination<T, P extends JsonValue> implements |
| 60 | |
Iterable<T> { |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
private final transient RtValuePagination.Mapping<T, P> map; |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
private final transient Request entry; |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
public RtValuePagination( |
| 78 | |
final Request req, |
| 79 | |
final RtValuePagination.Mapping<T, P> mpp |
| 80 | 49 | ) { |
| 81 | 49 | this.entry = req; |
| 82 | 49 | this.map = mpp; |
| 83 | 49 | } |
| 84 | |
|
| 85 | |
@Override |
| 86 | |
public String toString() { |
| 87 | 0 | return this.entry.uri().get().toString(); |
| 88 | |
} |
| 89 | |
|
| 90 | |
@Override |
| 91 | |
public Iterator<T> iterator() { |
| 92 | 44 | return new RtValuePagination.Items<T, P>(this.entry, this.map); |
| 93 | |
} |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
public Request request() { |
| 100 | 1 | return this.entry; |
| 101 | |
} |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
public RtValuePagination.Mapping<T, P> mapping() { |
| 108 | 1 | return this.map; |
| 109 | |
} |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
@Immutable |
| 117 | |
public interface Mapping<X, P extends JsonValue> { |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
X map(P value); |
| 124 | |
} |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | 0 | @EqualsAndHashCode(of = { "mapping", "request", "objects", "more" }) |
| 130 | |
private static final class Items<X, P extends JsonValue> implements |
| 131 | |
Iterator<X> { |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
private final transient RtValuePagination.Mapping<X, P> mapping; |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
private transient Request request; |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
private transient Queue<P> objects; |
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | 44 | private transient boolean more = true; |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | 43 | Items(final Request entry, final RtValuePagination.Mapping<X, P> mpp) { |
| 154 | 43 | this.request = entry; |
| 155 | 44 | this.mapping = mpp; |
| 156 | 44 | this.objects = new LinkedList<P>(); |
| 157 | 43 | } |
| 158 | |
@Override |
| 159 | |
public X next() { |
| 160 | 67 | synchronized (this.mapping) { |
| 161 | 67 | if (!this.hasNext()) { |
| 162 | 2 | throw new NoSuchElementException( |
| 163 | |
"no more elements in pagination, use #hasNext()" |
| 164 | |
); |
| 165 | |
} |
| 166 | 64 | return this.mapping.map(this.objects.remove()); |
| 167 | 3 | } |
| 168 | |
} |
| 169 | |
@Override |
| 170 | |
public void remove() { |
| 171 | 0 | throw new UnsupportedOperationException("#remove()"); |
| 172 | |
} |
| 173 | |
@Override |
| 174 | |
public boolean hasNext() { |
| 175 | 141 | synchronized (this.mapping) { |
| 176 | 142 | if ((this.objects == null || this.objects.isEmpty()) |
| 177 | |
&& this.more) { |
| 178 | |
try { |
| 179 | 45 | this.fetch(); |
| 180 | 0 | } catch (final IOException ex) { |
| 181 | 0 | throw new IllegalStateException(ex); |
| 182 | 46 | } |
| 183 | |
} |
| 184 | 141 | return !this.objects.isEmpty(); |
| 185 | 1 | } |
| 186 | |
} |
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
@SuppressWarnings("unchecked") |
| 192 | |
private void fetch() throws IOException { |
| 193 | 45 | final RestResponse response = this.request.fetch() |
| 194 | 47 | .as(RestResponse.class) |
| 195 | 47 | .assertStatus(HttpURLConnection.HTTP_OK); |
| 196 | 46 | final WebLinkingResponse.Link link = response |
| 197 | 46 | .as(WebLinkingResponse.class) |
| 198 | 46 | .links() |
| 199 | 46 | .get("next"); |
| 200 | 46 | if (link == null) { |
| 201 | 44 | this.more = false; |
| 202 | |
} else { |
| 203 | 2 | this.request = response.jump(link.uri()); |
| 204 | |
} |
| 205 | 46 | final JsonArray arr = response.as(JsonResponse.class).json() |
| 206 | 46 | .readArray(); |
| 207 | 46 | final Queue<P> list = new LinkedList<P>(); |
| 208 | 46 | for (final JsonValue value : arr) { |
| 209 | 65 | list.add((P) value); |
| 210 | 65 | } |
| 211 | 46 | this.objects = list; |
| 212 | 46 | } |
| 213 | |
} |
| 214 | |
|
| 215 | |
} |