Make all classes in be.nikiroo.fanfix.data Serializable
[fanfix.git] / src / be / nikiroo / fanfix / data / Chapter.java
index 551606348843446c00ddc59fc53827e7a3e35aa9..d490058a4703b51e042bb369effdf4b4fa511be6 100644 (file)
@@ -1,5 +1,6 @@
 package be.nikiroo.fanfix.data;
 
+import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -9,11 +10,22 @@ import java.util.List;
  * 
  * @author niki
  */
-public class Chapter implements Iterable<Paragraph> {
+public class Chapter implements Iterable<Paragraph>, Cloneable, Serializable {
+       private static final long serialVersionUID = 1L;
+       
        private String name;
        private int number;
        private List<Paragraph> paragraphs = new ArrayList<Paragraph>();
        private List<Paragraph> empty = new ArrayList<Paragraph>();
+       private long words;
+
+       /**
+        * Empty constructor, not to use.
+        */
+       @SuppressWarnings("unused")
+       private Chapter() {
+               // for serialisation purposes
+       }
 
        /**
         * Create a new {@link Chapter} with the given information.
@@ -78,7 +90,7 @@ public class Chapter implements Iterable<Paragraph> {
        /**
         * The included paragraphs.
         * 
-        * @param paragraphes
+        * @param paragraphs
         *            the paragraphs to set
         */
        public void setParagraphs(List<Paragraph> paragraphs) {
@@ -88,10 +100,30 @@ 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 (or images) in this {@link Chapter}.
+        * 
+        * @return the number of words
+        */
+       public long getWords() {
+               return words;
+       }
+
+       /**
+        * The number of words (or images) in this {@link Chapter}.
+        * 
+        * @param words
+        *            the number of words to set
+        */
+       public void setWords(long words) {
+               this.words = words;
+       }
+
        /**
         * Display a DEBUG {@link String} representation of this object.
         */
@@ -99,4 +131,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;
+       }
 }