From: Niki Roo Date: Wed, 29 Nov 2017 22:08:49 +0000 (+0100) Subject: Make data objects clonable (will not copy images) X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=39cd9738bfcdafaf8806d5007e443e572f8994c0;p=nikiroo-utils.git Make data objects clonable (will not copy images) --- diff --git a/src/be/nikiroo/fanfix/data/Chapter.java b/src/be/nikiroo/fanfix/data/Chapter.java index 832994a..873dcb8 100644 --- a/src/be/nikiroo/fanfix/data/Chapter.java +++ b/src/be/nikiroo/fanfix/data/Chapter.java @@ -9,7 +9,7 @@ import java.util.List; * * @author niki */ -public class Chapter implements Iterable { +public class Chapter implements Iterable, Cloneable { private String name; private int number; private List paragraphs = new ArrayList(); @@ -128,4 +128,24 @@ public class Chapter implements Iterable { public String toString() { return "Chapter " + number + ": " + name; } + + @Override + public Chapter clone() { + Chapter chap = null; + try { + chap = (Chapter) super.clone(); + } catch (CloneNotSupportedException e) { + // Did the clones rebel? + System.err.println(e); + } + + if (paragraphs != null) { + chap.paragraphs = new ArrayList(); + for (Paragraph para : paragraphs) { + chap.paragraphs.add(para.clone()); + } + } + + return chap; + } } diff --git a/src/be/nikiroo/fanfix/data/MetaData.java b/src/be/nikiroo/fanfix/data/MetaData.java index c969ec9..8185ddd 100644 --- a/src/be/nikiroo/fanfix/data/MetaData.java +++ b/src/be/nikiroo/fanfix/data/MetaData.java @@ -394,14 +394,11 @@ public class MetaData implements Cloneable, Comparable { } if (tags != null) { - meta.tags = new ArrayList(); - meta.tags.addAll(tags); + meta.tags = new ArrayList(tags); } + if (resume != null) { - meta.resume = new Chapter(resume.getNumber(), resume.getName()); - for (Paragraph para : resume) { - meta.resume.getParagraphs().add(para); - } + meta.resume = resume.clone(); } return meta; diff --git a/src/be/nikiroo/fanfix/data/Paragraph.java b/src/be/nikiroo/fanfix/data/Paragraph.java index a731c32..7a1a1c4 100644 --- a/src/be/nikiroo/fanfix/data/Paragraph.java +++ b/src/be/nikiroo/fanfix/data/Paragraph.java @@ -7,7 +7,7 @@ import java.awt.image.BufferedImage; * * @author niki */ -public class Paragraph { +public class Paragraph implements Cloneable { /** * A paragraph type, that will dictate how the paragraph will be handled. * @@ -139,4 +139,17 @@ public class Paragraph { public String toString() { return String.format("%s: [%s]", "" + type, "" + content); } + + @Override + public Paragraph clone() { + Paragraph para = null; + try { + para = (Paragraph) super.clone(); + } catch (CloneNotSupportedException e) { + // Did the clones rebel? + System.err.println(e); + } + + return para; + } } diff --git a/src/be/nikiroo/fanfix/data/Story.java b/src/be/nikiroo/fanfix/data/Story.java index 7ebfbc5..7e58571 100644 --- a/src/be/nikiroo/fanfix/data/Story.java +++ b/src/be/nikiroo/fanfix/data/Story.java @@ -9,7 +9,7 @@ import java.util.List; * * @author niki */ -public class Story implements Iterable { +public class Story implements Iterable, Cloneable { private MetaData meta; private List chapters = new ArrayList(); private List empty = new ArrayList(); @@ -101,4 +101,28 @@ public class Story implements Iterable { : meta.getAuthor(), meta == null ? "" : meta.getDate(), tags, resume, cover); } + + @Override + public Story clone() { + Story story = null; + try { + story = (Story) super.clone(); + } catch (CloneNotSupportedException e) { + // Did the clones rebel? + System.err.println(e); + } + + if (meta != null) { + story.meta = meta.clone(); + } + + if (chapters != null) { + story.chapters = new ArrayList(); + for (Chapter chap : chapters) { + story.chapters.add(chap.clone()); + } + } + + return story; + } }