1 /*
2 * Copyright 2003 - 2013 The eFaps Team
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Revision: $Rev$
17 * Last Changed: $Date$
18 * Last Changed By: $Author$
19 */
20
21 package org.efaps.util;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.efaps.admin.dbproperty.DBProperties;
27 import org.joda.time.Chronology;
28 import org.joda.time.DateTimeZone;
29 import org.joda.time.chrono.BuddhistChronology;
30 import org.joda.time.chrono.CopticChronology;
31 import org.joda.time.chrono.EthiopicChronology;
32 import org.joda.time.chrono.GJChronology;
33 import org.joda.time.chrono.GregorianChronology;
34 import org.joda.time.chrono.ISOChronology;
35 import org.joda.time.chrono.IslamicChronology;
36 import org.joda.time.chrono.JulianChronology;
37
38 /**
39 * Enumeration to define different possible calendars and to handle the
40 * conversions between than.
41 *
42 * @author The eFaps Team
43 * @version $Id$
44 */
45 public enum ChronologyType
46 {
47 /**
48 * Instance for the Buddhist calendar.
49 */
50 BUDDHIST("Buddhist") {
51 /**
52 * Method to get an instance of BuddhistChronology with the given time
53 * zone.
54 *
55 * @param _timeZone time zone the chronology must use
56 * @return an instance of BuddhistChronology
57 */
58 @Override
59 public Chronology getInstance(final DateTimeZone _timeZone)
60 {
61 return BuddhistChronology.getInstance(_timeZone);
62 }
63 },
64
65 /**
66 * Instance for the Coptic calendar.
67 */
68 COPTIC("Coptic") {
69 /**
70 * Method to get an Instance of CopticChronology with the given time
71 * zone.
72 *
73 * @param _timeZone time zone the chronology must use
74 * @return an instance of CopticChronology
75 */
76 @Override
77 public Chronology getInstance(final DateTimeZone _timeZone)
78 {
79 return CopticChronology.getInstance(_timeZone);
80 }
81 },
82
83 /**
84 * Instance for the Ethiopic Calendar.
85 */
86 ETHIOPIC("Ethiopic") {
87 /**
88 * Method to get an Instance of EthiopicChronology with the given time
89 * zone.
90 *
91 * @param _timeZone time zone the chronology must use
92 * @return an instance of EthiopicChronology
93 */
94 @Override
95 public Chronology getInstance(final DateTimeZone _timeZone)
96 {
97 return EthiopicChronology.getInstance(_timeZone);
98 }
99 },
100
101 /**
102 * Instance for the Gregorian Calendar.
103 */
104 GREGORIAN("Gregorian") {
105 /**
106 * Method to get an Instance of GregorianChronology with the given time
107 * zone.
108 *
109 * @param _timeZone time zone the chronology must use
110 * @return an instance of GregorianChronology
111 */
112 @Override
113 public Chronology getInstance(final DateTimeZone _timeZone)
114 {
115 return GregorianChronology.getInstance(_timeZone);
116 }
117 },
118
119 /**
120 * Instance for the GregorianJulian Calendar.
121 */
122 GREGORIAN_JULIAN("GregorianJulian") {
123 /**
124 * Method to get an Instance of GJChronology with the given time zone.
125 *
126 * @param _timeZone time zone the chronology must use
127 * @return an instance of GJChronology
128 */
129 @Override
130 public Chronology getInstance(final DateTimeZone _timeZone)
131 {
132 return GJChronology.getInstance(_timeZone);
133 }
134 },
135
136 /**
137 * Instance for the Islamic Calendar.
138 */
139 ISLAMIC("Islamic") {
140 /**
141 * Method to get an Instance of IslamicChronology with the given time
142 * zone.
143 *
144 * @param _timeZone time zone the chronology must use
145 * @return an instance of IslamicChronology
146 */
147 @Override
148 public Chronology getInstance(final DateTimeZone _timeZone)
149 {
150 return IslamicChronology.getInstance(_timeZone);
151 }
152 },
153
154 /**
155 * Instance for the ISO8601 Calendar.
156 */
157 ISO8601("ISO8601") {
158 /**
159 * Method to get an Instance of ISOChronology with the given time zone.
160 *
161 * @param _timeZone time zone the chronology must use
162 * @return an instance of ISOChronology
163 */
164 @Override
165 public Chronology getInstance(final DateTimeZone _timeZone)
166 {
167 return ISOChronology.getInstance(_timeZone);
168 }
169 },
170
171 /**
172 * Instance for the Julian Calendar.
173 */
174 JULIAN("Julian") {
175 /**
176 * Method to get an Instance of JulianChronology with the given time
177 * zone.
178 *
179 * @param _timeZone time zone the chronology must use
180 * @return an instance of JulianChronology
181 */
182 @Override
183 public Chronology getInstance(final DateTimeZone _timeZone)
184 {
185 return JulianChronology.getInstance(_timeZone);
186 }
187 };
188
189 /**
190 * The class is required because an enum definition does not allow to own
191 * and access directly static variables.
192 */
193 private static final class Mapper
194 {
195 /**
196 * Mapping between the key of a chronology type and the related
197 * enumeration instance.
198 */
199 private static final Map<String, ChronologyType> KEY2ENUM = new HashMap<String, ChronologyType>();
200
201 /**
202 * Private constructor so that no instance could be created.
203 */
204 private Mapper()
205 {
206 }
207 }
208
209 /**
210 * Stores the key for the type.
211 */
212 private final String key;
213
214 /**
215 * Constructor setting the instance variables.
216 *
217 * @param _key Key of the ChronologyType
218 */
219 private ChronologyType(final String _key)
220 {
221 this.key = _key;
222 ChronologyType.Mapper.KEY2ENUM.put(this.key, this);
223 }
224
225 /**
226 * Method to get the Label for the {@link ChronologyType}.
227 *
228 * @return string containing the label.
229 */
230 public String getLabel()
231 {
232 return DBProperties.getProperty("org.efaps.util.ChronologyType." + this.key);
233 }
234
235 /**
236 * Method to get the instance of a ChronologyType by the Key.
237 *
238 * @param _key Key for the ChronologyType
239 * @return instance of a ChronologyType
240 */
241 public static ChronologyType getByKey(final String _key)
242 {
243 final ChronologyType ret = ChronologyType.Mapper.KEY2ENUM.get(_key);
244 if (ret == null) {
245 throw new IllegalArgumentException("Unknown Key '" + _key + "' for enum ChronologyType.");
246 }
247 return ret;
248 }
249
250 /**
251 * Getter method for instance variable {@link #key}.
252 *
253 * @return value of instance variable {@link #key}
254 */
255 public String getKey()
256 {
257 return this.key;
258 }
259
260 /**
261 * Method is overwritten by all instances.
262 *
263 * @param _timeZone time zone the chronology must use
264 * @return Chronology
265 */
266 public abstract Chronology getInstance(final DateTimeZone _timeZone);
267 }