1 55 package org.lateralnz.common.model; 56 57 import java.io.Serializable ; 58 59 import org.lateralnz.common.util.Constants; 60 import org.lateralnz.common.util.StringUtils; 61 62 65 public class CompositeKey implements Serializable , Constants { 66 private String primaryKey; 67 private String [] secondaryKeys; 68 private String [] allKeys; 69 70 public CompositeKey(String primaryKey, String [] secondaryKeys) { 71 this.primaryKey = primaryKey; 72 setSecondaryKeys(secondaryKeys); 73 } 74 75 78 public void setSecondaryKeys(String [] secondaryKeys) { 79 this.secondaryKeys = secondaryKeys; 80 if (secondaryKeys == null) { 81 secondaryKeys = new String []{ }; 82 } 83 allKeys = new String [secondaryKeys.length + 1]; 84 allKeys[0] = primaryKey; 85 System.arraycopy(secondaryKeys, 0, allKeys, 1, secondaryKeys.length); 86 } 87 88 91 public String getPrimaryKey() { 92 return primaryKey; 93 } 94 95 98 public String [] getSecondaryKeys() { 99 return secondaryKeys; 100 } 101 102 106 public String [] getAllKeys() { 107 return allKeys; 108 } 109 110 113 public boolean equals(Object obj) { 114 CompositeKey ck = (CompositeKey)obj; 115 int len = getAllKeys().length; 116 if (len != ck.getAllKeys().length) { 117 return false; 118 } 119 else { 120 for (int i = 0; i < len; i++) { 121 if (!StringUtils.isEqual(getAllKeys()[i], ck.getAllKeys()[i])) { 122 return false; 123 } 124 } 125 return true; 126 } 127 } 128 129 public String toString() { 130 StringBuffer sb = new StringBuffer (); 131 int len = getAllKeys().length; 132 int offlen = len - 1; 133 for (int i = 0; i < len; i++) { 134 sb.append(getAllKeys()[i]); 135 if (i < offlen) { 136 sb.append(COMMA); 137 } 138 } 139 return sb.toString(); 140 } 141 } | Popular Tags |