From: Niki Roo Date: Thu, 22 Mar 2018 12:13:32 +0000 (+0100) Subject: New getComentById() method X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=72f39832a7b425003c1d717afcfb5a9cf7d3405b;p=gofetch.git New getComentById() method --- diff --git a/src/be/nikiroo/gofetch/data/Comment.java b/src/be/nikiroo/gofetch/data/Comment.java index 963d6aa..bbd648a 100644 --- a/src/be/nikiroo/gofetch/data/Comment.java +++ b/src/be/nikiroo/gofetch/data/Comment.java @@ -65,6 +65,30 @@ public class Comment implements Iterable { 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(); diff --git a/src/be/nikiroo/gofetch/data/Story.java b/src/be/nikiroo/gofetch/data/Story.java index 9fd4c14..a2ad7d3 100644 --- a/src/be/nikiroo/gofetch/data/Story.java +++ b/src/be/nikiroo/gofetch/data/Story.java @@ -130,4 +130,25 @@ public class Story { public void setComments(List 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