New getComentById() method
authorNiki Roo <niki@nikiroo.be>
Thu, 22 Mar 2018 12:13:32 +0000 (13:13 +0100)
committerNiki Roo <niki@nikiroo.be>
Thu, 22 Mar 2018 12:13:32 +0000 (13:13 +0100)
src/be/nikiroo/gofetch/data/Comment.java
src/be/nikiroo/gofetch/data/Story.java

index 963d6aa6d6dffe427d5bab979fc20cd1790550e7..bbd648a36a81be826af4b574b87f4a8db9137f53 100644 (file)
@@ -65,6 +65,30 @@ public class Comment implements Iterable<Comment> {
                return lines;
        }
 
+       /**
+        * Find a comment or sub-comment by its id.
+        * 
+        * @param id
+        *            the id to look for F
+        * @return this if it has the given id, or a child of this if the child have
+        *         the given id, or NULL if not
+        */
+       public Comment getById(String id) {
+               if (id != null) {
+                       if (id.equals(this.id)) {
+                               return this;
+                       }
+
+                       for (Comment subComment : this) {
+                               if (id.equals(subComment.getId())) {
+                                       return subComment;
+                               }
+                       }
+               }
+
+               return null;
+       }
+
        public boolean isEmpty() {
                return children.isEmpty() && lines.isEmpty()
                                && ("" + author + title).trim().isEmpty();
index 9fd4c1495b882ccf8591489cb901137b01271418..a2ad7d3b9e3a3660d18c951c6c3904b49a91c6b6 100644 (file)
@@ -130,4 +130,25 @@ public class Story {
        public void setComments(List<Comment> comments) {
                this.comments = comments;
        }
+
+       /**
+        * Find a comment or sub-comment by its id.
+        * 
+        * @param id
+        *            the id to look for F
+        * @return this if it has the given id, or a child of this if the child have
+        *         the given id, or NULL if not
+        */
+       public Comment getCommentById(String id) {
+               if (id != null && comments != null) {
+                       for (Comment comment : comments) {
+                               Comment found = comment.getById(id);
+                               if (found != null) {
+                                       return found;
+                               }
+                       }
+               }
+
+               return null;
+       }
 }
\ No newline at end of file