Coverage Report - com.jcabi.github.RtCommitsComparison
 
Classes in this File Line Coverage Branch Coverage Complexity
RtCommitsComparison
93%
14/15
0%
0/12
1.091
RtCommitsComparison$AjcClosure1
100%
1/1
N/A
1.091
RtCommitsComparison$AjcClosure3
100%
1/1
N/A
1.091
RtCommitsComparison$AjcClosure5
100%
1/1
N/A
1.091
RtCommitsComparison$FileChanges
71%
5/7
0%
0/12
1.091
RtCommitsComparison$FileChanges$AjcClosure1
100%
1/1
N/A
1.091
RtCommitsComparison$FileChangesIterator
75%
6/8
0%
0/12
1.091
 
 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.aspects.Loggable;
 34  
 import com.jcabi.http.Request;
 35  
 import java.io.IOException;
 36  
 import java.util.Iterator;
 37  
 import java.util.List;
 38  
 import javax.json.JsonArray;
 39  
 import javax.json.JsonObject;
 40  
 import lombok.EqualsAndHashCode;
 41  
 import lombok.ToString;
 42  
 
 43  
 /**
 44  
  * Commits comparison.
 45  
  * @author Alexander Sinyagin (sinyagin.alexander@gmail.com)
 46  
  * @version $Id: fa868ccf10c7be94588ca8cfbc93dfd442e43ad6 $
 47  
  */
 48  
 @Immutable
 49  
 @Loggable(Loggable.DEBUG)
 50  0
 @EqualsAndHashCode(of = "request")
 51  
 final class RtCommitsComparison implements CommitsComparison {
 52  
 
 53  
     /**
 54  
      * RESTful request for the comparison.
 55  
      */
 56  
     private final transient Request request;
 57  
 
 58  
     /**
 59  
      * Parent repository.
 60  
      */
 61  
     private final transient Repo owner;
 62  
 
 63  
     /**
 64  
      * Ctor.
 65  
      * @param req Entry point of API
 66  
      * @param repo Repository
 67  
      * @param base SHA of a base commit
 68  
      * @param head SHA of a head commit
 69  
      * @checkstyle ParameterNumber (3 lines)
 70  
      */
 71  
     RtCommitsComparison(final Request req, final Repo repo,
 72  5
         final String base, final String head) {
 73  5
         this.owner = repo;
 74  5
         this.request = req.uri()
 75  5
             .path("/repos")
 76  5
             .path(repo.coordinates().toString())
 77  5
             .path("/compare")
 78  5
             .path(String.format("%s...%s", base, head))
 79  5
             .back();
 80  5
     }
 81  
 
 82  
     @Override
 83  
     public Repo repo() {
 84  2
         return this.owner;
 85  
     }
 86  
 
 87  
     @Override
 88  
     public Iterable<FileChange> files() throws IOException {
 89  6
         return new FileChanges(this.json().getJsonArray("files"));
 90  
     }
 91  
 
 92  
     @Override
 93  
     public String toString() {
 94  1
         return this.request.uri().get().toString();
 95  
     }
 96  
 
 97  
     @Override
 98  
     public JsonObject json() throws IOException {
 99  10
         return new RtJson(this.request).fetch();
 100  
     }
 101  
 
 102  
     /**
 103  
      * Iterator that yields FileChange objects converted
 104  
      * from JSON objects in a JSON list.
 105  
      */
 106  3
     @EqualsAndHashCode(of = { "iterator" })
 107  0
     @ToString
 108  
     private static final class FileChangesIterator
 109  
         implements Iterator<FileChange> {
 110  
         /**
 111  
          * Encapsulated iterator of file change JSON objects.
 112  
          */
 113  
         private final transient Iterator<JsonObject> iterator;
 114  
 
 115  
         /**
 116  
          * Ctor.
 117  
          * @param iter Iterator of file change JSON objects
 118  
          */
 119  
         FileChangesIterator(
 120  
             final Iterator<JsonObject> iter
 121  3
         ) {
 122  3
             this.iterator = iter;
 123  3
         }
 124  
 
 125  
         @Override
 126  
         public FileChange next() {
 127  3
             return new RtFileChange(this.iterator.next());
 128  
         }
 129  
 
 130  
         @Override
 131  
         public boolean hasNext() {
 132  2
             return this.iterator.hasNext();
 133  
         }
 134  
 
 135  
         @Override
 136  
         public void remove() {
 137  0
             throw new UnsupportedOperationException("#remove()");
 138  
         }
 139  
     }
 140  
 
 141  
     /**
 142  
      * Trivial iterable that returns FileChangesIterators using
 143  
      * the given JSON list.
 144  
      */
 145  0
     @EqualsAndHashCode(of = { "list" })
 146  
     @Loggable(Loggable.DEBUG)
 147  0
     @ToString
 148  
     private static final class FileChanges
 149  
         implements Iterable<FileChange> {
 150  
         /**
 151  
          * List of file change JSON objects.
 152  
          */
 153  
         private final transient List<JsonObject> list;
 154  
 
 155  
         /**
 156  
          * Ctor.
 157  
          * @param files JsonArray of file change objects
 158  
          */
 159  
         FileChanges(
 160  
             final JsonArray files
 161  3
         ) {
 162  3
             this.list = files.getValuesAs(JsonObject.class);
 163  3
         }
 164  
 
 165  
         @Override
 166  
         public Iterator<FileChange> iterator() {
 167  6
             return new FileChangesIterator(this.list.iterator());
 168  
         }
 169  
     }
 170  
 }