| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Bulk |
|
| 2.2;2.2 | ||||
| Bulk$1 |
|
| 2.2;2.2 | ||||
| Bulk$1$1 |
|
| 2.2;2.2 |
| 1 | /** | |
| 2 | * Copyright (c) 2013-2017, jcabi.com | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: 1) Redistributions of source code must retain the above | |
| 8 | * copyright notice, this list of conditions and the following | |
| 9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
| 10 | * copyright notice, this list of conditions and the following | |
| 11 | * disclaimer in the documentation and/or other materials provided | |
| 12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
| 13 | * the names of its contributors may be used to endorse or promote | |
| 14 | * products derived from this software without specific prior written | |
| 15 | * permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
| 19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
| 21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
| 26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
| 28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 | */ | |
| 30 | package com.jcabi.github; | |
| 31 | ||
| 32 | import java.lang.reflect.InvocationHandler; | |
| 33 | import java.lang.reflect.InvocationTargetException; | |
| 34 | import java.lang.reflect.Method; | |
| 35 | import java.lang.reflect.Proxy; | |
| 36 | import java.util.Iterator; | |
| 37 | import javax.json.JsonObject; | |
| 38 | import lombok.EqualsAndHashCode; | |
| 39 | ||
| 40 | /** | |
| 41 | * Bulk items, with pre-saved JSON. | |
| 42 | * | |
| 43 | * <p>This class should be used as a decorator for object obtained | |
| 44 | * from Github, when you want to keep their JSON values in memory. For | |
| 45 | * example: | |
| 46 | * | |
| 47 | * <pre> Iterable<Issue> issues = repo.issues().iterate( | |
| 48 | * new HashMap<String, String>() | |
| 49 | * ); | |
| 50 | * for (Issue issue : issues) { | |
| 51 | * System.out.println(new Issue.Smart(issue).title()); | |
| 52 | * }</pre> | |
| 53 | * | |
| 54 | * <p>Let's say, there are 50 issues in Github's repo. This code will | |
| 55 | * make 52 HTTP requests to Github. The first one will fetch the first | |
| 56 | * 30 issues in JSON array. Then, for every one of them, in order | |
| 57 | * to retrieve issue title a separate HTTP request will be made. Then, | |
| 58 | * one more page will be fetched, with 20 issues. And again, 20 new | |
| 59 | * HTTP requests to get their titles. | |
| 60 | * | |
| 61 | * <p>Class {@code Bulk} helps us to reduce the amount of this extra work: | |
| 62 | * | |
| 63 | * <pre> Iterable<Issue> issues = new Bulk<Issue>( | |
| 64 | * repo.issues().iterate( | |
| 65 | * new HashMap<String, String>() | |
| 66 | * ) | |
| 67 | * );</pre> | |
| 68 | * | |
| 69 | * <p>Now, there will be just two HTTP requests. | |
| 70 | * | |
| 71 | * @author Yegor Bugayenko (yegor@tpc2.com) | |
| 72 | * @version $Id: 6db3339b902ff2aaa71820c94cb3c701141bec7b $ | |
| 73 | * @since 0.4 | |
| 74 | * @param <T> Type of iterable objects | |
| 75 | * @see <a href="http://developer.github.com/v3/#pagination">Pagination</a> | |
| 76 | */ | |
| 77 | 0 | @EqualsAndHashCode(of = "origin") |
| 78 | public final class Bulk<T extends JsonReadable> implements Iterable<T> { | |
| 79 | ||
| 80 | /** | |
| 81 | * Original iterable. | |
| 82 | */ | |
| 83 | private final transient Iterable<T> origin; | |
| 84 | ||
| 85 | /** | |
| 86 | * Public ctor. | |
| 87 | * @param items Items original | |
| 88 | * @checkstyle AnonInnerLength (50 lines) | |
| 89 | */ | |
| 90 | @SuppressWarnings("unchecked") | |
| 91 | 2 | public Bulk(final Iterable<T> items) { |
| 92 | 2 | if (items instanceof RtPagination) { |
| 93 | 1 | final RtPagination<T> page = RtPagination.class.cast(items); |
| 94 | 1 | final RtValuePagination.Mapping<T, JsonObject> mapping = |
| 95 | 1 | page.mapping(); |
| 96 | 1 | this.origin = new RtPagination<T>( |
| 97 | 1 | page.request(), |
| 98 | 2 | new RtValuePagination.Mapping<T, JsonObject>() { |
| 99 | @Override | |
| 100 | public T map(final JsonObject object) { | |
| 101 | 1 | final T item = mapping.map(object); |
| 102 | 1 | return (T) Proxy.newProxyInstance( |
| 103 | 1 | Thread.currentThread().getContextClassLoader(), |
| 104 | 1 | item.getClass().getInterfaces(), |
| 105 | 1 | new InvocationHandler() { |
| 106 | @Override | |
| 107 | @SuppressWarnings("PMD.UseVarargs") | |
| 108 | public Object invoke(final Object proxy, | |
| 109 | final Method method, final Object[] args) { | |
| 110 | final Object result; | |
| 111 | 2 | if ("json".equals(method.getName())) { |
| 112 | 1 | result = object; |
| 113 | } else { | |
| 114 | try { | |
| 115 | 1 | result = method.invoke(item, args); |
| 116 | 0 | } catch ( |
| 117 | final IllegalAccessException ex | |
| 118 | ) { | |
| 119 | 0 | throw new IllegalStateException(ex); |
| 120 | 0 | } catch ( |
| 121 | final InvocationTargetException ex | |
| 122 | ) { | |
| 123 | 0 | throw new IllegalStateException(ex); |
| 124 | 1 | } |
| 125 | } | |
| 126 | 2 | return result; |
| 127 | } | |
| 128 | } | |
| 129 | ); | |
| 130 | } | |
| 131 | } | |
| 132 | ); | |
| 133 | 1 | } else { |
| 134 | 1 | this.origin = items; |
| 135 | } | |
| 136 | 2 | } |
| 137 | ||
| 138 | @Override | |
| 139 | public String toString() { | |
| 140 | 0 | return this.origin.toString(); |
| 141 | } | |
| 142 | ||
| 143 | @Override | |
| 144 | public Iterator<T> iterator() { | |
| 145 | 2 | return this.origin.iterator(); |
| 146 | } | |
| 147 | ||
| 148 | } |