Coverage Report - com.jcabi.github.PublicKey
 
Classes in this File Line Coverage Branch Coverage Complexity
PublicKey
N/A
N/A
1
PublicKey$Smart
63%
12/19
0%
0/16
1
PublicKey$Smart$AjcClosure1
100%
1/1
N/A
1
PublicKey$Smart$AjcClosure11
0%
0/1
N/A
1
PublicKey$Smart$AjcClosure13
0%
0/1
N/A
1
PublicKey$Smart$AjcClosure15
0%
0/1
N/A
1
PublicKey$Smart$AjcClosure17
0%
0/1
N/A
1
PublicKey$Smart$AjcClosure3
100%
1/1
N/A
1
PublicKey$Smart$AjcClosure5
100%
1/1
N/A
1
PublicKey$Smart$AjcClosure7
100%
1/1
N/A
1
PublicKey$Smart$AjcClosure9
100%
1/1
N/A
1
 
 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.aspects.Loggable;
 34  
 import java.io.IOException;
 35  
 import java.net.URL;
 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 public key.
 44  
  *
 45  
  * <p>PublicKey implements {@link JsonReadable}, that's how you can get its full
 46  
  * details in JSON format. For example, to get its title,
 47  
  * you get the entire JSON and then gets its element:
 48  
  *
 49  
  * <pre>String title = key.json().getString("title");</pre>
 50  
  *
 51  
  * <p>However, it's better to use a supplementary "smart" decorator, which
 52  
  * automates most of these operations:
 53  
  *
 54  
  * <pre>String title = new PublicKey.Smart(comment).title();</pre>
 55  
  *
 56  
  * @author Carlos Miranda (miranda.cma@gmail.com)
 57  
  * @version $Id$
 58  
  * @see <a href="http://developer.github.com/v3/users/keys/">Public Keys API</a>
 59  
  */
 60  
 @Immutable
 61  
 @SuppressWarnings("PMD.TooManyMethods")
 62  
 public interface PublicKey extends JsonReadable, JsonPatchable {
 63  
 
 64  
     /**
 65  
      * User we're in.
 66  
      *
 67  
      * @return User
 68  
      */
 69  
     @NotNull(message = "user is never NULL")
 70  
     User user();
 71  
 
 72  
     /**
 73  
      * ID Number of this public key.
 74  
      * @return Public key ID number
 75  
      */
 76  
     int number();
 77  
 
 78  
     /**
 79  
      * Smart PublicKey with extra features.
 80  
      * @checkstyle MultipleStringLiterals (500 lines)
 81  
      */
 82  
     @Immutable
 83  0
     @ToString
 84  
     @Loggable(Loggable.DEBUG)
 85  0
     @EqualsAndHashCode(of = { "key", "jsn" })
 86  
     final class Smart implements PublicKey {
 87  
 
 88  
         /**
 89  
          * Encapsulated public key.
 90  
          */
 91  
         private final transient PublicKey key;
 92  
 
 93  
         /**
 94  
          * SmartJson object for convenient JSON parsing.
 95  
          */
 96  
         private final transient SmartJson jsn;
 97  
 
 98  
         /**
 99  
          * Public ctor.
 100  
          * @param pkey Public key
 101  
          */
 102  
         public Smart(
 103  
             @NotNull(message = "public key is never NULL") final PublicKey pkey
 104  5
         ) {
 105  5
             this.key = pkey;
 106  5
             this.jsn = new SmartJson(pkey);
 107  5
         }
 108  
 
 109  
         /**
 110  
          * Get its key value.
 111  
          * @return Value of public key
 112  
          * @throws IOException If there is any I/O problem
 113  
          */
 114  
         @NotNull(message = "key is never NULL")
 115  
         public String key() throws IOException {
 116  2
             return this.jsn.text("key");
 117  
         }
 118  
 
 119  
         /**
 120  
          * Change its value.
 121  
          * @param value Title of public key
 122  
          * @throws IOException If there is any I/O problem
 123  
          */
 124  
         public void key(
 125  
             @NotNull(message = "value can't be NULL") final String value
 126  
         ) throws IOException {
 127  2
             this.key.patch(
 128  
                 Json.createObjectBuilder().add("key", value).build()
 129  
             );
 130  1
         }
 131  
 
 132  
         /**
 133  
          * Get its URL.
 134  
          * @return URL of public key
 135  
          * @throws IOException If there is any I/O problem
 136  
          */
 137  
         @NotNull(message = "url is never NULL")
 138  
         public URL url() throws IOException {
 139  2
             return new URL(this.jsn.text("url"));
 140  
         }
 141  
 
 142  
         /**
 143  
          * Get its title.
 144  
          * @return Title of public key
 145  
          * @throws IOException If there is any I/O problem
 146  
          */
 147  
         @NotNull(message = "title is never NULL")
 148  
         public String title() throws IOException {
 149  2
             return this.jsn.text("title");
 150  
         }
 151  
 
 152  
         /**
 153  
          * Change its title.
 154  
          * @param text Title of public key
 155  
          * @throws IOException If there is any I/O problem
 156  
          */
 157  
         public void title(
 158  
             @NotNull(message = "text can't be NULL") final String text
 159  
         ) throws IOException {
 160  2
             this.key.patch(
 161  
                 Json.createObjectBuilder().add("title", text).build()
 162  
             );
 163  1
         }
 164  
 
 165  
         @Override
 166  
         @NotNull(message = "JSON is never NULL")
 167  
         public JsonObject json() throws IOException {
 168  0
             return this.key.json();
 169  
         }
 170  
 
 171  
         @Override
 172  
         public void patch(
 173  
             @NotNull(message = "JSON is never NULL") final JsonObject json
 174  
         ) throws IOException {
 175  0
             this.key.patch(json);
 176  0
         }
 177  
 
 178  
         @Override
 179  
         @NotNull(message = "user is never NULL")
 180  
         public User user() {
 181  0
             return this.key.user();
 182  
         }
 183  
 
 184  
         @Override
 185  
         public int number() {
 186  0
             return this.key.number();
 187  
         }
 188  
     }
 189  
 }