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