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: import java.util.Currency;
024: import java.util.Date;
025:
026: import org.jpox.exceptions.JPOXUserException;
027: import org.jpox.util.I18nUtils;
028:
029: /**
030: * Single-Field identity with an Object field.
031: * Supports java.util.Date, java.util.Locale, java.util.Currency.
032: *
033: * @version $Revision: 1.2 $
034: */
035: public class ObjectIdentity extends SingleFieldIdentity {
036: /** The delimiter for String constructor. */
037: private static final String STRING_DELIMITER = ":";
038:
039: /**
040: * Constructor with class and key.
041: * @param pcClass the class
042: * @param param the key
043: */
044: public ObjectIdentity(Class pcClass, Object param) {
045: super (pcClass);
046: assertKeyNotNull(param);
047: String paramString = null;
048: String keyString = null;
049: String className = null;
050: if (param instanceof String) {
051: /* The paramString is of the form "<className>:<keyString>" */
052: paramString = (String) param;
053: if (paramString.length() < 3) {
054: throw new JPOXUserException(
055: "ObjectIdentity should be passed a String greater than 3 characters in length");
056: }
057: int indexOfDelimiter = paramString
058: .indexOf(STRING_DELIMITER);
059: if (indexOfDelimiter < 0) {
060: throw new JPOXUserException(
061: "ObjectIdentity should be passed a String with a delimiter present");
062: }
063: keyString = paramString.substring(indexOfDelimiter + 1);
064: className = paramString.substring(0, indexOfDelimiter);
065:
066: // Create the object form of the key
067: if (className.equals("java.util.Date")) {
068: keyAsObject = new Date(Long.parseLong(keyString));
069: } else if (className.equals("java.util.Locale")) {
070: keyAsObject = I18nUtils.getLocaleFromString(keyString);
071: } else if (className.equals("java.util.Currency")) {
072: keyAsObject = Currency.getInstance(keyString);
073: } else {
074: throw new JPOXUserException("Class for ObjectIdentity "
075: + className + " is not supported as a PK type");
076: }
077: } else {
078: keyAsObject = param;
079: }
080: hashCode = hashClassName() ^ keyAsObject.hashCode();
081: }
082:
083: /**
084: * Constructor only for Externalizable.
085: */
086: public ObjectIdentity() {
087: }
088:
089: /**
090: * Return the key.
091: * @return the key
092: */
093: public Object getKey() {
094: return keyAsObject;
095: }
096:
097: /**
098: * Return the String form of the object id.
099: * The class of the object id is written as the first part of the result so that the class can be
100: * reconstructed later. Then the toString of the key instance is appended. During construction, this
101: * process is reversed. The class is extracted from the first part of the String, and the String
102: * constructor of the key is used to construct the key itself.
103: * @return the String form of the key
104: */
105: public String toString() {
106: return keyAsObject.getClass().getName() + STRING_DELIMITER
107: + keyAsObject.toString();
108: }
109:
110: /**
111: * Determine if the other object represents the same object id.
112: * @param obj the other object
113: * @return true if both objects represent the same object id
114: */
115: public boolean equals(Object obj) {
116: if (this == obj) {
117: return true;
118: } else if (!super .equals(obj)) {
119: return false;
120: } else {
121: ObjectIdentity other = (ObjectIdentity) obj;
122: return keyAsObject.equals(other.keyAsObject);
123: }
124: }
125:
126: /**
127: * Write this object. Write the superclass first.
128: * @param out the output
129: */
130: public void writeExternal(ObjectOutput out) throws IOException {
131: super .writeExternal(out);
132: out.writeObject(keyAsObject);
133: }
134:
135: /**
136: * Read this object. Read the superclass first.
137: * @param in the input
138: */
139: public void readExternal(ObjectInput in) throws IOException,
140: ClassNotFoundException {
141: super.readExternal(in);
142: keyAsObject = in.readObject();
143: }
144: }
|