Coverage Report - com.jcabi.github.RtValuePagination
 
Classes in this File Line Coverage Branch Coverage Complexity
RtValuePagination
80%
8/10
0%
0/16
1.909
RtValuePagination$Items
84%
28/33
34%
13/38
1.909
RtValuePagination$Mapping
N/A
N/A
1.909
 
 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 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 javax.validation.constraints.NotNull;
 46  
 import lombok.EqualsAndHashCode;
 47  
 
 48  
 /**
 49  
  * Github value pagination.
 50  
  *
 51  
  * @author Paul Polishchuk (ppol@ua.fm)
 52  
  * @version $Id$
 53  
  * @since 0.8
 54  
  * @param <T> Type of iterable objects
 55  
  * @param <P> Type of source objects
 56  
  * @see <a href="http://developer.github.com/v3/#pagination">Pagination</a>
 57  
  */
 58  
 @Immutable
 59  0
 @EqualsAndHashCode(of = { "entry", "map" })
 60  
 public final class RtValuePagination<T, P extends JsonValue> implements
 61  
     Iterable<T> {
 62  
 
 63  
     /**
 64  
      * Mapping to use.
 65  
      */
 66  
     private final transient RtValuePagination.Mapping<T, P> map;
 67  
 
 68  
     /**
 69  
      * Start entry to use.
 70  
      */
 71  
     private final transient Request entry;
 72  
 
 73  
     /**
 74  
      * Public ctor.
 75  
      * @param req Request
 76  
      * @param mpp Mapping
 77  
      */
 78  
     public RtValuePagination(
 79  
         @NotNull(message = "req can't be NULL") final Request req,
 80  
         @NotNull(message = "map can't be null")
 81  
         final RtValuePagination.Mapping<T, P> mpp
 82  42
     ) {
 83  42
         this.entry = req;
 84  42
         this.map = mpp;
 85  42
     }
 86  
 
 87  
     @Override
 88  
     @NotNull(message = "toString is never NULL")
 89  
     public String toString() {
 90  0
         return this.entry.uri().get().toString();
 91  
     }
 92  
 
 93  
     @Override
 94  
     @NotNull(message = "Iterator is never NULL")
 95  
     public Iterator<T> iterator() {
 96  38
         return new RtValuePagination.Items<T, P>(this.entry, this.map);
 97  
     }
 98  
 
 99  
     /**
 100  
      * Entry.
 101  
      * @return Entry point
 102  
      */
 103  
     @NotNull(message = "Request is never NULL")
 104  
     public Request request() {
 105  1
         return this.entry;
 106  
     }
 107  
 
 108  
     /**
 109  
      * Mapping.
 110  
      * @return Mapping
 111  
      */
 112  
     @NotNull(message = "map is never NULLs")
 113  
     public RtValuePagination.Mapping<T, P> mapping() {
 114  1
         return this.map;
 115  
     }
 116  
 
 117  
     /**
 118  
      * Mapping from JsonValue successor to the destination type.
 119  
      * @param <X> Type of custom object
 120  
      * @param <P> Type of source object
 121  
      */
 122  
     @Immutable
 123  
     public interface Mapping<X, P extends JsonValue> {
 124  
         /**
 125  
          * Map JsonValue successor to the type required.
 126  
          * @param value Extends JsonValue
 127  
          * @return Custom object
 128  
          */
 129  
         X map(P value);
 130  
     }
 131  
 
 132  
     /**
 133  
      * Iterator.
 134  
      */
 135  0
     @EqualsAndHashCode(of = { "mapping", "request", "objects", "more" })
 136  
     private static final class Items<X, P extends JsonValue> implements
 137  
         Iterator<X> {
 138  
         /**
 139  
          * Mapping to use.
 140  
          */
 141  
         private final transient RtValuePagination.Mapping<X, P> mapping;
 142  
         /**
 143  
          * Next entry to use.
 144  
          */
 145  
         private transient Request request;
 146  
         /**
 147  
          * Available objects.
 148  
          */
 149  
         private transient Queue<P> objects;
 150  
         /**
 151  
          * Current entry can be used to fetch objects.
 152  
          */
 153  38
         private transient boolean more = true;
 154  
         /**
 155  
          * Ctor.
 156  
          * @param entry Entry
 157  
          * @param mpp Mapping
 158  
          */
 159  38
         Items(final Request entry, final RtValuePagination.Mapping<X, P> mpp) {
 160  38
             this.request = entry;
 161  38
             this.mapping = mpp;
 162  38
             this.objects = new LinkedList<P>();
 163  38
         }
 164  
         @Override
 165  
         public X next() {
 166  60
             synchronized (this.mapping) {
 167  60
                 if (!this.hasNext()) {
 168  2
                     throw new NoSuchElementException(
 169  
                         "no more elements in pagination, use #hasNext()"
 170  
                     );
 171  
                 }
 172  58
                 return this.mapping.map(this.objects.remove());
 173  2
             }
 174  
         }
 175  
         @Override
 176  
         public void remove() {
 177  0
             throw new UnsupportedOperationException("#remove()");
 178  
         }
 179  
         @Override
 180  
         public boolean hasNext() {
 181  130
             synchronized (this.mapping) {
 182  130
                 if ((this.objects == null || this.objects.isEmpty())
 183  
                     && this.more) {
 184  
                     try {
 185  40
                         this.fetch();
 186  0
                     } catch (final IOException ex) {
 187  0
                         throw new IllegalStateException(ex);
 188  40
                     }
 189  
                 }
 190  130
                 return !this.objects.isEmpty();
 191  0
             }
 192  
         }
 193  
         /**
 194  
          * Fetch the next portion, if available.
 195  
          * @throws IOException If there is any I/O problem
 196  
          */
 197  
         @SuppressWarnings("unchecked")
 198  
         private void fetch() throws IOException {
 199  40
             final RestResponse response = this.request.fetch()
 200  
                 .as(RestResponse.class)
 201  
                 .assertStatus(HttpURLConnection.HTTP_OK);
 202  40
             final WebLinkingResponse.Link link = response
 203  
                 .as(WebLinkingResponse.class)
 204  
                 .links()
 205  
                 .get("next");
 206  40
             if (link == null) {
 207  38
                 this.more = false;
 208  
             } else {
 209  2
                 this.request = response.jump(link.uri());
 210  
             }
 211  40
             final JsonArray arr = response.as(JsonResponse.class).json()
 212  
                 .readArray();
 213  40
             final Queue<P> list = new LinkedList<P>();
 214  40
             for (final JsonValue value : arr) {
 215  58
                 list.add((P) value);
 216  58
             }
 217  40
             this.objects = list;
 218  40
         }
 219  
     }
 220  
 
 221  
 }