001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla.entities.domain.internal;
014:
015: import java.util.Calendar;
016: import java.util.Date;
017: import java.util.Locale;
018: import java.util.NoSuchElementException;
019:
020: import org.rapla.components.util.DateTools;
021: import org.rapla.entities.RaplaType;
022: import org.rapla.entities.domain.Period;
023: import org.rapla.entities.storage.Mementable;
024: import org.rapla.entities.storage.internal.SimpleEntity;
025:
026: public class PeriodImpl extends SimpleEntity implements Period,
027: java.io.Serializable, Mementable {
028: // Don't forget to increase the serialVersionUID when you change the fields
029: private static final long serialVersionUID = 1;
030:
031: private final static long WEEK_MILLIS = DateTools.MILLISECONDS_PER_WEEK;
032:
033: String name;
034: Date start;
035: Date end;
036: transient Calendar cal;
037:
038: public PeriodImpl() {
039:
040: }
041:
042: public PeriodImpl(Date start, Date end) {
043: this .start = start;
044: this .end = end;
045: }
046:
047: final public RaplaType getRaplaType() {
048: return TYPE;
049: }
050:
051: public Date getStart() {
052: return start;
053: }
054:
055: public void setStart(Date start) {
056: checkWritable();
057: this .start = start;
058: }
059:
060: public Date getEnd() {
061: return end;
062: }
063:
064: public void setEnd(Date end) {
065: checkWritable();
066: this .end = end;
067: }
068:
069: public int getWeeks() {
070: long diff = end.getTime() - start.getTime();
071: return (int) (((diff - 1) / WEEK_MILLIS) + 1);
072: }
073:
074: public String getName(Locale locale) {
075: return name;
076: }
077:
078: public String getName() {
079: return name;
080: }
081:
082: public void setName(String name) {
083: checkWritable();
084: this .name = name;
085: }
086:
087: public boolean contains(Date date) {
088: return (date.before(end) && !date.before(start));
089: }
090:
091: public int weekOf(Date date) {
092: Calendar cal = Calendar.getInstance(DateTools.getTimeZone());
093: if (!contains(date))
094: throw new NoSuchElementException(
095: "Period doesn't contain date " + date);
096: long duration = date.getTime() - start.getTime();
097: long weeks = duration / (DateTools.MILLISECONDS_PER_WEEK);
098: // setTimeInMillis has protected access in JDK 1.3.1
099: cal.setTime(new Date(date.getTime() - weeks
100: * DateTools.MILLISECONDS_PER_WEEK));
101: int week_of_year = cal.get(Calendar.WEEK_OF_YEAR);
102: cal.setTime(getStart());
103: return ((int) weeks)
104: + 1
105: + (((week_of_year) != cal.get(Calendar.WEEK_OF_YEAR)) ? 1
106: : 0);
107: }
108:
109: public String toString() {
110: return getName();
111: }
112:
113: public int compareTo(Date date) {
114: int result = getEnd().compareTo(date);
115: if (result == 0)
116: return 1;
117: else
118: return result;
119: }
120:
121: public int compareTo(Period period) {
122: int result = getStart().compareTo(period.getStart());
123: if (result != 0)
124: return result;
125:
126: if (equals(period))
127: return 0;
128:
129: return (hashCode() < period.hashCode()) ? -1 : 1;
130: }
131:
132: public int compareTo(Object o2) {
133: if (o2 instanceof Date) {
134: return compareTo((Date) o2);
135: }
136: if (o2 instanceof Period) {
137: return compareTo((Period) o2);
138: }
139: throw new ClassCastException(
140: "Object needs to be an instance of Date or Period");
141: }
142:
143: static private void copy(PeriodImpl source, PeriodImpl dest) {
144: dest.start = source.start;
145: dest.end = source.end;
146: dest.name = source.name;
147: }
148:
149: public void copy(Object obj) {
150: super .copy((PeriodImpl) obj);
151: copy((PeriodImpl) obj, this );
152: }
153:
154: public Object deepClone() {
155: PeriodImpl clone = new PeriodImpl();
156: super .deepClone(clone);
157: copy(this , clone);
158: return clone;
159: }
160:
161: public Object clone() {
162: PeriodImpl clone = new PeriodImpl();
163: super.clone(clone);
164: copy(this, clone);
165: return clone;
166: }
167: }
|