KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > sco > detached > DetachSCODate


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.detached;
13
14 import com.versant.core.common.Debug;
15 import com.versant.core.jdo.VersantStateManager;
16 import com.versant.core.common.VersantFieldMetaData;
17 import com.versant.core.jdo.sco.VersantSimpleSCO;
18 import com.versant.core.jdo.VersantStateManager;
19
20 import javax.jdo.spi.PersistenceCapable;
21 import java.io.*;
22
23 public class DetachSCODate extends java.util.Date JavaDoc implements Serializable, VersantSimpleSCO {
24
25     private PersistenceCapable owner;
26     private int fieldNo;
27     private VersantStateManager stateManager;
28
29     /**
30      * Creates a <code>DetachSCODate</code> object that represents the given time
31      * in milliseconds.
32      *
33      * @param date the number of milliseconds
34      */

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

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

64     public Object JavaDoc clone() {
65         Object JavaDoc obj = super.clone();
66         if (obj instanceof VersantSimpleSCO) {
67             ((VersantSimpleSCO) obj).makeTransient();
68         }
69
70         return obj;
71     }
72
73     /** -----------Depricated Methods------------------*/
74
75     /**
76      * Sets the year of this <tt>DetachSCODate</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>DetachSCODate</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>DetachSCODate</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>DetachSCODate</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>DetachSCODate</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>DetachSCODate</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 && owner != null) {
196             stateManager.makeDirty(owner, fieldNo);
197         }
198     }
199
200     public void reset() {
201     }
202
203     /**
204      * Return the java class that is represented by this sco.
205      *
206      * @return
207      */

208     public Class JavaDoc getJavaType() {
209         return java.util.Date JavaDoc.class;
210     }
211
212     public void writeExternal(ObjectOutput out) throws IOException {
213         out.writeLong(super.getTime());
214     }
215
216     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
217         super.setTime(in.readLong());
218     }
219
220     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
221         DetachSCODate d = new DetachSCODate(null, null, null, System.currentTimeMillis());
222         final long time = System.currentTimeMillis() - 5000;
223         d.setTime(time);
224         ByteArrayOutputStream bout = new ByteArrayOutputStream();
225         ObjectOutputStream out = new ObjectOutputStream(bout);
226         out.writeObject(d);
227
228         ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()));
229         Object JavaDoc o = in.readObject();
230         Debug.OUT.println("####### o.type = " + o.getClass().getName());
231         Debug.OUT.println("####### equal = " + d.equals(o));
232     }
233 }
234
Popular Tags