Coverage Report - com.jcabi.github.Limit
 
Classes in this File Line Coverage Branch Coverage Complexity
Limit
N/A
N/A
1
Limit$Smart
61%
8/13
0%
0/12
1
Limit$Smart$AjcClosure1
100%
1/1
N/A
1
Limit$Smart$AjcClosure3
0%
0/1
N/A
1
Limit$Smart$AjcClosure5
100%
1/1
N/A
1
Limit$Smart$AjcClosure7
0%
0/1
N/A
1
Limit$Smart$AjcClosure9
0%
0/1
N/A
1
Limit$Throttled
41%
7/17
0%
0/20
1
Limit$Throttled$AjcClosure1
100%
1/1
N/A
1
Limit$Throttled$AjcClosure3
0%
0/1
N/A
1
 
 1  0
 /**
 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 java.io.IOException;
 35  
 import java.util.Date;
 36  
 import java.util.concurrent.TimeUnit;
 37  
 import javax.json.Json;
 38  
 import javax.json.JsonObject;
 39  
 import lombok.EqualsAndHashCode;
 40  
 import lombok.ToString;
 41  
 
 42  
 /**
 43  
  * Github Rate Limit API, one resource limit.
 44  
  *
 45  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 46  
  * @version $Id: a49d871bee2f1c66485d5733d0ddbcde22ba552b $
 47  
  * @since 0.6
 48  
  * @see <a href="http://developer.github.com/v3/rate_limit/">Rate Limit API</a>
 49  
  * @checkstyle MultipleStringLiterals (500 lines)
 50  
  */
 51  
 @Immutable
 52  
 public interface Limit extends JsonReadable {
 53  
 
 54  
     /**
 55  
      * Github we're in.
 56  
      * @return Github
 57  
      */
 58  
     Github github();
 59  
 
 60  
     /**
 61  
      * Smart limits with extra features.
 62  
      */
 63  
     @Immutable
 64  0
     @ToString
 65  
     @Loggable(Loggable.DEBUG)
 66  0
     @EqualsAndHashCode(of = "origin")
 67  
     final class Smart implements Limit {
 68  
         /**
 69  
          * Encapsulated limit.
 70  
          */
 71  
         private final transient Limit origin;
 72  
         /**
 73  
          * Public ctor.
 74  
          * @param limit Limit
 75  
          */
 76  
         public Smart(
 77  
             final Limit limit
 78  2
         ) {
 79  2
             this.origin = limit;
 80  2
         }
 81  
         /**
 82  
          * Limit of number of requests.
 83  
          * @return Number of requests you can make in total
 84  
          * @throws IOException If it fails
 85  
          */
 86  
         public int limit() throws IOException {
 87  2
             return new SmartJson(this.origin).number("limit");
 88  
         }
 89  
         /**
 90  
          * Remaining number of requests.
 91  
          * @return Number of requests you can still make
 92  
          * @throws IOException If it fails
 93  
          */
 94  
         public int remaining() throws IOException {
 95  0
             return new SmartJson(this.origin).number("remaining");
 96  
         }
 97  
         /**
 98  
          * When will the limit be reset.
 99  
          * @return Date when this will happen
 100  
          * @throws IOException If it fails
 101  
          */
 102  
         public Date reset() throws IOException {
 103  3
             return new Date(
 104  1
                 TimeUnit.MILLISECONDS.convert(
 105  1
                     (long) new SmartJson(this.origin).number("reset"),
 106  
                     TimeUnit.SECONDS
 107  
                 )
 108  
             );
 109  
         }
 110  
         @Override
 111  
         public JsonObject json() throws IOException {
 112  0
             return this.origin.json();
 113  
         }
 114  
         @Override
 115  
         public Github github() {
 116  0
             return this.origin.github();
 117  
         }
 118  
     }
 119  
 
 120  
     /**
 121  
      * Throttled Limit.
 122  
      * @since 0.6
 123  
      */
 124  
     @Immutable
 125  0
     @ToString
 126  
     @Loggable(Loggable.DEBUG)
 127  0
     @EqualsAndHashCode(of = { "origin", "jsn" })
 128  
     final class Throttled implements Limit {
 129  
         /**
 130  
          * Original.
 131  
          */
 132  
         private final transient Limit origin;
 133  
         /**
 134  
          * SmartJson object for convenient JSON parsing.
 135  
          */
 136  
         private final transient SmartJson jsn;
 137  
         /**
 138  
          * Maximum allowed, instead of default 5000.
 139  
          */
 140  
         private final transient int max;
 141  
         /**
 142  
          * Public ctor.
 143  
          * @param limit Original limit
 144  
          * @param allowed Maximum allowed
 145  
          */
 146  
         public Throttled(
 147  
             final Limit limit,
 148  
             final int allowed
 149  1
         ) {
 150  1
             this.origin = limit;
 151  1
             this.max = allowed;
 152  1
             this.jsn = new SmartJson(limit);
 153  1
         }
 154  
         @Override
 155  
         public JsonObject json() throws IOException {
 156  2
             final int limit = new SmartJson(this.origin).number("limit");
 157  0
             final int remaining = this.max - (
 158  0
                 limit - new SmartJson(this.origin).number("remaining")
 159  
                 );
 160  0
             return Json.createObjectBuilder()
 161  0
                 .add("limit", limit)
 162  0
                 .add("remaining", remaining)
 163  0
                 .add("reset", new SmartJson(this.origin).number("reset"))
 164  0
                 .build();
 165  
         }
 166  
         @Override
 167  
         public Github github() {
 168  0
             return this.origin.github();
 169  
         }
 170  
     }
 171  
 
 172  
 }