01: /**********************************************************************
02: Copyright (c) 2007 Andy Jefferson and others. All rights reserved.
03: Licensed under the Apache License, Version 2.0 (the "License");
04: you may not use this file except in compliance with the License.
05: You may obtain a copy of the License at
06:
07: http://www.apache.org/licenses/LICENSE-2.0
08:
09: Unless required by applicable law or agreed to in writing, software
10: distributed under the License is distributed on an "AS IS" BASIS,
11: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: See the License for the specific language governing permissions and
13: limitations under the License.
14:
15: Contributors:
16: ...
17: **********************************************************************/package org.jpox.identity;
18:
19: import java.io.IOException;
20: import java.io.ObjectInput;
21: import java.io.ObjectOutput;
22:
23: /**
24: * Single-Field identity with a String field.
25: *
26: * @version $Revision: 1.1 $
27: */
28: public class StringIdentity extends SingleFieldIdentity {
29: /**
30: * Constructor with class and key.
31: * @param pcClass the class
32: * @param key the key
33: */
34: public StringIdentity(Class pcClass, String key) {
35: super (pcClass);
36: setKeyAsObject(key);
37: hashCode = hashClassName() ^ key.hashCode();
38: }
39:
40: /**
41: * Constructor only for Externalizable.
42: */
43: public StringIdentity() {
44: }
45:
46: /**
47: * Return the key.
48: * @return the key
49: */
50: public String getKey() {
51: return (String) keyAsObject;
52: }
53:
54: /**
55: * Return the String form of the key.
56: * @return the String form of the key
57: */
58: public String toString() {
59: return (String) keyAsObject;
60: }
61:
62: /**
63: * Determine if the other object represents the same object id.
64: * @param obj the other object
65: * @return true if both objects represent the same object id
66: */
67: public boolean equals(Object obj) {
68: if (this == obj) {
69: return true;
70: } else if (!super .equals(obj)) {
71: return false;
72: } else {
73: StringIdentity other = (StringIdentity) obj;
74: return keyAsObject.equals(other.keyAsObject);
75: }
76: }
77:
78: /**
79: * Write this object. Write the superclass first.
80: * @param out the output
81: */
82: public void writeExternal(ObjectOutput out) throws IOException {
83: super .writeExternal(out);
84: out.writeObject(keyAsObject);
85: }
86:
87: /**
88: * Read this object. Read the superclass first.
89: * @param in the input
90: */
91: public void readExternal(ObjectInput in) throws IOException,
92: ClassNotFoundException {
93: super.readExternal(in);
94: keyAsObject = in.readObject();
95: }
96: }
|