View Javadoc

1   /* Copyright (c) 2008 Sascha Kohlmann
2    *
3    * This program is free software: you can redistribute it and/or modify
4    * it under the terms of the GNU Affero General Public License as published by
5    * the Free Software Foundation, either version 3 of the License, or
6    * (at your option) any later version.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Affero General Public License for more details.
12   *
13   * You should have received a copy of the GNU Affero General Public License
14   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15   */
16  package net.sf.eos.document;
17  
18  import net.sf.eos.Metadata;
19  import net.sf.eos.util.EqualsAndHashUtil;
20  
21  import java.io.IOException;
22  import java.io.StringWriter;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  /**
28   * Represents a simple &#949;&#959;s document. An instance of this document
29   * never contains any linefeed (ASCII <tt>0xa</tt>) or carriage return
30   * (ASCII <tt>0xd</tt>) characters.
31   * @author Sascha Kohlmann
32   */
33  public final class EosDocument implements Metadata {
34  
35      /** A metadata key for the year in a metadata. */
36      @SuppressWarnings("nls")
37      public final static String YEAR_META_KEY = "EosDocument/date";
38  
39      /** A metadata key for the creator in a metadata. */
40      @SuppressWarnings("nls")
41      public final static String CREATOR_META_KEY = "EosDocument/creator";
42  
43      /** A metadata key for the IDs in a metadata. */
44      @SuppressWarnings("nls")
45      public final static String ID_META_KEY = "EosDocument/id";
46  
47      private CharSequence text;
48      private CharSequence title;
49      private Map<String, List<String>> meta =
50          new HashMap<String, List<String>>();
51  
52      /** Returns the text of the document.
53       * @return the text*/
54      public CharSequence getText() {
55          return this.text;
56      }
57      /** Returns the title of the document.
58       * @return the title of the document */
59      public CharSequence getTitle() {
60          return this.title;
61      }
62      /** Returns the metadata of the document.
63       * @return the metadata of the document */
64      public Map<String, List<String>> getMeta() {
65          return this.meta;
66      }
67      /** Sets the text of the document.
68       * @param text the text of the document */
69      public void setText(@SuppressWarnings("hiding") final CharSequence text) {
70          this.text = text;
71      }
72      /** Sets the title of the document.
73       * @param title the title of the document */
74      public void setTitle(@SuppressWarnings("hiding") final CharSequence title) {
75          this.title = title;
76      }
77      /** Sets the metadata of the document.
78       * @param meta the metadata of the document */
79      public void setMeta(
80              @SuppressWarnings("hiding") final Map<String, List<String>> meta) {
81          this.meta = meta;
82      }
83  
84      /**
85       * The value of the return string may change in future implementations.
86       * Don't use for information extraction.
87       * @return a {@code String} representation of the document
88       * @see Serializer for serialization
89       */
90      @Override
91      @SuppressWarnings("nls")
92      public String toString() {
93          final StringWriter writer = new StringWriter();
94          final XmlSerializer serializer = new XmlSerializer();
95          final StringBuffer sb = new StringBuffer("EosDocument[");
96          try {
97              serializer.serialize(this, writer);
98              sb.append(writer.toString());
99              sb.append("]");
100         } catch (final IOException e) {
101             sb.append("throws IOException:");
102             sb.append(e.getMessage());
103             sb.append("]");
104         }
105 
106         return sb.toString();
107     }
108 
109     /**
110      * @see Object#equals(Object)
111      */
112     @Override
113     public boolean equals(final Object other) {
114         if (other == null) {
115             return false;
116         }
117         if (! (other instanceof EosDocument)) {
118             return false;
119         }
120         final EosDocument doc = (EosDocument) other;
121         return EqualsAndHashUtil.isEqual(this.text, doc.text)
122                 && EqualsAndHashUtil.isEqual(this.title, doc.title)
123                 && EqualsAndHashUtil.isEqual(this.meta, doc.meta);
124     }
125 
126     /**
127      * Implemented for {@linkplain Object#hashCode() hash contract}. Don't use
128      * a {@code EosDocument} instance in a hash oriented dictionary.
129      */
130     @Override
131     public int hashCode() {
132         int hash = EqualsAndHashUtil.hash(this.text);
133         hash *= EqualsAndHashUtil.hash(this.title);
134         hash *= EqualsAndHashUtil.hash(this.meta);
135         return hash;
136     }
137 }