001: /**********************************************************************
002: Copyright (c) 2007 Xuan Baldauf 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: 2007 Andy Jefferson - generalised for all datastores
017: ...
018: **********************************************************************/package org.jpox.identity;
019:
020: /**
021: * Identity for use with datastore-identity where the datastore provides a unique "identity" key per object
022: * and hence doesn't need the class name. The behaviour of this class is governed by JDO spec 5.4.3.
023: * Main benefit over OIDImpl is size.
024: *
025: * @version $Revision: 1.1 $
026: */
027: public class DatastoreUniqueOID implements java.io.Serializable, OID {
028: // JDO spec 5.4.3 says: all serializable fields of ObjectID classes are required to be public.
029:
030: /** The key value. */
031: public final long key;
032:
033: /**
034: * Creates an OID with no value. Required by the JDO spec.
035: */
036: public DatastoreUniqueOID() {
037: this .key = -1;
038: }
039:
040: /**
041: * Constructor taking the long form of the key.
042: * @param key The key
043: */
044: public DatastoreUniqueOID(long key) {
045: this .key = key;
046: }
047:
048: /**
049: * Constructs an OID from its string representation that is consistent with the output of toString().
050: * @param str the string representation of an OID
051: * @exception IllegalArgumentException if the given string representation is not valid.
052: * @see #toString
053: */
054: public DatastoreUniqueOID(String str)
055: throws IllegalArgumentException {
056: this .key = Long.parseLong(str);
057: }
058:
059: /**
060: * Accessor for the key value.
061: * @return The key value
062: */
063: public Object getKeyValue() {
064: return new Long(key);
065: }
066:
067: /**
068: * Convenience accessor for the long form of the key.
069: * @return long primitive form of the key
070: */
071: public long getKey() {
072: return key;
073: }
074:
075: /**
076: * Accessor for the PersistenceCapable class name.
077: * @return PC class name
078: */
079: public String getPcClass() {
080: // We do not need a class name.
081: throw new UnsupportedOperationException();
082: }
083:
084: /**
085: * Equality operator.
086: * @param obj Object to compare against
087: * @return Whether they are equal
088: */
089: public boolean equals(Object obj) {
090: if (obj == this ) {
091: return true;
092: }
093: if (obj == null) {
094: return false;
095: }
096: if (!(obj.getClass().equals(this .getClass()))) {
097: return false;
098: }
099: return key == ((DatastoreUniqueOID) obj).key;
100: }
101:
102: /**
103: * Accessor for the hashcode
104: * @return Hashcode for this object
105: */
106: public int hashCode() {
107: // Assume that we wont overflow the int range
108: return (int) key;
109: }
110:
111: /**
112: * Creates a String representation of the datastore identity, formed from the key value.
113: * This will be something like <pre>3254</pre>
114: * @return The String form of the identity
115: */
116: public String toString() {
117: return Long.toString(key);
118: }
119: }
|