| ReadingsKey.java |
1 /**
2 * Distribution License:
3 * JSword is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU Lesser General Public License, version 2.1 as published by
5 * the Free Software Foundation. This program is distributed in the hope
6 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 * See the GNU Lesser General Public License for more details.
9 *
10 * The License is available on the internet at:
11 * http://www.gnu.org/copyleft/lgpl.html
12 * or by writing to:
13 * Free Software Foundation, Inc.
14 * 59 Temple Place - Suite 330
15 * Boston, MA 02111-1307, USA
16 *
17 * Copyright: 2005
18 * The copyright to this program is held by it's authors.
19 *
20 * ID: $Id: ReadingsKey.java 2110 2011-03-08 13:55:32Z dmsmith $
21 */
22 package org.crosswire.jsword.book.readings;
23
24 import java.text.MessageFormat;
25 import java.text.ParseException;
26 import java.util.Calendar;
27 import java.util.Date;
28
29 import org.crosswire.common.icu.DateFormatter;
30 import org.crosswire.jsword.JSOtherMsg;
31 import org.crosswire.jsword.passage.DefaultLeafKeyList;
32 import org.crosswire.jsword.passage.Key;
33 import org.crosswire.jsword.passage.NoSuchKeyException;
34
35 /**
36 * For a readings dictionary the keys are dates.
37 *
38 * @see gnu.lgpl.License for license details.<br>
39 * The copyright to this program is held by it's authors.
40 * @author Joe Walker [joe at eireneh dot com]
41 * @author DM Smith [dmsmith555 at yahoo dot com]
42 */
43 public class ReadingsKey extends DefaultLeafKeyList {
44 /**
45 * Simple Constructor.
46 *
47 * @param text
48 * The textual version of the date for these readings in the
49 * format "d mmmm"
50 * @param osisName
51 * The OSIS id of this Key
52 * @param parent
53 * This Key's parent (or null of this Key has no parent)
54 */
55 protected ReadingsKey(String text, String osisName, Key parent) throws NoSuchKeyException {
56 super(text, osisName, parent);
57
58 try {
59 DateFormatter formatter = DateFormatter.getDateInstance();
60 formatter.setLenient(true);
61 date = formatter.parse(text);
62 } catch (ParseException ex) {
63 throw new NoSuchKeyException(JSOtherMsg.lookupText("Failed to parse {0}", text), ex);
64 }
65 }
66
67 /**
68 * Simple Constructor.
69 *
70 * @param date
71 * The date for this key
72 */
73 protected ReadingsKey(Date date) {
74 super(DateFormatter.getDateInstance().format(date), DateFormatter.getSimpleDateInstance("d.MMMM").format(date));
75 this.date = date;
76 }
77
78 @Override
79 public boolean equals(Object obj) {
80 if (this == obj) {
81 return true;
82 }
83
84 // Since this can not be null
85 if (obj == null) {
86 return false;
87 }
88
89 // Check that that is the same as this
90 // Don't use instanceof since that breaks inheritance
91 if (!obj.getClass().equals(this.getClass())) {
92 return false;
93 }
94
95 // The real bit ...
96 ReadingsKey that = (ReadingsKey) obj;
97
98 return getName().equals(that.getName());
99 }
100
101 @Override
102 public int hashCode() {
103 return date.hashCode();
104 }
105
106 @Override
107 public int compareTo(Key obj) {
108 ReadingsKey that = (ReadingsKey) obj;
109 return this.date.compareTo(that.date);
110 }
111
112 @Override
113 public ReadingsKey clone() {
114 return (ReadingsKey) super.clone();
115 }
116
117 /**
118 * Convert the Gregorian Calendar to a string.
119 *
120 * @param externalKey
121 * @return the internal representation of the key
122 */
123 public static String external2internal(Calendar externalKey) {
124 Object[] objs = {
125 Integer.valueOf(1 + externalKey.get(Calendar.MONTH)), Integer.valueOf(externalKey.get(Calendar.DATE))
126 };
127 return KEY_FORMAT.format(objs);
128
129 }
130
131 /**
132 * The day of the year for the readings
133 */
134 private Date date;
135
136 /**
137 * Serialization ID
138 */
139 private static final long serialVersionUID = -5500401548068844993L;
140
141 /**
142 * Date formatter
143 */
144 private static final MessageFormat KEY_FORMAT = new MessageFormat("{0,number,00}.{1,number,00}");
145 }
146