KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > common > NewObjectOID


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.common;
13
14 import com.versant.core.metadata.ClassMetaData;
15 import com.versant.core.util.OIDObjectInput;
16 import com.versant.core.util.OIDObjectOutput;
17
18 import java.io.*;
19
20 /**
21  * This OID class is used for new JDO instances of any type. It is used as
22  * a placeholder for the real OID for new objects. The realOID field is
23  * filled once the real OID has been created so that references to this
24  * OID can be fixed up.
25  */

26 public class NewObjectOID implements OID {
27
28     private ClassMetaData cmd;
29     /**
30      * This is an id that has to be unique in a client space.
31      *
32      * @see #init
33      */

34     public int idNo;
35     /**
36      * The real OID generated for this object. This is filled when the new
37      * object is persisted. Other objects with references to this OID use
38      * this field instead when they are persisted.
39      */

40     public OID realOID;
41
42     public NewObjectOID(ClassMetaData cmd) {
43         this.cmd = cmd;
44     }
45
46     /**
47      * This is only for used for Externalizable
48      *
49      * @see java.io.Externalizable
50      */

51     public NewObjectOID() {
52     }
53
54     /**
55      * Is this an OID for a new object?
56      */

57     public boolean isNew() {
58         return true;
59     }
60
61     public boolean isResolved() {
62         return true;
63     }
64
65     public void resolve(State state) {
66     }
67
68     public int getIdentityType() {
69         return cmd.identityType;
70     }
71
72     /**
73      * Initialise the oid.
74      */

75     public void init(int id) {
76         idNo = id;
77     }
78
79     public int getClassIndex() {
80         return cmd.index;
81     }
82
83     public ClassMetaData getCmd() {
84         return cmd;
85     }
86
87     /**
88      * Get the meta data for our class.
89      *
90      */

91     public ClassMetaData getClassMetaData() {
92         return cmd;
93     }
94
95     public ClassMetaData getAvailableClassMetaData() {
96         return cmd;
97     }
98
99     public ClassMetaData getBaseClassMetaData() {
100         return cmd.top;
101     }
102
103     public void copyKeyFields(Object JavaDoc[] data) {
104         throw BindingSupportImpl.getInstance().internal(
105                 "copyKeyFields(Object[]) called on NewObjectOID");
106     }
107
108     public OID copy() {
109         NewObjectOID nOID = new NewObjectOID();
110         nOID.cmd = cmd;
111         nOID.idNo = idNo;
112         return nOID;
113     }
114
115     public void fillFromPK(Object JavaDoc pk) {
116         //no-op
117
}
118
119     public OID fillFromIDObject(Object JavaDoc id) {
120         //no-op
121
return this;
122     }
123
124     public int hashCode() {
125         return cmd.index + idNo;
126     }
127
128     public boolean equals(Object JavaDoc obj) {
129         if (obj == this) return true;
130         if (obj instanceof NewObjectOID) {
131             NewObjectOID other = (NewObjectOID)obj;
132             if (other.cmd == cmd && other.idNo == idNo) {
133                 return true;
134             }
135         }
136         return false;
137     }
138
139     public String JavaDoc toString() {
140         return toStringImp();
141     }
142
143     /**
144      * Get the toString of this OID even if it has not been resolved.
145      */

146     public String JavaDoc toStringImp() {
147         return "NewObjectOID@" +
148                 Integer.toHexString(System.identityHashCode(this)) +
149                 " classIndex = " + cmd.index +
150                 " id = " + idNo +
151                 " realOID = " + realOID;
152     }
153
154     public String JavaDoc toSString() {
155         return "NewObjectOID@" +
156                 Integer.toHexString(System.identityHashCode(this)) +
157                 " classIndex = " + cmd.index +
158                 " id = " + idNo + (realOID == null ? "" : " realOID = " + realOID.toSString());
159     }
160
161     public String JavaDoc toPkString() {
162         return "(New)";
163     }
164
165     public OID getAvailableOID() {
166         if (realOID != null) return realOID;
167         return this;
168     }
169
170     public OID getRealOID() {
171         return realOID;
172     }
173
174     public OID setRealOid(OID oid) {
175         if (oid == null) {
176             throw BindingSupportImpl.getInstance().internal("The supplied oid is NULL");
177         }
178         if (this.realOID == null) {
179             this.realOID = oid;
180         } else {
181             if (!this.realOID.equals(oid)) {
182                 throw BindingSupportImpl.getInstance().internal("The supplied R-OID is not " +
183                         "equal to the current realOID");
184             }
185         }
186         return oid;
187     }
188
189     public void writeExternal(OIDObjectOutput os) throws IOException {
190         os.writeInt(idNo);
191         os.write(realOID);
192     }
193
194     public void readExternal(OIDObjectInput is) throws ClassNotFoundException JavaDoc,
195             IOException {
196         idNo = is.readInt();
197         realOID = is.readOID();
198     }
199
200     public void fillFromIDString(String JavaDoc idString, int index) {
201         throw BindingSupportImpl.getInstance().internal("fillFromIDString called");
202     }
203
204     public void fillFromIDString(String JavaDoc idString) {
205         throw BindingSupportImpl.getInstance().internal("Should not be called");
206     }
207
208     public int compareTo(Object JavaDoc o) {
209         OID oo = (OID)o;
210         int diff = cmd.index - oo.getClassIndex();
211         if (diff != 0) return diff;
212         if (oo.isNew()) {
213             return idNo - ((NewObjectOID)o).idNo;
214         } else {
215             return -1; // we are always before non-new OIDs
216
}
217     }
218
219     /**
220      * Return the primary key stored in this OID as an long. This will only
221      * be called for datastore identity classes.
222      */

223     public long getLongPrimaryKey() {
224         if (realOID == null) return idNo;
225         return realOID.getLongPrimaryKey();
226     }
227
228     /**
229      * Set the primary key stored in this OID as an long. This will only be
230      * called for datastore identity classes.
231      */

232     public void setLongPrimaryKey(long pk) {
233         realOID.setLongPrimaryKey(pk);
234     }
235
236     public void populateObjectIdClassInstance(Object JavaDoc o) {
237         throw BindingSupportImpl.getInstance().internal("Should not be called");
238      }
239
240     public int getAvailableClassId() {
241         return getAvailableClassMetaData().classId;
242     }
243
244     public NewObjectOID newInstance(ClassMetaData cmd) {
245         return new NewObjectOID(cmd);
246     }
247
248 }
249
Popular Tags