001: /*
002: * Copyright 2004 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: Date.java,v 1.6 2004/01/18 03:01:06 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.sco;
012:
013: import com.triactive.jdo.SCO;
014: import java.io.ObjectStreamException;
015: import javax.jdo.JDOHelper;
016:
017: /**
018: * A mutable second-class date object.
019: *
020: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
021: * @version $Revision: 1.6 $
022: */
023:
024: public class Date extends java.util.Date implements SCO {
025: private transient Object owner;
026: private transient String fieldName;
027:
028: /**
029: * Creates a <tt>Date</tt> object that represents the same time as the
030: * given <tt>java.util.Date</tt>. Assigns owning object and field name.
031: *
032: * @param owner the owning object
033: * @param fieldName the owning field name
034: * @param date the initial date value
035: */
036:
037: public Date(Object owner, String fieldName, java.util.Date date) {
038: super (date.getTime());
039:
040: this .owner = owner;
041: this .fieldName = fieldName;
042: }
043:
044: public Object getOwner() {
045: return owner;
046: }
047:
048: public String getFieldName() {
049: return fieldName;
050: }
051:
052: public void makeDirty() {
053: if (owner != null)
054: JDOHelper.makeDirty(owner, fieldName);
055: }
056:
057: public void applyUpdates() {
058: }
059:
060: public void unsetOwner() {
061: owner = null;
062: fieldName = null;
063: }
064:
065: /**
066: * Creates and returns a copy of this object.
067: *
068: * <p>Mutable second-class Objects are required to provide a public
069: * clone method in order to allow for copying PersistenceCapable
070: * objects. In contrast to Object.clone(), this method must not throw a
071: * CloneNotSupportedException.
072: */
073:
074: public Object clone() {
075: Object obj = super .clone();
076:
077: ((Date) obj).unsetOwner();
078:
079: return obj;
080: }
081:
082: public void setTime(long time) {
083: super .setTime(time);
084: makeDirty();
085: }
086:
087: /**
088: * Sets the year of this <tt>Date</tt> object to be the specified
089: * value plus 1900. This <code>Date</code> object is modified so
090: * that it represents a point in time within the specified year,
091: * with the month, date, hour, minute, and second the same as
092: * before, as interpreted in the local time zone. (Of course, if
093: * the date was February 29, for example, and the year is set to a
094: * non-leap year, then the new date will be treated as if it were
095: * on March 1.)
096: *
097: * @param year the year value.
098: * @see java.util.Calendar
099: * @deprecated As of JDK version 1.1,
100: * replaced by <code>Calendar.set(Calendar.YEAR, year + 1900)</code>.
101: */
102:
103: public void setYear(int year) {
104: super .setYear(year);
105: makeDirty();
106: }
107:
108: /**
109: * Sets the month of this date to the specified value. This
110: * <tt>Date</tt> object is modified so that it represents a point
111: * in time within the specified month, with the year, date, hour,
112: * minute, and second the same as before, as interpreted in the
113: * local time zone. If the date was October 31, for example, and
114: * the month is set to June, then the new date will be treated as
115: * if it were on July 1, because June has only 30 days.
116: *
117: * @param month the month value between 0-11.
118: * @see java.util.Calendar
119: * @deprecated As of JDK version 1.1,
120: * replaced by <code>Calendar.set(Calendar.MONTH, int month)</code>.
121: */
122:
123: public void setMonth(int month) {
124: super .setMonth(month);
125: makeDirty();
126: }
127:
128: /**
129: * Sets the day of the month of this <tt>Date</tt> object to the
130: * specified value. This <tt>Date</tt> object is modified so that
131: * it represents a point in time within the specified day of the
132: * month, with the year, month, hour, minute, and second the same
133: * as before, as interpreted in the local time zone. If the date
134: * was April 30, for example, and the date is set to 31, then it
135: * will be treated as if it were on May 1, because April has only
136: * 30 days.
137: *
138: * @param date the day of the month value between 1-31.
139: * @see java.util.Calendar
140: * @deprecated As of JDK version 1.1,
141: * replaced by <code>Calendar.set(Calendar.DAY_OF_MONTH, int date)</code>.
142: */
143:
144: public void setDate(int date) {
145: super .setDate(date);
146: makeDirty();
147: }
148:
149: /**
150: * Sets the hour of this <tt>Date</tt> object to the specified value.
151: * This <tt>Date</tt> object is modified so that it represents a point
152: * in time within the specified hour of the day, with the year, month,
153: * date, minute, and second the same as before, as interpreted in the
154: * local time zone.
155: *
156: * @param hours the hour value.
157: * @see java.util.Calendar
158: * @deprecated As of JDK version 1.1,
159: * replaced by <code>Calendar.set(Calendar.HOUR_OF_DAY, int hours)</code>.
160: */
161:
162: public void setHours(int hours) {
163: super .setHours(hours);
164: makeDirty();
165: }
166:
167: /**
168: * Sets the minutes of this <tt>Date</tt> object to the specified value.
169: * This <tt>Date</tt> object is modified so that it represents a point
170: * in time within the specified minute of the hour, with the year, month,
171: * date, hour, and second the same as before, as interpreted in the
172: * local time zone.
173: *
174: * @param minutes the value of the minutes.
175: * @see java.util.Calendar
176: * @deprecated As of JDK version 1.1,
177: * replaced by <code>Calendar.set(Calendar.MINUTE, int minutes)</code>.
178: */
179:
180: public void setMinutes(int minutes) {
181: super .setMinutes(minutes);
182: makeDirty();
183: }
184:
185: /**
186: * Sets the seconds of this <tt>Date</tt> to the specified value.
187: * This <tt>Date</tt> object is modified so that it represents a
188: * point in time within the specified second of the minute, with
189: * the year, month, date, hour, and minute the same as before, as
190: * interpreted in the local time zone.
191: *
192: * @param seconds the seconds value.
193: * @see java.util.Calendar
194: * @deprecated As of JDK version 1.1,
195: * replaced by <code>Calendar.set(Calendar.SECOND, int seconds)</code>.
196: */
197:
198: public void setSeconds(int seconds) {
199: super .setSeconds(seconds);
200: makeDirty();
201: }
202:
203: /**
204: * Replaces the object to be serialized with a java.util.Date object.
205: * Invoked by the serialization mechanism to obtain an alternative object
206: * to be used when writing an object to the stream.
207: *
208: * @return
209: * The <code>Date</code> to be serialized instead of this object.
210: */
211:
212: protected Object writeReplace() throws ObjectStreamException {
213: return new java.util.Date(getTime());
214: }
215: }
|