01: /*--------------------------------------------------------------------------*
02: | Copyright (C) 2006 Christopher Kohlhaas |
03: | |
04: | This program is free software; you can redistribute it and/or modify |
05: | it under the terms of the GNU General Public License as published by the |
06: | Free Software Foundation. A copy of the license has been included with |
07: | these distribution in the COPYING file, if not go to www.fsf.org |
08: | |
09: | As a special exception, you are granted the permissions to link this |
10: | program with every library, which license fulfills the Open Source |
11: | Definition as published by the Open Source Initiative (OSI). |
12: *--------------------------------------------------------------------------*/
13: package org.rapla.entities.storage.internal;
14:
15: import java.io.IOException;
16:
17: import org.rapla.entities.RaplaType;
18:
19: /*
20: An identifier could be something like a URI. It is used for:
21: <ol>
22: <li>Lookup or store the identified objects.</li>
23: <li>Distinct the identified objects: Two objects are identical if and
24: only if obj1.getId().equals(obj2.getId()).
25: </li>
26: <li>Serialize/Deserialize relationsships (e.g. references) between
27: objects.</li>
28: </ol>
29: Two conditions should hold for all identifiers:
30: <ol>
31: <li>An identifier is an immutable object.</li>
32:
33: <li>Every object that has got an identifier should keep it for it's
34: lifetime. There is one exception: If it is possible to
35: serialize/deserialize the object-map that no relationship
36: information get's lost and obj1.getId().equals(obj2.getId()) returns
37: the same information with the new ids. This exception is important,
38: if we want to serialize data to an XML-File.</li>
39: </ol>
40: */
41: public class SimpleIdentifier implements java.io.Serializable {
42: // Don't forget to increase the serialVersionUID when you change the fields
43: private static final long serialVersionUID = 1;
44:
45: String type = null;
46: int key;
47: transient String name;
48:
49: private void readObject(java.io.ObjectInputStream in)
50: throws IOException, ClassNotFoundException {
51: in.defaultReadObject();
52: type = type.intern();
53: }
54:
55: public SimpleIdentifier(RaplaType type, int key) {
56: this .type = type.toString().intern();
57: this .key = key;
58: }
59:
60: public boolean equals(Object o) {
61: if (o == null) {
62: return false;
63: }
64: SimpleIdentifier ident = (SimpleIdentifier) o;
65: return (ident.key == key && ident.type == type);
66: }
67:
68: public int hashCode() {
69: int typeHc;
70: if (type != null) {
71: typeHc = type.hashCode();
72: } else {
73: typeHc = 0;
74: }
75: return typeHc + typeHc * key;
76: }
77:
78: public int getKey() {
79: return key;
80: }
81:
82: public String getTypeName() {
83: return type;
84: }
85:
86: public String toString() {
87: if (name == null)
88: name = type + "_" + key;
89: return name;
90: }
91:
92: }
|