1 16 package org.apache.commons.collections.keyvalue; 17 18 import java.io.Serializable ; 19 import java.util.Arrays ; 20 21 47 public class MultiKey implements Serializable { 48 50 51 private static final long serialVersionUID = 4465448607415788805L; 52 53 54 private final Object [] keys; 55 56 private final int hashCode; 57 58 67 public MultiKey(Object key1, Object key2) { 68 this(new Object [] {key1, key2}, false); 69 } 70 71 81 public MultiKey(Object key1, Object key2, Object key3) { 82 this(new Object [] {key1, key2, key3}, false); 83 } 84 85 96 public MultiKey(Object key1, Object key2, Object key3, Object key4) { 97 this(new Object [] {key1, key2, key3, key4}, false); 98 } 99 100 112 public MultiKey(Object key1, Object key2, Object key3, Object key4, Object key5) { 113 this(new Object [] {key1, key2, key3, key4, key5}, false); 114 } 115 116 127 public MultiKey(Object [] keys) { 128 this(keys, true); 129 } 130 131 155 public MultiKey(Object [] keys, boolean makeClone) { 156 super(); 157 if (keys == null) { 158 throw new IllegalArgumentException ("The array of keys must not be null"); 159 } 160 if (makeClone) { 161 this.keys = (Object []) keys.clone(); 162 } else { 163 this.keys = keys; 164 } 165 166 int total = 0; 167 for (int i = 0; i < keys.length; i++) { 168 if (keys[i] != null) { 169 total ^= keys[i].hashCode(); 170 } 171 } 172 hashCode = total; 173 } 174 175 184 public Object [] getKeys() { 185 return (Object []) keys.clone(); 186 } 187 188 199 public Object getKey(int index) { 200 return keys[index]; 201 } 202 203 209 public int size() { 210 return keys.length; 211 } 212 213 223 public boolean equals(Object other) { 224 if (other == this) { 225 return true; 226 } 227 if (other instanceof MultiKey) { 228 MultiKey otherMulti = (MultiKey) other; 229 return Arrays.equals(keys, otherMulti.keys); 230 } 231 return false; 232 } 233 234 244 public int hashCode() { 245 return hashCode; 246 } 247 248 253 public String toString() { 254 return "MultiKey" + Arrays.asList(keys).toString(); 255 } 256 257 } 258 | Popular Tags |