1 4 package com.tc.util; 5 6 import java.util.Arrays ; 7 8 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 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 toString() { 53 StringBuffer buf = new StringBuffer (); 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 |