001: /*
002: * Helma License Notice
003: *
004: * The contents of this file are subject to the Helma License
005: * Version 2.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://adele.helma.org/download/helma/license.txt
008: *
009: * Copyright 1998-2003 Helma Software. All Rights Reserved.
010: *
011: * $RCSfile$
012: * $Author: root $
013: * $Revision: 8604 $
014: * $Date: 2007-09-28 15:16:38 +0200 (Fre, 28 Sep 2007) $
015: */
016:
017: package helma.objectmodel.db;
018:
019: import java.io.IOException;
020: import java.io.ObjectInputStream;
021: import java.io.ObjectOutputStream;
022: import java.io.Serializable;
023:
024: /**
025: * This is the internal representation of a database key. It is constructed
026: * from the logical table (type) name and the object's primary key
027: * within the table. Currently only single keys are supported.
028: */
029: public final class DbKey implements Key, Serializable {
030: // the name of the prototype which defines the storage of this object.
031: // this is the name of the object's prototype, or one of its ancestors.
032: // If null, the object is stored in the embedded db.
033: private String storageName;
034:
035: // the id that defines this key's object within the above storage space
036: private String id;
037:
038: // lazily initialized hashcode
039: private transient int hashcode = 0;
040:
041: static final long serialVersionUID = 1618863960930966588L;
042:
043: /**
044: * make a key for a persistent Object, describing its datasource and id.
045: */
046: public DbKey(DbMapping dbmap, String id) {
047: this .id = id;
048: this .storageName = (dbmap == null) ? null : dbmap
049: .getStorageTypeName();
050: }
051:
052: /**
053: *
054: *
055: * @param what the other key to be compared with this one
056: *
057: * @return true if both keys are identical
058: */
059: public boolean equals(Object what) {
060: if (what == this ) {
061: return true;
062: }
063:
064: if (!(what instanceof DbKey)) {
065: return false;
066: }
067:
068: DbKey k = (DbKey) what;
069:
070: // storageName is an interned string (by DbMapping, from where we got it)
071: // so we can compare by using == instead of the equals method.
072: return (storageName == k.storageName)
073: && ((id == k.id) || id.equals(k.id));
074: }
075:
076: /**
077: *
078: *
079: * @return this key's hash code
080: */
081: public int hashCode() {
082: if (hashcode == 0) {
083: hashcode = (storageName == null) ? (17 + (37 * id
084: .hashCode()))
085: : (17 + (37 * storageName.hashCode()) + (+37 * id
086: .hashCode()));
087: }
088:
089: return hashcode;
090: }
091:
092: /**
093: *
094: *
095: * @return the key of this key's object's parent object
096: */
097: public Key getParentKey() {
098: return null;
099: }
100:
101: /**
102: *
103: *
104: * @return the unique storage name for this key's object
105: */
106: public String getStorageName() {
107: return storageName;
108: }
109:
110: /**
111: *
112: *
113: * @return this key's object's id
114: */
115: public String getID() {
116: return id;
117: }
118:
119: /**
120: *
121: *
122: * @return a string representation for this key
123: */
124: public String toString() {
125: return (storageName == null) ? ("[" + id + "]") : (storageName
126: + "[" + id + "]");
127: }
128:
129: // We implement write/readObject to set storageName
130: // to the interned version of the string.
131:
132: private void writeObject(ObjectOutputStream stream)
133: throws IOException {
134: stream.writeObject(storageName);
135: stream.writeObject(id);
136: }
137:
138: private void readObject(ObjectInputStream stream)
139: throws IOException, ClassNotFoundException {
140: storageName = (String) stream.readObject();
141: id = (String) stream.readObject();
142: // if storageName is not null, set it to the interned version
143: if (storageName != null) {
144: storageName = storageName.intern();
145: }
146: }
147:
148: }
|