Coverage Report - com.jcabi.github.Bulk
 
Classes in this File Line Coverage Branch Coverage Complexity
Bulk
83%
10/12
16%
2/12
2.2
Bulk$1
100%
3/3
N/A
2.2
Bulk$1$1
60%
6/10
100%
2/2
2.2
 
 1  2
 /**
 2  
  * Copyright (c) 2013-2015, 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 javax.validation.constraints.NotNull;
 39  
 import lombok.EqualsAndHashCode;
 40  
 
 41  
 /**
 42  
  * Bulk items, with pre-saved JSON.
 43  
  *
 44  
  * <p>This class should be used as a decorator for object obtained
 45  
  * from Github, when you want to keep their JSON values in memory. For
 46  
  * example:
 47  
  *
 48  
  * <pre> Iterable&lt;Issue&gt; issues = repo.issues().iterate(
 49  
  *   new HashMap&lt;String, String&gt;()
 50  
  * );
 51  
  * for (Issue issue : issues) {
 52  
  *   System.out.println(new Issue.Smart(issue).title());
 53  
  * }</pre>
 54  
  *
 55  
  * <p>Let's say, there are 50 issues in Github's repo. This code will
 56  
  * make 52 HTTP requests to Github. The first one will fetch the first
 57  
  * 30 issues in JSON array. Then, for every one of them, in order
 58  
  * to retrieve issue title a separate HTTP request will be made. Then,
 59  
  * one more page will be fetched, with 20 issues. And again, 20 new
 60  
  * HTTP requests to get their titles.
 61  
  *
 62  
  * <p>Class {@code Bulk} helps us to reduce the amount of this extra work:
 63  
  *
 64  
  * <pre> Iterable&lt;Issue&gt; issues = new Bulk&lt;Issue&gt;(
 65  
  *   repo.issues().iterate(
 66  
  *     new HashMap&lt;String, String&gt;()
 67  
  *   )
 68  
  * );</pre>
 69  
  *
 70  
  * <p>Now, there will be just two HTTP requests.
 71  
  *
 72  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 73  
  * @version $Id$
 74  
  * @since 0.4
 75  
  * @param <T> Type of iterable objects
 76  
  * @see <a href="http://developer.github.com/v3/#pagination">Pagination</a>
 77  
  */
 78  0
 @EqualsAndHashCode(of = "origin")
 79  
 public final class Bulk<T extends JsonReadable> implements Iterable<T> {
 80  
 
 81  
     /**
 82  
      * Original iterable.
 83  
      */
 84  
     private final transient Iterable<T> origin;
 85  
 
 86  
     /**
 87  
      * Public ctor.
 88  
      * @param items Items original
 89  
      * @checkstyle AnonInnerLength (50 lines)
 90  
      */
 91  
     @SuppressWarnings("unchecked")
 92  
     public Bulk(
 93  
         @NotNull(message = "items can't be NULL") final Iterable<T> items
 94  2
     ) {
 95  2
         if (items instanceof RtPagination) {
 96  1
             final RtPagination<T> page = RtPagination.class.cast(items);
 97  1
             final RtValuePagination.Mapping<T, JsonObject> mapping =
 98  
                 page.mapping();
 99  1
             this.origin = new RtPagination<T>(
 100  
                 page.request(),
 101  2
                 new RtValuePagination.Mapping<T, JsonObject>() {
 102  
                     @Override
 103  
                     public T map(final JsonObject object) {
 104  1
                         final T item = mapping.map(object);
 105  1
                         return (T) Proxy.newProxyInstance(
 106  
                             Thread.currentThread().getContextClassLoader(),
 107  
                             item.getClass().getInterfaces(),
 108  1
                             new InvocationHandler() {
 109  
                                 @Override
 110  
                                 public Object invoke(final Object proxy,
 111  
                                     final Method method, final Object[] args) {
 112  
                                     final Object result;
 113  2
                                     if ("json".equals(method.getName())) {
 114  1
                                         result = object;
 115  
                                     } else {
 116  
                                         try {
 117  1
                                             result = method.invoke(item, args);
 118  0
                                         } catch (
 119  
                                             final IllegalAccessException ex
 120  
                                         ) {
 121  0
                                             throw new IllegalStateException(ex);
 122  0
                                         } catch (
 123  
                                             final InvocationTargetException ex
 124  
                                         ) {
 125  0
                                             throw new IllegalStateException(ex);
 126  1
                                         }
 127  
                                     }
 128  2
                                     return result;
 129  
                                 }
 130  
                             }
 131  
                         );
 132  
                     }
 133  
                 }
 134  
             );
 135  1
         } else {
 136  1
             this.origin = items;
 137  
         }
 138  2
     }
 139  
 
 140  
     @Override
 141  
     public String toString() {
 142  0
         return this.origin.toString();
 143  
     }
 144  
 
 145  
     @Override
 146  
     @NotNull(message = "iterator is never NULL")
 147  
     public Iterator<T> iterator() {
 148  2
         return this.origin.iterator();
 149  
     }
 150  
 
 151  
 }