001: /**********************************************************************
002: Copyright (c) 2007 Andy Jefferson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: ...
017: **********************************************************************/package org.jpox.identity;
018:
019: import java.io.IOException;
020: import java.io.ObjectInput;
021: import java.io.ObjectOutput;
022:
023: /**
024: * Single-Field identity with a short/Short field.
025: * @version $Revision: 1.1 $
026: */
027: public class ShortIdentity extends SingleFieldIdentity {
028: private short key;
029:
030: /**
031: * Constructor with class and key.
032: * @param pcClass the class
033: * @param key the key
034: */
035: public ShortIdentity(Class pcClass, short key) {
036: super (pcClass);
037: this .key = key;
038: hashCode = hashClassName() ^ this .key;
039: }
040:
041: /**
042: * Constructor with class and key.
043: * @param pcClass the class
044: * @param key the key
045: */
046: public ShortIdentity(Class pcClass, Short key) {
047: super (pcClass);
048: setKeyAsObject(key);
049: this .key = key.shortValue();
050: hashCode = hashClassName() ^ this .key;
051: }
052:
053: /**
054: * Constructor with class and key.
055: * @param pcClass the class
056: * @param str the key
057: */
058: public ShortIdentity(Class pcClass, String str) {
059: super (pcClass);
060: assertKeyNotNull(str);
061: this .key = Short.parseShort(str);
062: hashCode = hashClassName() ^ this .key;
063: }
064:
065: /**
066: * Constructor only for Externalizable.
067: */
068: public ShortIdentity() {
069: }
070:
071: /**
072: * Return the key.
073: * @return the key
074: */
075: public short getKey() {
076: return key;
077: }
078:
079: /**
080: * Return the String form of the key.
081: * @return the String form of the key
082: */
083: public String toString() {
084: return Short.toString(key);
085: }
086:
087: /**
088: * Determine if the other object represents the same object id.
089: * @param obj the other object
090: * @return true if both objects represent the same object id
091: */
092: public boolean equals(Object obj) {
093: if (this == obj) {
094: return true;
095: } else if (!super .equals(obj)) {
096: return false;
097: } else {
098: ShortIdentity other = (ShortIdentity) obj;
099: return key == other.key;
100: }
101: }
102:
103: /**
104: * Create the key as an Object.
105: * @return the key as an Object
106: */
107: protected Object createKeyAsObject() {
108: return new Short(key);
109: }
110:
111: /**
112: * Write this object. Write the superclass first.
113: * @param out the output
114: */
115: public void writeExternal(ObjectOutput out) throws IOException {
116: super .writeExternal(out);
117: out.writeShort(key);
118: }
119:
120: /**
121: * Read this object. Read the superclass first.
122: * @param in the input
123: */
124: public void readExternal(ObjectInput in) throws IOException,
125: ClassNotFoundException {
126: super.readExternal(in);
127: key = in.readShort();
128: }
129: }
|