2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.model
.common
;
12 import java
.util
.Calendar
;
14 import javax
.persistence
.Embeddable
;
15 import javax
.persistence
.Temporal
;
16 import javax
.persistence
.TemporalType
;
17 import javax
.persistence
.Transient
;
18 import javax
.xml
.bind
.annotation
.XmlAccessType
;
19 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
20 import javax
.xml
.bind
.annotation
.XmlElement
;
21 import javax
.xml
.bind
.annotation
.XmlRootElement
;
22 import javax
.xml
.bind
.annotation
.XmlType
;
24 import org
.apache
.log4j
.Logger
;
25 import org
.hibernate
.annotations
.Type
;
26 import org
.joda
.time
.DateTime
;
27 import org
.joda
.time
.DateTimeFieldType
;
28 import org
.joda
.time
.LocalDate
;
29 import org
.joda
.time
.Months
;
30 import org
.joda
.time
.Partial
;
31 import org
.joda
.time
.ReadableInstant
;
36 * @created 08-Nov-2007 13:07:00
38 @XmlAccessorType(XmlAccessType
.FIELD
)
39 @XmlType(name
= "TimePeriod", propOrder
= {
43 @XmlRootElement(name
= "TimePeriod")
45 public class TimePeriod
implements Cloneable
{
47 private static final Logger logger
= Logger
.getLogger(TimePeriod
.class);
49 @XmlElement(name
= "Start")
50 private Partial start
;
52 @XmlElement(name
= "End")
60 public static TimePeriod
NewInstance(){
61 return new TimePeriod();
69 public static TimePeriod
NewInstance(Partial startDate
){
70 return new TimePeriod(startDate
);
78 public static TimePeriod
NewInstance(Partial startDate
, Partial endDate
){
79 return new TimePeriod(startDate
, endDate
);
87 public static TimePeriod
NewInstance(Integer year
){
88 Integer endYear
= null;
89 return NewInstance(year
, endYear
);
96 public static TimePeriod
NewInstance(Integer startYear
, Integer endYear
){
97 Partial startDate
= null;
98 Partial endDate
= null;
99 if (startYear
!= null){
100 startDate
= new Partial().with(DateTimeFieldType
.year(), startYear
);
102 if (endYear
!= null){
103 endDate
= new Partial().with(DateTimeFieldType
.year(), endYear
);
105 return new TimePeriod(startDate
, endDate
);
111 * Factory method to create a TimePeriod from a <code>Calendar</code>. The Calendar is stored as the starting instant.
114 public static TimePeriod
NewInstance(Calendar startCalendar
){
115 return NewInstance(startCalendar
, null);
119 * Factory method to create a TimePeriod from a <code>ReadableInstant</code>(e.g. <code>DateTime</code>).
120 * The <code>ReadableInstant</code> is stored as the starting instant.
123 public static TimePeriod
NewInstance(ReadableInstant readableInstant
){
124 return NewInstance(readableInstant
, null);
128 * Factory method to create a TimePeriod from a starting and an ending <code>Calendar</code>
131 public static TimePeriod
NewInstance(Calendar startCalendar
, Calendar endCalendar
){
132 Partial startDate
= null;
133 Partial endDate
= null;
134 if (startCalendar
!= null){
135 startDate
= calendarToPartial(startCalendar
);
137 if (endCalendar
!= null){
138 endDate
= calendarToPartial(endCalendar
);
140 return new TimePeriod(startDate
, endDate
);
145 * Factory method to create a TimePeriod from a starting and an ending <code>ReadableInstant</code>(e.g. <code>DateTime</code>)
148 public static TimePeriod
NewInstance(ReadableInstant startInstant
, ReadableInstant endInstant
){
149 Partial startDate
= null;
150 Partial endDate
= null;
151 if (startInstant
!= null){
152 startDate
= readableInstantToPartial(startInstant
);
154 if (endInstant
!= null){
155 endDate
= readableInstantToPartial(endInstant
);
157 return new TimePeriod(startDate
, endDate
);
162 * Transforms a <code>Calendar</code> into a <code>Partial</code>
166 public static Partial
calendarToPartial(Calendar calendar
){
167 LocalDate ld
= new LocalDate(calendar
);
168 Partial partial
= new Partial(ld
);
173 * Transforms a <code>Calendar</code> into a <code>Partial</code>
177 public static Partial
readableInstantToPartial(ReadableInstant readableInstant
){
178 DateTime dt
= readableInstant
.toInstant().toDateTime();
179 LocalDate ld
= dt
.toLocalDate();
180 Partial partial
= new Partial(ld
);
187 protected TimePeriod() {
190 public TimePeriod(Partial startDate
) {
193 public TimePeriod(Partial startDate
, Partial endDate
) {
198 //@Temporal(TemporalType.TIMESTAMP)
199 @Type(type
="partialUserType")
200 public Partial
getStart() {
203 public void setStart(Partial start
) {
207 //@Temporal(TemporalType.TIMESTAMP)
208 @Type(type
="partialUserType")
209 public Partial
getEnd() {
212 public void setEnd(Partial end
) {
217 public String
getYear(){
219 if (getStartYear() != null){
220 result
+= String
.valueOf(getStartYear());
221 if (getEndYear() != null){
222 result
+= "-" + String
.valueOf(getEndYear());
225 if (getEndYear() != null){
226 result
+= String
.valueOf(getEndYear());
233 public Integer
getStartYear(){
234 return getPartialValue(start
, DateTimeFieldType
.year());
238 public Integer
getStartMonth(){
239 return getPartialValue(start
, DateTimeFieldType
.monthOfYear());
243 public Integer
getStartDay(){
244 return getPartialValue(start
, DateTimeFieldType
.dayOfMonth());
248 public Integer
getEndYear(){
249 return getPartialValue(end
, DateTimeFieldType
.year());
253 public Integer
getEndMonth(){
254 return getPartialValue(end
, DateTimeFieldType
.monthOfYear());
258 public Integer
getEndDay(){
259 return getPartialValue(end
, DateTimeFieldType
.dayOfMonth());
264 private Integer
getPartialValue(Partial partial
, DateTimeFieldType type
){
265 if (partial
== null || ! partial
.isSupported(type
)){
268 return partial
.get(type
);
273 public TimePeriod
setStartYear(Integer year
){
274 return setStartField(year
, DateTimeFieldType
.year());
277 public TimePeriod
setStartMonth(Integer month
) throws IndexOutOfBoundsException
{
278 return setStartField(month
, DateTimeFieldType
.monthOfYear());
281 public TimePeriod
setStartDay(Integer day
) throws IndexOutOfBoundsException
{
282 return setStartField(day
, DateTimeFieldType
.dayOfMonth());
285 public TimePeriod
setEndYear(Integer year
){
286 return setEndField(year
, DateTimeFieldType
.year());
289 public TimePeriod
setEndMonth(Integer month
) throws IndexOutOfBoundsException
{
290 return setEndField(month
, DateTimeFieldType
.monthOfYear());
293 public TimePeriod
setEndDay(Integer day
) throws IndexOutOfBoundsException
{
294 return setEndField(day
, DateTimeFieldType
.dayOfMonth());
297 private TimePeriod
setStartField(Integer value
, DateTimeFieldType type
) throws IndexOutOfBoundsException
{
300 start
= start
.without(type
);
303 if (type
.equals(DateTimeFieldType
.monthOfYear())){
306 if (type
.equals(DateTimeFieldType
.dayOfMonth())){
309 if (value
< 1 || value
> max
){
310 throw new IndexOutOfBoundsException("Value must be between 1 and " + max
);
312 start
= this.start
.with(type
, value
);
316 private TimePeriod
setEndField(Integer value
, DateTimeFieldType type
){
319 end
= end
.without(type
);
322 if (type
.equals(DateTimeFieldType
.monthOfYear())){
325 if (type
.equals(DateTimeFieldType
.dayOfMonth())){
328 if (value
< 1 || value
> max
){
329 throw new IndexOutOfBoundsException("Value must be between 1 and " + max
);
331 end
= this.end
.with(type
, value
);
335 private void initStart(){
337 start
= new Partial();
341 private void initEnd(){
349 //*********** CLONE **********************************/
352 * @see java.lang.Object#clone()
355 public Object
clone() {
357 TimePeriod result
= (TimePeriod
)super.clone();
358 result
.setStart(this.start
); //DateTime is immutable
359 result
.setEnd(this.end
);
361 } catch (CloneNotSupportedException e
) {
362 logger
.warn("Clone not supported exception. Should never occurr !!");