Make data objects clonable (will not copy images)
[nikiroo-utils.git] / src / be / nikiroo / fanfix / data / Story.java
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;
+       }
 }