Make data objects clonable (will not copy images)
authorNiki Roo <niki@nikiroo.be>
Wed, 29 Nov 2017 22:08:49 +0000 (23:08 +0100)
committerNiki Roo <niki@nikiroo.be>
Wed, 29 Nov 2017 22:08:49 +0000 (23:08 +0100)
src/be/nikiroo/fanfix/data/Chapter.java
src/be/nikiroo/fanfix/data/MetaData.java
src/be/nikiroo/fanfix/data/Paragraph.java
src/be/nikiroo/fanfix/data/Story.java

index 832994a937d4379d8c3d2105344d8ed2f67eb241..873dcb801f89b03f5ca648bd9c2f67f84ab761f6 100644 (file)
@@ -9,7 +9,7 @@ import java.util.List;
  * 
  * @author niki
  */
-public class Chapter implements Iterable<Paragraph> {
+public class Chapter implements Iterable<Paragraph>, Cloneable {
        private String name;
        private int number;
        private List<Paragraph> paragraphs = new ArrayList<Paragraph>();
@@ -128,4 +128,24 @@ public class Chapter implements Iterable<Paragraph> {
        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<Paragraph>();
+                       for (Paragraph para : paragraphs) {
+                               chap.paragraphs.add(para.clone());
+                       }
+               }
+
+               return chap;
+       }
 }
index c969ec9366f5c7d7e45a1ae5e3701b6b4a521020..8185ddd235340c07e814eae3665464a056762f3d 100644 (file)
@@ -394,14 +394,11 @@ public class MetaData implements Cloneable, Comparable<MetaData> {
                }
 
                if (tags != null) {
-                       meta.tags = new ArrayList<String>();
-                       meta.tags.addAll(tags);
+                       meta.tags = new ArrayList<String>(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;
index a731c328ce0a2ef4a0bc373689fe275c2abfb663..7a1a1c448322212cbd09cebdc7180b0f8912b6c4 100644 (file)
@@ -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;
+       }
 }
index 7ebfbc598d945e1502352872daa8b0e55dbbbaa0..7e585712c8db7d5001047c648045b0fb3f229635 100644 (file)
@@ -9,7 +9,7 @@ import java.util.List;
  * 
  * @author niki
  */
-public class Story implements Iterable<Chapter> {
+public class Story implements Iterable<Chapter>, Cloneable {
        private MetaData meta;
        private List<Chapter> chapters = new ArrayList<Chapter>();
        private List<Chapter> empty = new ArrayList<Chapter>();
@@ -101,4 +101,28 @@ public class Story implements Iterable<Chapter> {
                                                : 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<Chapter>();
+                       for (Chapter chap : chapters) {
+                               story.chapters.add(chap.clone());
+                       }
+               }
+
+               return story;
+       }
 }