Make data objects clonable (will not copy images)
[nikiroo-utils.git] / src / be / nikiroo / fanfix / data / Story.java
index cb65119149414a2eef6a9db75aca333950e4e36e..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>();
@@ -55,6 +55,7 @@ public class Story implements Iterable<Chapter> {
        /**
         * Get an iterator on the {@link Chapter}s.
         */
+       @Override
        public Iterator<Chapter> iterator() {
                return chapters == null ? empty.iterator() : chapters.iterator();
        }
@@ -100,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;
+       }
 }