Merge branch 'subtree'
[fanfix.git] / src / be / nikiroo / fanfix / data / Chapter.java
1 package be.nikiroo.fanfix.data;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.Iterator;
6 import java.util.List;
7
8 /**
9 * A chapter in the story (or the resume/description).
10 *
11 * @author niki
12 */
13 public class Chapter implements Iterable<Paragraph>, Cloneable, Serializable {
14 private static final long serialVersionUID = 1L;
15
16 private String name;
17 private int number;
18 private List<Paragraph> paragraphs = new ArrayList<Paragraph>();
19 private List<Paragraph> empty = new ArrayList<Paragraph>();
20 private long words;
21
22 /**
23 * Empty constructor, not to use.
24 */
25 @SuppressWarnings("unused")
26 private Chapter() {
27 // for serialisation purposes
28 }
29
30 /**
31 * Create a new {@link Chapter} with the given information.
32 *
33 * @param number
34 * the chapter number, or 0 for the description/resume.
35 * @param name
36 * the chapter name
37 */
38 public Chapter(int number, String name) {
39 this.number = number;
40 this.name = name;
41 }
42
43 /**
44 * The chapter name.
45 *
46 * @return the name
47 */
48 public String getName() {
49 return name;
50 }
51
52 /**
53 * The chapter name.
54 *
55 * @param name
56 * the name to set
57 */
58 public void setName(String name) {
59 this.name = name;
60 }
61
62 /**
63 * The chapter number, or 0 for the description/resume.
64 *
65 * @return the number
66 */
67 public int getNumber() {
68 return number;
69 }
70
71 /**
72 * The chapter number, or 0 for the description/resume.
73 *
74 * @param number
75 * the number to set
76 */
77 public void setNumber(int number) {
78 this.number = number;
79 }
80
81 /**
82 * The included paragraphs.
83 *
84 * @return the paragraphs
85 */
86 public List<Paragraph> getParagraphs() {
87 return paragraphs;
88 }
89
90 /**
91 * The included paragraphs.
92 *
93 * @param paragraphs
94 * the paragraphs to set
95 */
96 public void setParagraphs(List<Paragraph> paragraphs) {
97 this.paragraphs = paragraphs;
98 }
99
100 /**
101 * Get an iterator on the {@link Paragraph}s.
102 */
103 @Override
104 public Iterator<Paragraph> iterator() {
105 return paragraphs == null ? empty.iterator() : paragraphs.iterator();
106 }
107
108 /**
109 * The number of words (or images) in this {@link Chapter}.
110 *
111 * @return the number of words
112 */
113 public long getWords() {
114 return words;
115 }
116
117 /**
118 * The number of words (or images) in this {@link Chapter}.
119 *
120 * @param words
121 * the number of words to set
122 */
123 public void setWords(long words) {
124 this.words = words;
125 }
126
127 /**
128 * Display a DEBUG {@link String} representation of this object.
129 */
130 @Override
131 public String toString() {
132 return "Chapter " + number + ": " + name;
133 }
134
135 @Override
136 public Chapter clone() {
137 Chapter chap = null;
138 try {
139 chap = (Chapter) super.clone();
140 } catch (CloneNotSupportedException e) {
141 // Did the clones rebel?
142 System.err.println(e);
143 }
144
145 if (paragraphs != null) {
146 chap.paragraphs = new ArrayList<Paragraph>();
147 for (Paragraph para : paragraphs) {
148 chap.paragraphs.add(para.clone());
149 }
150 }
151
152 return chap;
153 }
154 }