KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > sco > Date


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: Date.java,v 1.6 2004/01/18 03:01:06 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.sco;
12
13 import com.triactive.jdo.SCO;
14 import java.io.ObjectStreamException JavaDoc;
15 import javax.jdo.JDOHelper;
16
17
18 /**
19  * A mutable second-class date object.
20  *
21  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
22  * @version $Revision: 1.6 $
23  */

24
25 public class Date extends java.util.Date JavaDoc implements SCO
26 {
27     private transient Object JavaDoc owner;
28     private transient String JavaDoc fieldName;
29
30
31     /**
32      * Creates a <tt>Date</tt> object that represents the same time as the
33      * given <tt>java.util.Date</tt>. Assigns owning object and field name.
34      *
35      * @param owner the owning object
36      * @param fieldName the owning field name
37      * @param date the initial date value
38      */

39
40     public Date(Object JavaDoc owner, String JavaDoc fieldName, java.util.Date JavaDoc date)
41     {
42         super(date.getTime());
43
44         this.owner = owner;
45         this.fieldName = fieldName;
46     }
47
48
49     public Object JavaDoc getOwner()
50     {
51         return owner;
52     }
53
54
55     public String JavaDoc getFieldName()
56     {
57         return fieldName;
58     }
59
60
61     public void makeDirty()
62     {
63         if (owner != null)
64             JDOHelper.makeDirty(owner, fieldName);
65     }
66
67
68     public void applyUpdates()
69     {
70     }
71
72
73     public void unsetOwner()
74     {
75         owner = null;
76         fieldName = null;
77     }
78
79
80     /**
81      * Creates and returns a copy of this object.
82      *
83      * <p>Mutable second-class Objects are required to provide a public
84      * clone method in order to allow for copying PersistenceCapable
85      * objects. In contrast to Object.clone(), this method must not throw a
86      * CloneNotSupportedException.
87      */

88
89     public Object JavaDoc clone()
90     {
91         Object JavaDoc obj = super.clone();
92
93         ((Date)obj).unsetOwner();
94
95         return obj;
96     }
97
98
99     public void setTime(long time)
100     {
101         super.setTime(time);
102         makeDirty();
103     }
104
105
106     /**
107      * Sets the year of this <tt>Date</tt> object to be the specified
108      * value plus 1900. This <code>Date</code> object is modified so
109      * that it represents a point in time within the specified year,
110      * with the month, date, hour, minute, and second the same as
111      * before, as interpreted in the local time zone. (Of course, if
112      * the date was February 29, for example, and the year is set to a
113      * non-leap year, then the new date will be treated as if it were
114      * on March 1.)
115      *
116      * @param year the year value.
117      * @see java.util.Calendar
118      * @deprecated As of JDK version 1.1,
119      * replaced by <code>Calendar.set(Calendar.YEAR, year + 1900)</code>.
120      */

121
122     public void setYear(int year)
123     {
124         super.setYear(year);
125         makeDirty();
126     }
127
128
129     /**
130      * Sets the month of this date to the specified value. This
131      * <tt>Date</tt> object is modified so that it represents a point
132      * in time within the specified month, with the year, date, hour,
133      * minute, and second the same as before, as interpreted in the
134      * local time zone. If the date was October 31, for example, and
135      * the month is set to June, then the new date will be treated as
136      * if it were on July 1, because June has only 30 days.
137      *
138      * @param month the month value between 0-11.
139      * @see java.util.Calendar
140      * @deprecated As of JDK version 1.1,
141      * replaced by <code>Calendar.set(Calendar.MONTH, int month)</code>.
142      */

143
144     public void setMonth(int month)
145     {
146         super.setMonth(month);
147         makeDirty();
148     }
149
150
151     /**
152      * Sets the day of the month of this <tt>Date</tt> object to the
153      * specified value. This <tt>Date</tt> object is modified so that
154      * it represents a point in time within the specified day of the
155      * month, with the year, month, hour, minute, and second the same
156      * as before, as interpreted in the local time zone. If the date
157      * was April 30, for example, and the date is set to 31, then it
158      * will be treated as if it were on May 1, because April has only
159      * 30 days.
160      *
161      * @param date the day of the month value between 1-31.
162      * @see java.util.Calendar
163      * @deprecated As of JDK version 1.1,
164      * replaced by <code>Calendar.set(Calendar.DAY_OF_MONTH, int date)</code>.
165      */

166
167     public void setDate(int date)
168     {
169         super.setDate(date);
170         makeDirty();
171     }
172
173
174     /**
175      * Sets the hour of this <tt>Date</tt> object to the specified value.
176      * This <tt>Date</tt> object is modified so that it represents a point
177      * in time within the specified hour of the day, with the year, month,
178      * date, minute, and second the same as before, as interpreted in the
179      * local time zone.
180      *
181      * @param hours the hour value.
182      * @see java.util.Calendar
183      * @deprecated As of JDK version 1.1,
184      * replaced by <code>Calendar.set(Calendar.HOUR_OF_DAY, int hours)</code>.
185      */

186
187     public void setHours(int hours)
188     {
189         super.setHours(hours);
190         makeDirty();
191     }
192
193
194     /**
195      * Sets the minutes of this <tt>Date</tt> object to the specified value.
196      * This <tt>Date</tt> object is modified so that it represents a point
197      * in time within the specified minute of the hour, with the year, month,
198      * date, hour, and second the same as before, as interpreted in the
199      * local time zone.
200      *
201      * @param minutes the value of the minutes.
202      * @see java.util.Calendar
203      * @deprecated As of JDK version 1.1,
204      * replaced by <code>Calendar.set(Calendar.MINUTE, int minutes)</code>.
205      */

206
207     public void setMinutes(int minutes)
208     {
209         super.setMinutes(minutes);
210         makeDirty();
211     }
212
213
214     /**
215      * Sets the seconds of this <tt>Date</tt> to the specified value.
216      * This <tt>Date</tt> object is modified so that it represents a
217      * point in time within the specified second of the minute, with
218      * the year, month, date, hour, and minute the same as before, as
219      * interpreted in the local time zone.
220      *
221      * @param seconds the seconds value.
222      * @see java.util.Calendar
223      * @deprecated As of JDK version 1.1,
224      * replaced by <code>Calendar.set(Calendar.SECOND, int seconds)</code>.
225      */

226
227     public void setSeconds(int seconds)
228     {
229         super.setSeconds(seconds);
230         makeDirty();
231     }
232
233
234     /**
235      * Replaces the object to be serialized with a java.util.Date object.
236      * Invoked by the serialization mechanism to obtain an alternative object
237      * to be used when writing an object to the stream.
238      *
239      * @return
240      * The <code>Date</code> to be serialized instead of this object.
241      */

242
243     protected Object JavaDoc writeReplace() throws ObjectStreamException JavaDoc
244     {
245         return new java.util.Date JavaDoc(getTime());
246     }
247 }
248
Popular Tags