Merge branch 'subtree'
[nikiroo-utils.git] / src / be / nikiroo / fanfix / data / Story.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 * The main data class, where the whole story resides.
10 *
11 * @author niki
12 */
13 public class Story implements Iterable<Chapter>, Cloneable, Serializable {
14 private static final long serialVersionUID = 1L;
15
16 private MetaData meta;
17 private List<Chapter> chapters = new ArrayList<Chapter>();
18 private List<Chapter> empty = new ArrayList<Chapter>();
19
20 /**
21 * The metadata about this {@link Story}.
22 *
23 * @return the meta
24 */
25 public MetaData getMeta() {
26 return meta;
27 }
28
29 /**
30 * The metadata about this {@link Story}.
31 *
32 * @param meta
33 * the meta to set
34 */
35 public void setMeta(MetaData meta) {
36 this.meta = meta;
37 }
38
39 /**
40 * The chapters of the story.
41 *
42 * @return the chapters
43 */
44 public List<Chapter> getChapters() {
45 return chapters;
46 }
47
48 /**
49 * The chapters of the story.
50 *
51 * @param chapters
52 * the chapters to set
53 */
54 public void setChapters(List<Chapter> chapters) {
55 this.chapters = chapters;
56 }
57
58 /**
59 * Get an iterator on the {@link Chapter}s.
60 */
61 @Override
62 public Iterator<Chapter> iterator() {
63 return chapters == null ? empty.iterator() : chapters.iterator();
64 }
65
66 /**
67 * Display a DEBUG {@link String} representation of this object.
68 * <p>
69 * This is not efficient, nor intended to be.
70 */
71 @Override
72 public String toString() {
73 if (getMeta() != null)
74 return "Story: [\n" + getMeta().toString() + "\n]";
75 return "Story: [ no metadata found ]";
76 }
77
78 @Override
79 public Story clone() {
80 Story story = null;
81 try {
82 story = (Story) super.clone();
83 } catch (CloneNotSupportedException e) {
84 // Did the clones rebel?
85 System.err.println(e);
86 }
87
88 if (meta != null) {
89 story.meta = meta.clone();
90 }
91
92 if (chapters != null) {
93 story.chapters = new ArrayList<Chapter>();
94 for (Chapter chap : chapters) {
95 story.chapters.add(chap.clone());
96 }
97 }
98
99 return story;
100 }
101 }