01: /**
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */package com.tc.util;
04:
05: import java.util.Arrays;
06:
07: /**
08: * Use me to build an identifier composed of 1 or more AbstactIdentifier instances
09: */
10: public class CompositeIdentifier {
11:
12: private final AbstractIdentifier[] components;
13: private final int hashCode;
14:
15: public CompositeIdentifier(AbstractIdentifier[] components) {
16: Assert.assertNoNullElements(components);
17: this .components = components;
18: this .hashCode = makeHashCode(components);
19: }
20:
21: private int makeHashCode(AbstractIdentifier[] ids) {
22: int rv = 17;
23: for (int i = 0; i < ids.length; i++) {
24: rv += (37 * rv) + ids[i].hashCode();
25: }
26: return rv;
27: }
28:
29: public boolean contains(AbstractIdentifier id) {
30: for (int i = 0; i < components.length; i++) {
31: if (components[i].equals(id)) {
32: return true;
33: }
34: }
35: return false;
36: }
37:
38: public int hashCode() {
39: return this .hashCode;
40: }
41:
42: public boolean equals(Object obj) {
43: if (obj instanceof CompositeIdentifier) {
44: CompositeIdentifier other = (CompositeIdentifier) obj;
45: return Arrays.equals(this .components, other.components);
46: }
47:
48: return false;
49: }
50:
51: public String toString() {
52: StringBuffer buf = new StringBuffer();
53: for (int i = 0; i < components.length; i++) {
54: buf.append(components[i].toString());
55: if (i != components.length - 1) {
56: buf.append(',');
57: }
58: }
59:
60: return buf.toString();
61: }
62:
63: public AbstractIdentifier[] getComponents() {
64: return components;
65: }
66: }
|