From 72f39832a7b425003c1d717afcfb5a9cf7d3405b Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Thu, 22 Mar 2018 13:13:32 +0100 Subject: [PATCH] New getComentById() method --- src/be/nikiroo/gofetch/data/Comment.java | 24 ++++++++++++++++++++++++ src/be/nikiroo/gofetch/data/Story.java | 21 +++++++++++++++++++++ 2 files changed, 45 insertions(+) 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 -- 2.27.0