KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > CompositeIdentifier


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util;
5
6 import java.util.Arrays JavaDoc;
7
8 /**
9  * Use me to build an identifier composed of 1 or more AbstactIdentifier instances
10  */

11 public class CompositeIdentifier {
12
13   private final AbstractIdentifier[] components;
14   private final int hashCode;
15
16   public CompositeIdentifier(AbstractIdentifier[] components) {
17     Assert.assertNoNullElements(components);
18     this.components = components;
19     this.hashCode = makeHashCode(components);
20   }
21
22   private int makeHashCode(AbstractIdentifier[] ids) {
23     int rv = 17;
24     for (int i = 0; i < ids.length; i++) {
25       rv += (37 * rv) + ids[i].hashCode();
26     }
27     return rv;
28   }
29
30   public boolean contains(AbstractIdentifier id) {
31     for (int i = 0; i < components.length; i++) {
32       if (components[i].equals(id)) {
33         return true;
34       }
35     }
36     return false;
37   }
38
39   public int hashCode() {
40     return this.hashCode;
41   }
42
43   public boolean equals(Object JavaDoc obj) {
44     if (obj instanceof CompositeIdentifier) {
45       CompositeIdentifier other = (CompositeIdentifier) obj;
46       return Arrays.equals(this.components, other.components);
47     }
48
49     return false;
50   }
51
52   public String JavaDoc toString() {
53     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
54     for (int i = 0; i < components.length; i++) {
55       buf.append(components[i].toString());
56       if (i != components.length - 1) {
57         buf.append(',');
58       }
59     }
60
61     return buf.toString();
62   }
63
64   public AbstractIdentifier[] getComponents() {
65     return components;
66   }
67 }
68
Popular Tags