001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.jdo.sco;
012:
013: import com.versant.core.common.Debug;
014: import com.versant.core.jdo.VersantStateManager;
015: import com.versant.core.common.VersantFieldMetaData;
016:
017: import javax.jdo.spi.PersistenceCapable;
018: import java.io.*;
019:
020: /**
021: * @keep-all
022: */
023: public class Date extends java.util.Date implements SCODate {
024:
025: private transient PersistenceCapable owner;
026: private final transient VersantFieldMetaData fmd;
027: private transient VersantStateManager stateManager;
028:
029: /**
030: * Creates a <code>Date</code> object that represents the given time
031: * in milliseconds.
032: *
033: * @param date the number of milliseconds
034: */
035: public Date(PersistenceCapable owner,
036: VersantStateManager stateManager, VersantFieldMetaData fmd,
037: long date) {
038: super (date);
039: this .owner = owner;
040: this .stateManager = stateManager;
041: this .fmd = fmd;
042: }
043:
044: /**
045: * Sets the <tt>Date</tt> object to represent a point in time that is
046: * <tt>time</tt> milliseconds after January 1, 1970 00:00:00 GMT.
047: *
048: * @param time the number of milliseconds.
049: * @see java.util.Date
050: */
051: public void setTime(long time) {
052: this .makeDirty();
053: super .setTime(time);
054: }
055:
056: /**
057: * Creates and returns a copy of this object.
058: * <p/>
059: * Mutable Second Class Objects are required to provide a public
060: * clone method in order to allow for copying PersistenceCapable
061: * objects. In contrast to Object.clone(), this method must not throw a
062: * CloneNotSupportedException.
063: */
064: public Object clone() {
065: Object obj = super .clone();
066: if (obj instanceof VersantSimpleSCO) {
067: ((VersantSimpleSCO) obj).makeTransient();
068: }
069: return obj;
070: }
071:
072: /** -----------Depricated Methods------------------*/
073:
074: /**
075: * Sets the year of this <tt>Date</tt> object to be the specified
076: * value plus 1900.
077: *
078: * @param year the year value.
079: * @see java.util.Calendar
080: * @see java.util.Date
081: * @deprecated As of JDK version 1.1,
082: * replaced by <code>Calendar.set(Calendar.YEAR, year + 1900)</code>.
083: */
084: public void setYear(int year) {
085: this .makeDirty();
086: super .setYear(year);
087: }
088:
089: /**
090: * Sets the month of this date to the specified value.
091: *
092: * @param month the month value between 0-11.
093: * @see java.util.Calendar
094: * @see java.util.Date
095: * @deprecated As of JDK version 1.1,
096: * replaced by <code>Calendar.set(Calendar.MONTH, int month)</code>.
097: */
098: public void setMonth(int month) {
099: this .makeDirty();
100: super .setMonth(month);
101: }
102:
103: /**
104: * Sets the day of the month of this <tt>Date</tt> object to the
105: * specified value.
106: *
107: * @param date the day of the month value between 1-31.
108: * @see java.util.Calendar
109: * @see java.util.Date
110: * @deprecated As of JDK version 1.1,
111: * replaced by <code>Calendar.set(Calendar.DAY_OF_MONTH, int date)</code>.
112: */
113: public void setDate(int date) {
114: this .makeDirty();
115: super .setDate(date);
116: }
117:
118: /**
119: * Sets the hour of this <tt>Date</tt> object to the specified value.
120: *
121: * @param hours the hour value.
122: * @see java.util.Calendar
123: * @see java.util.Date
124: * @deprecated As of JDK version 1.1,
125: * replaced by <code>Calendar.set(Calendar.HOUR_OF_DAY, int hours)</code>.
126: */
127: public void setHours(int hours) {
128: this .makeDirty();
129: super .setHours(hours);
130: }
131:
132: /**
133: * Sets the minutes of this <tt>Date</tt> object to the specified value.
134: *
135: * @param minutes the value of the minutes.
136: * @see java.util.Calendar
137: * @see java.util.Date
138: * @deprecated As of JDK version 1.1,
139: * replaced by <code>Calendar.set(Calendar.MINUTE, int minutes)</code>.
140: */
141: public void setMinutes(int minutes) {
142: this .makeDirty();
143: super .setMinutes(minutes);
144: }
145:
146: /**
147: * Sets the seconds of this <tt>Date</tt> to the specified value.
148: *
149: * @param seconds the seconds value.
150: * @see java.util.Calendar
151: * @see java.util.Date
152: * @deprecated As of JDK version 1.1,
153: * replaced by <code>Calendar.set(Calendar.SECOND, int seconds)</code>.
154: */
155: public void setSeconds(int seconds) {
156: this .makeDirty();
157: super .setSeconds(seconds);
158: }
159:
160: /** ---------------- internal methods ------------------- */
161:
162: /**
163: * Sets the <tt>Date</tt> object without notification of the Owner
164: * field. Used internaly to populate date from DB
165: *
166: * @param time the number of milliseconds.
167: * @see java.util.Date
168: */
169: public void setTimeInternal(long time) {
170: super .setTime(time);
171: }
172:
173: /**
174: * Nullifies references to the owner Object and Field
175: */
176: public void makeTransient() {
177: this .owner = null;
178: this .stateManager = null;
179: }
180:
181: /**
182: * Returns the owner object of the SCO instance
183: *
184: * @return owner object
185: */
186: public Object getOwner() {
187: return this .owner;
188: }
189:
190: /**
191: * Marks object dirty
192: */
193: public void makeDirty() {
194: if (stateManager != null) {
195: stateManager.makeDirty(owner, fmd.getManagedFieldNo());
196: }
197: }
198:
199: public void reset() {
200: }
201:
202: public void writeExternal(ObjectOutput out) throws IOException {
203: out.writeLong(super .getTime());
204: }
205:
206: public void readExternal(ObjectInput in) throws IOException,
207: ClassNotFoundException {
208: super .setTime(in.readLong());
209: }
210:
211: public static void main(String[] args) throws Exception {
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(
220: new ByteArrayInputStream(bout.toByteArray()));
221: Object o = in.readObject();
222: Debug.OUT.println("####### o.type = " + o.getClass().getName());
223: Debug.OUT.println("####### equal = " + d.equals(o));
224: }
225:
226: }
|