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 long/Long field.
025: *
026: * @version $Revision: 1.1 $
027: */
028: public class LongIdentity extends SingleFieldIdentity {
029: private long key;
030:
031: /**
032: * Constructor with class and key.
033: * @param pcClass the class
034: * @param key the key
035: */
036: public LongIdentity(Class pcClass, long key) {
037: super (pcClass);
038: this .key = key;
039: hashCode = hashClassName() ^ (int) this .key;
040: }
041:
042: /**
043: * Constructor with class and key.
044: * @param pcClass the class
045: * @param key the key
046: */
047: public LongIdentity(Class pcClass, Long key) {
048: super (pcClass);
049: setKeyAsObject(key);
050: this .key = key.longValue();
051: hashCode = hashClassName() ^ (int) this .key;
052: }
053:
054: /**
055: * Constructor with class and key.
056: * @param pcClass the class
057: * @param str the key
058: */
059: public LongIdentity(Class pcClass, String str) {
060: super (pcClass);
061: assertKeyNotNull(str);
062: this .key = Long.parseLong(str);
063: hashCode = hashClassName() ^ (int) this .key;
064: }
065:
066: /**
067: * Constructor only for Externalizable.
068: */
069: public LongIdentity() {
070: }
071:
072: /**
073: * Return the key.
074: * @return the key
075: */
076: public long getKey() {
077: return key;
078: }
079:
080: /**
081: * Return the String form of the key.
082: * @return the String form of the key
083: */
084: public String toString() {
085: return Long.toString(key);
086: }
087:
088: /**
089: * Determine if the other object represents the same object id.
090: * @param obj the other object
091: * @return true if both objects represent the same object id
092: */
093: public boolean equals(Object obj) {
094: if (this == obj) {
095: return true;
096: } else if (!super .equals(obj)) {
097: return false;
098: } else {
099: LongIdentity other = (LongIdentity) obj;
100: return key == other.key;
101: }
102: }
103:
104: /**
105: * Create the key as an Object.
106: * @return the key as an Object
107: */
108: protected Object createKeyAsObject() {
109: return new Long(key);
110: }
111:
112: /**
113: * Write this object. Write the superclass first.
114: * @param out the output
115: */
116: public void writeExternal(ObjectOutput out) throws IOException {
117: super .writeExternal(out);
118: out.writeLong(key);
119: }
120:
121: /**
122: * Read this object. Read the superclass first.
123: * @param in the input
124: */
125: public void readExternal(ObjectInput in) throws IOException,
126: ClassNotFoundException {
127: super.readExternal(in);
128: key = in.readLong();
129: }
130: }
|