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 or later
5    * as published by the Free Software Foundation. This program is distributed
6    * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
7    * the 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   * © CrossWire Bible Society, 2005 - 2016
18   *
19   */
20  package org.crosswire.jsword.book.readings;
21  
22  import java.text.MessageFormat;
23  import java.util.Calendar;
24  import java.util.Date;
25  
26  import org.crosswire.common.icu.DateFormatter;
27  import org.crosswire.jsword.passage.DefaultLeafKeyList;
28  import org.crosswire.jsword.passage.Key;
29  
30  /**
31   * For a readings dictionary the keys are dates.
32   * 
33   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
34   * @author Joe Walker
35   * @author DM Smith
36   */
37  public class ReadingsKey extends DefaultLeafKeyList {
38      /**
39       * Simple Constructor.
40       * 
41       * @param text
42       *            The textual version of the date for these readings in the
43       *            format "d mmmm"
44       * @param osisName
45       *            The OSIS id of this Key
46       * @param parent
47       *            This Key's parent (or null of this Key has no parent)
48       */
49      protected ReadingsKey(String text, String osisName, Key parent) {
50          super(text, osisName, parent);
51  
52          DateFormatter formatter = DateFormatter.getDateInstance();
53          formatter.setLenient(true);
54          date = formatter.parse(text);
55      }
56  
57      /**
58       * Simple Constructor.
59       * 
60       * @param date
61       *            The date for this key
62       */
63      protected ReadingsKey(Date date) {
64          super(DateFormatter.getDateInstance().format(date), DateFormatter.getSimpleDateInstance("d.MMMM").format(date));
65          this.date = date;
66      }
67  
68      @Override
69      public boolean equals(Object obj) {
70          if (this == obj) {
71              return true;
72          }
73  
74          // Since this can not be null
75          if (obj == null) {
76              return false;
77          }
78  
79          // Check that that is the same as this
80          // Don't use instanceof since that breaks inheritance
81          if (!obj.getClass().equals(this.getClass())) {
82              return false;
83          }
84  
85          // The real bit ...
86          ReadingsKey that = (ReadingsKey) obj;
87  
88          return getName().equals(that.getName());
89      }
90  
91      @Override
92      public int hashCode() {
93          return date.hashCode();
94      }
95  
96      @Override
97      public int compareTo(Key obj) {
98          ReadingsKey that = (ReadingsKey) obj;
99          return this.date.compareTo(that.date);
100     }
101 
102     @Override
103     public ReadingsKey clone() {
104         return (ReadingsKey) super.clone();
105     }
106 
107     /**
108      * Convert the Gregorian Calendar to a string.
109      * 
110      * @param externalKey
111      * @return the internal representation of the key
112      */
113     public static String external2internal(Calendar externalKey) {
114         Object[] objs = {
115                 Integer.valueOf(1 + externalKey.get(Calendar.MONTH)), Integer.valueOf(externalKey.get(Calendar.DATE))
116         };
117         return KEY_FORMAT.format(objs);
118 
119     }
120 
121     /**
122      * The day of the year for the readings
123      */
124     private Date date;
125 
126     /**
127      * Date formatter
128      */
129     private static final MessageFormat KEY_FORMAT = new MessageFormat("{0,number,00}.{1,number,00}");
130 
131     /**
132      * Serialization ID
133      */
134     private static final long serialVersionUID = -5500401548068844993L;
135 }
136