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