1 16 17 21 22 package javax.jdo.identity; 23 24 import java.io.Externalizable ; 25 import java.io.IOException ; 26 import java.io.ObjectInput ; 27 import java.io.ObjectOutput ; 28 29 import javax.jdo.JDOFatalInternalException; 30 import javax.jdo.JDONullIdentityException; 31 32 import javax.jdo.spi.I18NHelper; 33 34 41 public abstract class SingleFieldIdentity 42 implements Externalizable { 43 44 46 protected static I18NHelper msg = I18NHelper.getInstance ("javax.jdo.Bundle"); 48 50 transient private Class targetClass; 51 52 54 private String targetClassName; 55 56 58 protected int hashCode; 59 60 62 protected Object keyAsObject; 63 64 68 protected SingleFieldIdentity(Class pcClass) { 69 if (pcClass == null) 70 throw new NullPointerException (); 71 targetClass = pcClass; 72 targetClassName = pcClass.getName(); 73 } 74 75 77 public SingleFieldIdentity () { 78 } 79 80 83 protected void setKeyAsObject(Object key) { 84 assertKeyNotNull(key); 85 keyAsObject = key; 86 } 87 88 91 protected void assertKeyNotNull(Object key) { 92 if (key == null) { 93 throw new JDONullIdentityException( 94 msg.msg("EXC_SingleFieldIdentityNullParameter")); } 96 } 97 98 102 public Class getTargetClass() { 103 return targetClass; 104 } 105 106 110 public String getTargetClassName() { 111 return targetClassName; 112 } 113 114 119 public synchronized Object getKeyAsObject() { 120 if (keyAsObject == null) { 121 keyAsObject = createKeyAsObject(); 122 } 123 return keyAsObject; 124 } 125 126 130 protected Object createKeyAsObject() { 131 throw new JDOFatalInternalException 132 (msg.msg("EXC_CreateKeyAsObjectMustNotBeCalled")); 133 } 134 135 141 public boolean equals(Object obj) { 142 if (this == obj) { 143 return true; 144 } else if (obj == null || this.getClass() != obj.getClass()) { 145 return false; 146 } else { 147 SingleFieldIdentity other = (SingleFieldIdentity) obj; 148 if (targetClass != null && targetClass == other.targetClass) 149 return true; 150 return targetClassName.equals (other.targetClassName); 151 } 152 } 153 154 157 protected int hashClassName() { 158 return targetClassName.hashCode(); 159 } 160 161 164 public int hashCode() { 165 return hashCode; 166 } 167 168 171 public void writeExternal(ObjectOutput out) throws IOException { 172 out.writeObject(targetClassName); 173 out.writeInt(hashCode); 174 } 175 176 180 public void readExternal(ObjectInput in) 181 throws IOException , ClassNotFoundException { 182 targetClass = null; 183 targetClassName = (String )in.readObject(); 184 hashCode = in.readInt(); 185 } 186 } 187 | Popular Tags |