01: /*
02: $Header: /cvsroot/xorm/xorm/src/org/xorm/ObjectId.java,v 1.4 2002/10/01 01:51:52 wbiggs Exp $
03:
04: This file is part of XORM.
05:
06: XORM is free software; you can redistribute it and/or modify
07: it under the terms of the GNU General Public License as published by
08: the Free Software Foundation; either version 2 of the License, or
09: (at your option) any later version.
10:
11: XORM is distributed in the hope that it will be useful,
12: but WITHOUT ANY WARRANTY; without even the implied warranty of
13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: GNU General Public License for more details.
15:
16: You should have received a copy of the GNU General Public License
17: along with XORM; if not, write to the Free Software
18: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: */
20: package org.xorm;
21:
22: import java.io.Serializable;
23: import javax.jdo.JDOFatalException;
24:
25: /**
26: * The shared ObjectId class used to represent datastore identity.
27: */
28: public class ObjectId implements Serializable {
29: // These are public as specified in JDO 5.4.2, Datastore Identity
30: public Class mappedClass;
31: public Object id;
32:
33: // This method is required for spec adherence
34: public ObjectId() {
35: }
36:
37: public ObjectId(String mangledId) {
38: // De-mangle the ID
39: int p = mangledId.indexOf(';');
40: if (p == -1) {
41: throw new JDOFatalException("Badly formatted object ID");
42: }
43: try {
44: mappedClass = Class.forName(mangledId.substring(0, p));
45: } catch (ClassNotFoundException e) {
46: throw new JDOFatalException("Unknown object class "
47: + mangledId.substring(0, p));
48: }
49: id = mangledId.substring(p + 1);
50: }
51:
52: /** Constructs a new ObjectId for the given class and argument. */
53: public ObjectId(Class mappedClass, Object id) {
54: this .mappedClass = mappedClass;
55: this .id = id;
56: }
57:
58: /**
59: * Returns the String representation of this ObjectId,
60: * which is simply getId().toString() with a null check.
61: * This value can later be used with PersistenceManager.
62: * newObjectIdInstance().
63: */
64: public String toString() {
65: return mappedClass.getName() + ';'
66: + ((id == null) ? "" : id.toString());
67: }
68:
69: public boolean equals(Object o) {
70: if (o == this )
71: return true;
72: if (o == null)
73: return false;
74: if (!(o instanceof ObjectId))
75: return false;
76: ObjectId other = (ObjectId) o;
77: boolean e1 = (mappedClass == null) ? (other.mappedClass == null)
78: : (mappedClass.equals(other.mappedClass));
79: boolean e2 = (id == null) ? (other.id == null) : (id
80: .equals(other.id));
81: return e1 && e2;
82: }
83: }
|