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