| 1 | package be.nikiroo.fanfix.data; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Iterator; |
| 5 | import java.util.List; |
| 6 | |
| 7 | /** |
| 8 | * The main data class, where the whole story resides. |
| 9 | * |
| 10 | * @author niki |
| 11 | */ |
| 12 | public class Story implements Iterable<Chapter>, Cloneable { |
| 13 | private MetaData meta; |
| 14 | private List<Chapter> chapters = new ArrayList<Chapter>(); |
| 15 | private List<Chapter> empty = new ArrayList<Chapter>(); |
| 16 | |
| 17 | /** |
| 18 | * The metadata about this {@link Story}. |
| 19 | * |
| 20 | * @return the meta |
| 21 | */ |
| 22 | public MetaData getMeta() { |
| 23 | return meta; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * The metadata about this {@link Story}. |
| 28 | * |
| 29 | * @param meta |
| 30 | * the meta to set |
| 31 | */ |
| 32 | public void setMeta(MetaData meta) { |
| 33 | this.meta = meta; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * The chapters of the story. |
| 38 | * |
| 39 | * @return the chapters |
| 40 | */ |
| 41 | public List<Chapter> getChapters() { |
| 42 | return chapters; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * The chapters of the story. |
| 47 | * |
| 48 | * @param chapters |
| 49 | * the chapters to set |
| 50 | */ |
| 51 | public void setChapters(List<Chapter> chapters) { |
| 52 | this.chapters = chapters; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get an iterator on the {@link Chapter}s. |
| 57 | */ |
| 58 | @Override |
| 59 | public Iterator<Chapter> iterator() { |
| 60 | return chapters == null ? empty.iterator() : chapters.iterator(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Display a DEBUG {@link String} representation of this object. |
| 65 | * <p> |
| 66 | * This is not efficient, nor intended to be. |
| 67 | */ |
| 68 | @Override |
| 69 | public String toString() { |
| 70 | String title = ""; |
| 71 | if (meta != null && meta.getTitle() != null) { |
| 72 | title = meta.getTitle(); |
| 73 | } |
| 74 | |
| 75 | String tags = ""; |
| 76 | if (meta != null && meta.getTags() != null) { |
| 77 | for (String tag : meta.getTags()) { |
| 78 | if (!tags.isEmpty()) { |
| 79 | tags += ", "; |
| 80 | } |
| 81 | tags += tag; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | String resume = ""; |
| 86 | if (meta != null && meta.getResume() != null) { |
| 87 | for (Paragraph para : meta.getResume()) { |
| 88 | resume += "\n\t"; |
| 89 | resume += para.toString().substring(0, |
| 90 | Math.min(para.toString().length(), 120)); |
| 91 | } |
| 92 | resume += "\n"; |
| 93 | } |
| 94 | |
| 95 | String cover = (meta == null || meta.getCover() == null) ? "none" |
| 96 | : meta.getCover().getWidth() + "x" |
| 97 | + meta.getCover().getHeight(); |
| 98 | return String.format( |
| 99 | "Title: [%s]\nAuthor: [%s]\nDate: [%s]\nTags: [%s]\n" |
| 100 | + "Resume: [%s]\nCover: [%s]", title, meta == null ? "" |
| 101 | : meta.getAuthor(), meta == null ? "" : meta.getDate(), |
| 102 | tags, resume, cover); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public Story clone() { |
| 107 | Story story = null; |
| 108 | try { |
| 109 | story = (Story) super.clone(); |
| 110 | } catch (CloneNotSupportedException e) { |
| 111 | // Did the clones rebel? |
| 112 | System.err.println(e); |
| 113 | } |
| 114 | |
| 115 | if (meta != null) { |
| 116 | story.meta = meta.clone(); |
| 117 | } |
| 118 | |
| 119 | if (chapters != null) { |
| 120 | story.chapters = new ArrayList<Chapter>(); |
| 121 | for (Chapter chap : chapters) { |
| 122 | story.chapters.add(chap.clone()); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return story; |
| 127 | } |
| 128 | } |