1 15 package org.apache.tapestry.util; 16 17 import java.io.Externalizable ; 18 import java.io.IOException ; 19 import java.io.ObjectInput ; 20 import java.io.ObjectOutput ; 21 22 import org.apache.tapestry.Tapestry; 23 24 31 32 public class MultiKey implements Externalizable 33 { 34 38 39 private static final long serialVersionUID = 4465448607415788806L; 40 41 private static final int HASH_CODE_UNSET = -1; 42 43 private transient int hashCode = HASH_CODE_UNSET; 44 45 private Object [] keys; 46 47 53 54 public MultiKey() 55 { 56 } 57 58 70 71 public MultiKey(Object [] keys, boolean makeCopy) 72 { 73 super(); 74 75 if (keys == null || keys.length == 0) 76 throw new IllegalArgumentException (Tapestry.getMessage("MultiKey.null-keys")); 77 78 if (keys[0] == null) 79 throw new IllegalArgumentException (Tapestry.getMessage("MultiKey.first-element-may-not-be-null")); 80 81 if (makeCopy) 82 { 83 this.keys = new Object [keys.length]; 84 System.arraycopy(keys, 0, this.keys, 0, keys.length); 85 } 86 else 87 this.keys = keys; 88 } 89 90 99 100 public boolean equals(Object other) 101 { 102 int i; 103 104 if (other == null) 105 return false; 106 107 if (keys == null) 108 throw new IllegalStateException (Tapestry.getMessage("MultiKey.no-keys")); 109 110 112 try 113 { 114 MultiKey otherMulti = (MultiKey) other; 115 116 if (keys.length != otherMulti.keys.length) 117 return false; 118 119 for (i = 0; i < keys.length; i++) 120 { 121 124 if (keys[i] == otherMulti.keys[i]) 125 continue; 126 127 130 if (keys[i] == null || otherMulti.keys[i] == null) 131 return false; 132 133 if (!keys[i].equals(otherMulti.keys[i])) 134 return false; 135 136 } 137 138 140 return true; 141 } 142 catch (ClassCastException e) 143 { 144 } 145 146 return false; 147 } 148 149 160 161 public int hashCode() 162 { 163 if (hashCode == HASH_CODE_UNSET) 164 { 165 hashCode = keys[0].hashCode(); 166 167 for (int i = 1; i < keys.length; i++) 168 { 169 if (keys[i] != null) 170 hashCode ^= keys[i].hashCode(); 171 } 172 } 173 174 return hashCode; 175 } 176 177 181 182 public String toString() 183 { 184 StringBuffer buffer; 185 int i; 186 187 buffer = new StringBuffer ("MultiKey["); 188 189 for (i = 0; i < keys.length; i++) 190 { 191 if (i > 0) 192 buffer.append(", "); 193 194 if (keys[i] == null) 195 buffer.append("<null>"); 196 else 197 buffer.append(keys[i]); 198 } 199 200 buffer.append(']'); 201 202 return buffer.toString(); 203 } 204 205 209 210 public void writeExternal(ObjectOutput out) throws IOException 211 { 212 out.writeInt(keys.length); 213 214 for (int i = 0; i < keys.length; i++) 215 out.writeObject(keys[i]); 216 } 217 218 222 223 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 224 { 225 int count; 226 227 count = in.readInt(); 228 keys = new Object [count]; 229 230 for (int i = 0; i < count; i++) 231 keys[i] = in.readObject(); 232 } 233 } | Popular Tags |