KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > sco > Date


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo.sco;
13
14 import com.versant.core.common.Debug;
15 import com.versant.core.jdo.VersantStateManager;
16 import com.versant.core.common.VersantFieldMetaData;
17
18 import javax.jdo.spi.PersistenceCapable;
19 import java.io.*;
20
21 /**
22  * @keep-all
23  */

24 public class Date extends java.util.Date JavaDoc implements SCODate {
25
26     private transient PersistenceCapable owner;
27     private final transient VersantFieldMetaData fmd;
28     private transient VersantStateManager stateManager;
29
30     /**
31      * Creates a <code>Date</code> object that represents the given time
32      * in milliseconds.
33      *
34      * @param date the number of milliseconds
35      */

36     public Date(PersistenceCapable owner,
37                 VersantStateManager stateManager, VersantFieldMetaData fmd,
38                 long date) {
39         super(date);
40         this.owner = owner;
41         this.stateManager = stateManager;
42         this.fmd = fmd;
43     }
44
45     /**
46      * Sets the <tt>Date</tt> object to represent a point in time that is
47      * <tt>time</tt> milliseconds after January 1, 1970 00:00:00 GMT.
48      *
49      * @param time the number of milliseconds.
50      * @see java.util.Date
51      */

52     public void setTime(long time) {
53         this.makeDirty();
54         super.setTime(time);
55     }
56
57     /**
58      * Creates and returns a copy of this object.
59      * <p/>
60      * Mutable Second Class Objects are required to provide a public
61      * clone method in order to allow for copying PersistenceCapable
62      * objects. In contrast to Object.clone(), this method must not throw a
63      * CloneNotSupportedException.
64      */

65     public Object JavaDoc clone() {
66         Object JavaDoc obj = super.clone();
67         if (obj instanceof VersantSimpleSCO) {
68             ((VersantSimpleSCO) obj).makeTransient();
69         }
70         return obj;
71     }
72
73     /** -----------Depricated Methods------------------*/
74
75     /**
76      * Sets the year of this <tt>Date</tt> object to be the specified
77      * value plus 1900.
78      *
79      * @param year the year value.
80      * @see java.util.Calendar
81      * @see java.util.Date
82      * @deprecated As of JDK version 1.1,
83      * replaced by <code>Calendar.set(Calendar.YEAR, year + 1900)</code>.
84      */

85     public void setYear(int year) {
86         this.makeDirty();
87         super.setYear(year);
88     }
89
90     /**
91      * Sets the month of this date to the specified value.
92      *
93      * @param month the month value between 0-11.
94      * @see java.util.Calendar
95      * @see java.util.Date
96      * @deprecated As of JDK version 1.1,
97      * replaced by <code>Calendar.set(Calendar.MONTH, int month)</code>.
98      */

99     public void setMonth(int month) {
100         this.makeDirty();
101         super.setMonth(month);
102     }
103
104     /**
105      * Sets the day of the month of this <tt>Date</tt> object to the
106      * specified value.
107      *
108      * @param date the day of the month value between 1-31.
109      * @see java.util.Calendar
110      * @see java.util.Date
111      * @deprecated As of JDK version 1.1,
112      * replaced by <code>Calendar.set(Calendar.DAY_OF_MONTH, int date)</code>.
113      */

114     public void setDate(int date) {
115         this.makeDirty();
116         super.setDate(date);
117     }
118
119     /**
120      * Sets the hour of this <tt>Date</tt> object to the specified value.
121      *
122      * @param hours the hour value.
123      * @see java.util.Calendar
124      * @see java.util.Date
125      * @deprecated As of JDK version 1.1,
126      * replaced by <code>Calendar.set(Calendar.HOUR_OF_DAY, int hours)</code>.
127      */

128     public void setHours(int hours) {
129         this.makeDirty();
130         super.setHours(hours);
131     }
132
133     /**
134      * Sets the minutes of this <tt>Date</tt> object to the specified value.
135      *
136      * @param minutes the value of the minutes.
137      * @see java.util.Calendar
138      * @see java.util.Date
139      * @deprecated As of JDK version 1.1,
140      * replaced by <code>Calendar.set(Calendar.MINUTE, int minutes)</code>.
141      */

142     public void setMinutes(int minutes) {
143         this.makeDirty();
144         super.setMinutes(minutes);
145     }
146
147     /**
148      * Sets the seconds of this <tt>Date</tt> to the specified value.
149      *
150      * @param seconds the seconds value.
151      * @see java.util.Calendar
152      * @see java.util.Date
153      * @deprecated As of JDK version 1.1,
154      * replaced by <code>Calendar.set(Calendar.SECOND, int seconds)</code>.
155      */

156     public void setSeconds(int seconds) {
157         this.makeDirty();
158         super.setSeconds(seconds);
159     }
160
161     /** ---------------- internal methods ------------------- */
162
163     /**
164      * Sets the <tt>Date</tt> object without notification of the Owner
165      * field. Used internaly to populate date from DB
166      *
167      * @param time the number of milliseconds.
168      * @see java.util.Date
169      */

170     public void setTimeInternal(long time) {
171         super.setTime(time);
172     }
173
174     /**
175      * Nullifies references to the owner Object and Field
176      */

177     public void makeTransient() {
178         this.owner = null;
179         this.stateManager = null;
180     }
181
182     /**
183      * Returns the owner object of the SCO instance
184      *
185      * @return owner object
186      */

187     public Object JavaDoc getOwner() {
188         return this.owner;
189     }
190
191     /**
192      * Marks object dirty
193      */

194     public void makeDirty() {
195         if (stateManager != null) {
196             stateManager.makeDirty(owner, fmd.getManagedFieldNo());
197         }
198     }
199
200     public void reset() {
201     }
202
203     public void writeExternal(ObjectOutput out) throws IOException {
204         out.writeLong(super.getTime());
205     }
206
207     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
208         super.setTime(in.readLong());
209     }
210
211     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
212         Date d = new Date(null, null, null, System.currentTimeMillis());
213         final long time = System.currentTimeMillis() - 5000;
214         d.setTime(time);
215         ByteArrayOutputStream bout = new ByteArrayOutputStream();
216         ObjectOutputStream out = new ObjectOutputStream(bout);
217         out.writeObject(d);
218
219         ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()));
220         Object JavaDoc o = in.readObject();
221         Debug.OUT.println("####### o.type = " + o.getClass().getName());
222         Debug.OUT.println("####### equal = " + d.equals(o));
223     }
224
225 }
226
227
Popular Tags