Make data objects clonable (will not copy images)
[nikiroo-utils.git] / src / be / nikiroo / fanfix / data / Chapter.java
index d9f285f96762ce135b8046578f0df9d75d31b819..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>();
@@ -19,6 +19,7 @@ public class Chapter implements Iterable<Paragraph> {
        /**
         * Empty constructor, not to use.
         */
+       @SuppressWarnings("unused")
        private Chapter() {
                // for serialisation purposes
        }
@@ -96,12 +97,13 @@ public class Chapter implements Iterable<Paragraph> {
        /**
         * Get an iterator on the {@link Paragraph}s.
         */
+       @Override
        public Iterator<Paragraph> iterator() {
                return paragraphs == null ? empty.iterator() : paragraphs.iterator();
        }
 
        /**
-        * The number of words in this {@link Chapter}.
+        * The number of words (or images) in this {@link Chapter}.
         * 
         * @return the number of words
         */
@@ -110,7 +112,7 @@ public class Chapter implements Iterable<Paragraph> {
        }
 
        /**
-        * The number of words in this {@link Chapter}.
+        * The number of words (or images) in this {@link Chapter}.
         * 
         * @param words
         *            the number of words to set
@@ -126,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;
+       }
 }