1 16 17 21 22 package javax.jdo.identity; 23 24 import java.io.IOException ; 25 import java.io.ObjectInput ; 26 import java.io.ObjectOutput ; 27 28 import java.security.AccessController ; 29 import java.security.PrivilegedAction ; 30 31 import javax.jdo.JDOUserException; 32 33 import javax.jdo.spi.JDOImplHelper; 34 35 38 public class ObjectIdentity extends SingleFieldIdentity { 39 40 42 43 45 private static JDOImplHelper helper = (JDOImplHelper) 46 AccessController.doPrivileged( 47 new PrivilegedAction () { 48 public Object run () { 49 return JDOImplHelper.getInstance(); 50 } 51 } 52 ); 53 54 56 public static String STRING_DELIMITER = ":"; 58 62 public ObjectIdentity (Class pcClass, Object param) { 63 super (pcClass); 64 assertKeyNotNull(param); 65 String paramString = null; 66 String keyString = null; 67 String className = null; 68 if (param instanceof String ) { 69 70 paramString = (String )param; 71 if (paramString.length() < 3) { 72 throw new JDOUserException( 73 msg.msg("EXC_ObjectIdentityStringConstructionTooShort") + msg.msg("EXC_ObjectIdentityStringConstructionUsage", paramString)); 76 } 77 int indexOfDelimiter = paramString.indexOf(STRING_DELIMITER); 78 if (indexOfDelimiter < 0) { 79 throw new JDOUserException( 80 msg.msg("EXC_ObjectIdentityStringConstructionNoDelimiter") + msg.msg("EXC_ObjectIdentityStringConstructionUsage", paramString)); 83 } 84 keyString = paramString.substring(indexOfDelimiter+1); 85 className = paramString.substring(0, indexOfDelimiter); 86 keyAsObject = helper.construct(className, keyString); 87 } else { 88 keyAsObject = param; 89 } 90 hashCode = hashClassName() ^ keyAsObject.hashCode(); 91 } 92 93 95 public ObjectIdentity () { 96 } 97 98 101 public Object getKey () { 102 return keyAsObject; 103 } 104 105 114 public String toString () { 115 return keyAsObject.getClass().getName() 116 + STRING_DELIMITER 117 + keyAsObject.toString(); 118 } 119 120 124 public boolean equals (Object obj) { 125 if (this == obj) { 126 return true; 127 } else if (!super.equals (obj)) { 128 return false; 129 } else { 130 ObjectIdentity other = (ObjectIdentity) obj; 131 return keyAsObject.equals(other.keyAsObject); 132 } 133 } 134 135 138 public void writeExternal(ObjectOutput out) throws IOException { 139 super.writeExternal (out); 140 out.writeObject(keyAsObject); 141 } 142 143 146 public void readExternal(ObjectInput in) 147 throws IOException , ClassNotFoundException { 148 super.readExternal (in); 149 keyAsObject = in.readObject(); 150 } 151 152 } 153 | Popular Tags |