use more template, use replace input stream
[nikiroo-utils.git] / src / be / nikiroo / fanfix / data / MetaData.java
index 189277d9480fb8ef4abf3f5b6b8646a5d74d303e..2e34ef98ad27bda10ff34a009fa3d3d314aaf6b2 100644 (file)
@@ -1,21 +1,32 @@
 package be.nikiroo.fanfix.data;
 
-import java.awt.image.BufferedImage;
+import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.List;
 
+import be.nikiroo.utils.Image;
+import be.nikiroo.utils.StringUtils;
+
 /**
  * The meta data associated to a {@link Story} object.
+ * <p>
+ * Note that some earlier version of the program did not save the resume as an
+ * external file; for those stories, the resume is not fetched until the story
+ * is.
+ * <p>
+ * The cover is never fetched until the story is.
  * 
  * @author niki
  */
-public class MetaData implements Cloneable, Comparable<MetaData> {
+public class MetaData implements Cloneable, Comparable<MetaData>, Serializable {
+       private static final long serialVersionUID = 1L;
+
        private String title;
        private String author;
        private String date;
        private Chapter resume;
        private List<String> tags;
-       private BufferedImage cover;
+       private Image cover;
        private String subject;
        private String source;
        private String url;
@@ -29,6 +40,12 @@ public class MetaData implements Cloneable, Comparable<MetaData> {
        private String creationDate;
        private boolean fakeCover;
 
+       /**
+        * Create an empty {@link MetaData}.
+        */
+       public MetaData() {
+       }
+       
        /**
         * The title of the story.
         * 
@@ -107,6 +124,12 @@ public class MetaData implements Cloneable, Comparable<MetaData> {
 
        /**
         * The story resume (a.k.a. description).
+        * <p>
+        * This can be NULL if we don't have a resume for this {@link Story}.
+        * <p>
+        * Note that some earlier version of the program did not save the resume as
+        * an external file; for those stories, the resume is not fetched until the
+        * story is.
         * 
         * @return the resume
         */
@@ -116,6 +139,10 @@ public class MetaData implements Cloneable, Comparable<MetaData> {
 
        /**
         * The story resume (a.k.a. description).
+        * <p>
+        * Note that some earlier version of the program did not save the resume as
+        * an external file; for those stories, the resume is not fetched until the
+        * story is.
         * 
         * @param resume
         *            the resume to set
@@ -126,20 +153,24 @@ public class MetaData implements Cloneable, Comparable<MetaData> {
 
        /**
         * The cover image of the story if any (can be NULL).
+        * <p>
+        * The cover is not fetched until the story is.
         * 
         * @return the cover
         */
-       public BufferedImage getCover() {
+       public Image getCover() {
                return cover;
        }
 
        /**
         * The cover image of the story if any (can be NULL).
+        * <p>
+        * The cover is not fetched until the story is.
         * 
         * @param cover
         *            the cover to set
         */
-       public void setCover(BufferedImage cover) {
+       public void setCover(Image cover) {
                this.cover = cover;
        }
 
@@ -235,7 +266,7 @@ public class MetaData implements Cloneable, Comparable<MetaData> {
        /**
         * A unique value representing the story in the local library.
         * 
-        * @param uuid
+        * @param luid
         *            the luid to set
         */
        public void setLuid(String luid) {
@@ -377,9 +408,39 @@ public class MetaData implements Cloneable, Comparable<MetaData> {
                this.fakeCover = fakeCover;
        }
 
+       @Override
        public int compareTo(MetaData o) {
-               String oUuid = o == null ? null : o.getUuid();
-               return getUuid().compareTo(oUuid);
+               if (o == null) {
+                       return 1;
+               }
+
+               String id = (getTitle() == null ? "" : getTitle())
+                               + (getUuid() == null ? "" : getUuid())
+                               + (getLuid() == null ? "" : getLuid());
+               String oId = (getTitle() == null ? "" : o.getTitle())
+                               + (getUuid() == null ? "" : o.getUuid())
+                               + (o.getLuid() == null ? "" : o.getLuid());
+
+               return id.compareToIgnoreCase(oId);
+       }
+
+       @Override
+       public boolean equals(Object obj) {
+               if (!(obj instanceof MetaData)) {
+                       return false;
+               }
+
+               return compareTo((MetaData) obj) == 0;
+       }
+
+       @Override
+       public int hashCode() {
+               String uuid = getUuid();
+               if (uuid == null) {
+                       uuid = "" + title + author + source;
+               }
+
+               return uuid.hashCode();
        }
 
        @Override
@@ -393,16 +454,58 @@ public class MetaData implements Cloneable, Comparable<MetaData> {
                }
 
                if (tags != null) {
-                       meta.tags = new ArrayList<String>();
-                       meta.tags.addAll(tags);
+                       meta.tags = new ArrayList<String>(tags);
                }
+
                if (resume != null) {
-                       meta.resume = new Chapter(resume.getNumber(), resume.getName());
-                       for (Paragraph para : resume) {
-                               meta.resume.getParagraphs().add(para);
-                       }
+                       meta.resume = resume.clone();
                }
 
                return meta;
        }
+
+       /**
+        * Display a DEBUG {@link String} representation of this object.
+        * <p>
+        * This is not efficient, nor intended to be.
+        */
+       @Override
+       public String toString() {
+               String title = "";
+               if (getTitle() != null) {
+                       title = getTitle();
+               }
+
+               StringBuilder tags = new StringBuilder();
+               if (getTags() != null) {
+                       for (String tag : getTags()) {
+                               if (tags.length() > 0) {
+                                       tags.append(", ");
+                               }
+                               tags.append(tag);
+                       }
+               }
+
+               String resume = "";
+               if (getResume() != null) {
+                       for (Paragraph para : getResume()) {
+                               resume += "\n\t";
+                               resume += para.toString().substring(0,
+                                               Math.min(para.toString().length(), 120));
+                       }
+                       resume += "\n";
+               }
+
+               String cover = "none";
+               if (getCover() != null) {
+                       cover = StringUtils.formatNumber(getCover().getSize())
+                                       + "bytes";
+               }
+
+               return String.format(
+                               "Meta %s:\n\tTitle: [%s]\n\tAuthor: [%s]\n\tDate: [%s]\n\tTags: [%s]\n\tWord count: [%s]"
+                                               + "\n\tResume: [%s]\n\tCover: [%s]",
+                               luid, title, getAuthor(), getDate(), tags.toString(),
+                               "" + words, resume, cover);
+       }
 }