Coverage Report - com.jcabi.github.GistComment
 
Classes in this File Line Coverage Branch Coverage Complexity
GistComment
N/A
N/A
1.375
GistComment$Smart
22%
6/27
0%
0/16
1.375
GistComment$Smart$AjcClosure1
0%
0/1
N/A
1.375
GistComment$Smart$AjcClosure11
0%
0/1
N/A
1.375
GistComment$Smart$AjcClosure13
0%
0/1
N/A
1.375
GistComment$Smart$AjcClosure15
0%
0/1
N/A
1.375
GistComment$Smart$AjcClosure17
0%
0/1
N/A
1.375
GistComment$Smart$AjcClosure19
0%
0/1
N/A
1.375
GistComment$Smart$AjcClosure21
0%
0/1
N/A
1.375
GistComment$Smart$AjcClosure23
0%
0/1
N/A
1.375
GistComment$Smart$AjcClosure3
100%
1/1
N/A
1.375
GistComment$Smart$AjcClosure5
0%
0/1
N/A
1.375
GistComment$Smart$AjcClosure7
0%
0/1
N/A
1.375
GistComment$Smart$AjcClosure9
0%
0/1
N/A
1.375
 
 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.net.URL;
 36  
 import java.text.ParseException;
 37  
 import java.util.Date;
 38  
 import javax.json.Json;
 39  
 import javax.json.JsonObject;
 40  
 import javax.validation.constraints.NotNull;
 41  
 import lombok.EqualsAndHashCode;
 42  
 import lombok.ToString;
 43  
 
 44  
 /**
 45  
  * Gist comment.
 46  
  *
 47  
  * <p>Gist Comment implements {@link JsonReadable}, that's how you can get
 48  
  * its full details in JSON format.
 49  
  * For example, to get its author's Github login
 50  
  * you get the entire JSON and then gets its element:
 51  
  *
 52  
  * <pre>String login = comment.json()
 53  
  *   .getJsonObject("user")
 54  
  *   .getString("login");</pre>
 55  
  *
 56  
  * <p>However, it's better to use a supplementary "smart" decorator, which
 57  
  * automates most of these operations:
 58  
  *
 59  
  * <pre>String login = new GistComment.Smart(comment).author().login();</pre>
 60  
  *
 61  
  * @author Giang Le (giang@vn-smartsolutions.com)
 62  
  * @version $Id$
 63  
  * @since 0.8
 64  
  * @see <a href="http://developer.github.com/v3/gists/comments/">Gist Comments API</a>
 65  
  * @checkstyle MultipleStringLiterals (500 lines)
 66  
  */
 67  
 @Immutable
 68  
 @SuppressWarnings("PMD.TooManyMethods")
 69  
 public interface GistComment
 70  
     extends Comparable<GistComment>, JsonReadable, JsonPatchable {
 71  
     /**
 72  
      * The gist it's in.
 73  
      * @return Owner of the comment
 74  
      */
 75  
     @NotNull(message = "Gist is never NULL")
 76  
     Gist gist();
 77  
 
 78  
     /**
 79  
      * Number.
 80  
      * @return Comment id
 81  
      */
 82  
     int number();
 83  
 
 84  
     /**
 85  
      * Delete the comment.
 86  
      * @throws java.io.IOException If there is any I/O problem
 87  
      * @see <a href="http://developer.github.com/v3/gists/comments/#delete-a-comment">Delete a Comment</a>
 88  
      */
 89  
     void remove() throws IOException;
 90  
 
 91  
     /**
 92  
      * Smart comment with additional features.
 93  
      */
 94  0
     @Immutable
 95  0
     @ToString
 96  
     @Loggable(Loggable.DEBUG)
 97  0
     @EqualsAndHashCode(of = { "comment", "jsn" })
 98  
     final class Smart implements GistComment {
 99  
         /**
 100  
          * Encapsulated gist comment.
 101  
          */
 102  
         private final transient GistComment comment;
 103  
         /**
 104  
          * SmartJson object for convenient JSON parsing.
 105  
          */
 106  
         private final transient SmartJson jsn;
 107  
 
 108  
         /**
 109  
          * Public ctor.
 110  
          * @param cmt Comment
 111  
          */
 112  
         public Smart(
 113  
             @NotNull(message = "cmt can't be NULL") final GistComment cmt
 114  2
         ) {
 115  2
             this.comment = cmt;
 116  2
             this.jsn = new SmartJson(cmt);
 117  2
         }
 118  
 
 119  
         /**
 120  
          * Get its author.
 121  
          * @return Author of comment
 122  
          * @throws IOException If there is any I/O problem
 123  
          */
 124  
         @NotNull(message = "user is never NULL")
 125  
         public User author() throws IOException {
 126  0
             return this.comment.gist().github().users().get(
 127  
                 this.comment.json().getJsonObject("user").getString("login")
 128  
             );
 129  
         }
 130  
 
 131  
         /**
 132  
          * Get its body.
 133  
          * @return Body of comment
 134  
          * @throws IOException If there is any I/O problem
 135  
          */
 136  
         @NotNull(message = "body is never NULL")
 137  
         public String body() throws IOException {
 138  4
             return this.jsn.text("body");
 139  
         }
 140  
 
 141  
         /**
 142  
          * Change comment body.
 143  
          * @param text Body of comment
 144  
          * @throws IOException If there is any I/O problem
 145  
          */
 146  
         public void body(
 147  
             @NotNull(message = "text can't be NULL") final String text
 148  
         ) throws IOException {
 149  0
             this.comment.patch(
 150  
                 Json.createObjectBuilder().add("body", text).build()
 151  
             );
 152  0
         }
 153  
 
 154  
         /**
 155  
          * Get its URL.
 156  
          * @return URL of comment
 157  
          * @throws IOException If there is any I/O problem
 158  
          */
 159  
         @NotNull(message = "URL can't be NULL")
 160  
         public URL url() throws IOException {
 161  0
             return new URL(this.jsn.text("url"));
 162  
         }
 163  
 
 164  
         /**
 165  
          * When this comment was created.
 166  
          * @return Date of creation
 167  
          * @throws IOException If there is any I/O problem
 168  
          */
 169  
         @NotNull(message = "date can't be NULL")
 170  
         public Date createdAt() throws IOException {
 171  
             try {
 172  0
                 return new Github.Time(
 173  
                     this.jsn.text("created_at")
 174  
                 ).date();
 175  0
             } catch (final ParseException ex) {
 176  0
                 throw new IOException(ex);
 177  
             }
 178  
         }
 179  
 
 180  
         /**
 181  
          * When this comment was updated last time.
 182  
          * @return Date of update
 183  
          * @throws IOException If there is any I/O problem
 184  
          */
 185  
         @NotNull(message = "date can't be NULL")
 186  
         public Date updatedAt() throws IOException {
 187  
             try {
 188  0
                 return new Github.Time(
 189  
                     this.jsn.text("updated_at")
 190  
                 ).date();
 191  0
             } catch (final ParseException ex) {
 192  0
                 throw new IOException(ex);
 193  
             }
 194  
         }
 195  
 
 196  
         @Override
 197  
         @NotNull(message = "gist is never NULL")
 198  
         public Gist gist() {
 199  0
             return this.comment.gist();
 200  
         }
 201  
 
 202  
         @Override
 203  
         public int number() {
 204  0
             return this.comment.number();
 205  
         }
 206  
 
 207  
         @Override
 208  
         public void remove() throws IOException {
 209  0
             this.comment.remove();
 210  0
         }
 211  
 
 212  
         @Override
 213  
         public int compareTo(
 214  
             @NotNull(message = "cmt can't be NULL") final GistComment cmt
 215  
         ) {
 216  0
             return this.comment.compareTo(cmt);
 217  
         }
 218  
 
 219  
         @Override
 220  
         public void patch(
 221  
             @NotNull(message = "json can't be NULL") final JsonObject json
 222  
         ) throws IOException {
 223  0
             this.comment.patch(json);
 224  0
         }
 225  
 
 226  
         @Override
 227  
         @NotNull(message = "JSON is never NULL")
 228  
         public JsonObject json() throws IOException {
 229  0
             return this.comment.json();
 230  
         }
 231  
     }
 232  
 }