Coverage Report - com.jcabi.github.Limit
 
Classes in this File Line Coverage Branch Coverage Complexity
Limit
N/A
N/A
1
Limit$Smart
45%
5/11
0%
0/10
1
Limit$Smart$AjcClosure1
100%
1/1
N/A
1
Limit$Smart$AjcClosure3
0%
0/1
N/A
1
Limit$Smart$AjcClosure5
0%
0/1
N/A
1
Limit$Smart$AjcClosure7
0%
0/1
N/A
1
Limit$Smart$AjcClosure9
0%
0/1
N/A
1
Limit$Throttled
58%
7/12
0%
0/16
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-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.aspects.Loggable;
 34  
 import java.io.IOException;
 35  
 import java.util.Date;
 36  
 import javax.json.Json;
 37  
 import javax.json.JsonObject;
 38  
 import javax.validation.constraints.NotNull;
 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$
 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  
     @NotNull(message = "Github is never NULL")
 59  
     Github github();
 60  
 
 61  
     /**
 62  
      * Smart limits with extra features.
 63  
      */
 64  
     @Immutable
 65  0
     @ToString
 66  
     @Loggable(Loggable.DEBUG)
 67  0
     @EqualsAndHashCode(of = "origin")
 68  
     final class Smart implements Limit {
 69  
         /**
 70  
          * Encapsulated limit.
 71  
          */
 72  
         private final transient Limit origin;
 73  
         /**
 74  
          * Public ctor.
 75  
          * @param limit Limit
 76  
          */
 77  
         public Smart(
 78  
             @NotNull(message = "limit can't be NULL") final Limit limit
 79  1
         ) {
 80  1
             this.origin = limit;
 81  1
         }
 82  
         /**
 83  
          * Limit of number of requests.
 84  
          * @return Number of requests you can make in total
 85  
          * @throws IOException If it fails
 86  
          */
 87  
         public int limit() throws IOException {
 88  2
             return new SmartJson(this.origin).number("limit");
 89  
         }
 90  
         /**
 91  
          * Remaining number of requests.
 92  
          * @return Number of requests you can still make
 93  
          * @throws IOException If it fails
 94  
          */
 95  
         public int remaining() throws IOException {
 96  0
             return new SmartJson(this.origin).number("remaining");
 97  
         }
 98  
         /**
 99  
          * When will the limit be reset.
 100  
          * @return Date when this will happen
 101  
          * @throws IOException If it fails
 102  
          */
 103  
         @NotNull(message = "date is never NULL")
 104  
         public Date reset() throws IOException {
 105  0
             return new Date(
 106  
                 (long) new SmartJson(this.origin).number("reset")
 107  
             );
 108  
         }
 109  
         @Override
 110  
         @NotNull(message = "JSON is never NULL")
 111  
         public JsonObject json() throws IOException {
 112  0
             return this.origin.json();
 113  
         }
 114  
         @Override
 115  
         @NotNull(message = "github is never NULL")
 116  
         public Github github() {
 117  0
             return this.origin.github();
 118  
         }
 119  
     }
 120  
 
 121  
     /**
 122  
      * Throttled Limit.
 123  
      * @since 0.6
 124  
      */
 125  
     @Immutable
 126  0
     @ToString
 127  
     @Loggable(Loggable.DEBUG)
 128  0
     @EqualsAndHashCode(of = { "origin", "jsn" })
 129  
     final class Throttled implements Limit {
 130  
         /**
 131  
          * Original.
 132  
          */
 133  
         private final transient Limit origin;
 134  
         /**
 135  
          * SmartJson object for convenient JSON parsing.
 136  
          */
 137  
         private final transient SmartJson jsn;
 138  
         /**
 139  
          * Maximum allowed, instead of default 5000.
 140  
          */
 141  
         private final transient int max;
 142  
         /**
 143  
          * Public ctor.
 144  
          * @param limit Original limit
 145  
          * @param allowed Maximum allowed
 146  
          */
 147  
         public Throttled(
 148  
             @NotNull(message = "limit is never NULL") final Limit limit,
 149  
             final int allowed
 150  1
         ) {
 151  1
             this.origin = limit;
 152  1
             this.max = allowed;
 153  1
             this.jsn = new SmartJson(limit);
 154  1
         }
 155  
         @Override
 156  
         @NotNull(message = "JSON is never NULL")
 157  
         public JsonObject json() throws IOException {
 158  2
             final int limit = new SmartJson(this.origin).number("limit");
 159  0
             final int remaining = this.max - (
 160  
                 limit - new SmartJson(this.origin).number("remaining")
 161  
                 );
 162  0
             return Json.createObjectBuilder()
 163  
                 .add("limit", limit)
 164  
                 .add("remaining", remaining)
 165  
                 .add("reset", new SmartJson(this.origin).number("reset"))
 166  
                 .build();
 167  
         }
 168  
         @Override
 169  
         @NotNull(message = "github is never NULL")
 170  
         public Github github() {
 171  0
             return this.origin.github();
 172  
         }
 173  
     }
 174  
 
 175  
 }