Coverage Report - com.jcabi.github.RtValuePagination
 
Classes in this File Line Coverage Branch Coverage Complexity
RtValuePagination
80%
8/10
0%
0/20
1.909
RtValuePagination$Items
89%
35/39
28%
13/46
1.909
RtValuePagination$Mapping
N/A
N/A
1.909
 
 1  2
 /**
 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 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  
  * Github value pagination.
 49  
  *
 50  
  * @author Paul Polishchuk (ppol@ua.fm)
 51  
  * @version $Id: 01f43a36132eb12d493202cb6a449acd1b2f1789 $
 52  
  * @since 0.8
 53  
  * @param <T> Type of iterable objects
 54  
  * @param <P> Type of source objects
 55  
  * @see <a href="http://developer.github.com/v3/#pagination">Pagination</a>
 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  
      * Mapping to use.
 64  
      */
 65  
     private final transient RtValuePagination.Mapping<T, P> map;
 66  
 
 67  
     /**
 68  
      * Start entry to use.
 69  
      */
 70  
     private final transient Request entry;
 71  
 
 72  
     /**
 73  
      * Public ctor.
 74  
      * @param req Request
 75  
      * @param mpp Mapping
 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  
      * Entry.
 97  
      * @return Entry point
 98  
      */
 99  
     public Request request() {
 100  1
         return this.entry;
 101  
     }
 102  
 
 103  
     /**
 104  
      * Mapping.
 105  
      * @return Mapping
 106  
      */
 107  
     public RtValuePagination.Mapping<T, P> mapping() {
 108  1
         return this.map;
 109  
     }
 110  
 
 111  
     /**
 112  
      * Mapping from JsonValue successor to the destination type.
 113  
      * @param <X> Type of custom object
 114  
      * @param <P> Type of source object
 115  
      */
 116  
     @Immutable
 117  
     public interface Mapping<X, P extends JsonValue> {
 118  
         /**
 119  
          * Map JsonValue successor to the type required.
 120  
          * @param value Extends JsonValue
 121  
          * @return Custom object
 122  
          */
 123  
         X map(P value);
 124  
     }
 125  
 
 126  
     /**
 127  
      * Iterator.
 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  
          * Mapping to use.
 134  
          */
 135  
         private final transient RtValuePagination.Mapping<X, P> mapping;
 136  
         /**
 137  
          * Next entry to use.
 138  
          */
 139  
         private transient Request request;
 140  
         /**
 141  
          * Available objects.
 142  
          */
 143  
         private transient Queue<P> objects;
 144  
         /**
 145  
          * Current entry can be used to fetch objects.
 146  
          */
 147  44
         private transient boolean more = true;
 148  
         /**
 149  
          * Ctor.
 150  
          * @param entry Entry
 151  
          * @param mpp Mapping
 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  
          * Fetch the next portion, if available.
 189  
          * @throws IOException If there is any I/O problem
 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  
 }