1 package org.apache.torque.om; 2 3 21 22 import java.util.ArrayList ; 23 import org.apache.commons.lang.ObjectUtils; 24 25 35 public class ComboKey extends ObjectKey 36 { 37 40 private static final long serialVersionUID = -264927663211141894L; 41 42 44 45 public static final char SEPARATOR = ':'; 46 47 48 public static final String SEPARATOR_STRING = ":"; 49 50 51 private SimpleKey[] key; 52 53 57 public ComboKey() 58 { 59 } 60 61 67 public ComboKey(SimpleKey[] keys) 68 { 69 setValue(keys); 70 } 71 72 78 public ComboKey(String keys) 79 { 80 setValue(keys); 81 } 82 83 88 public void setValue(SimpleKey[] keys) 89 { 90 this.key = keys; 91 } 92 93 99 public void setValue(String keys) 100 { 101 int startPtr = 0; 102 int indexOfSep = keys.indexOf(SEPARATOR); 103 ArrayList tmpKeys = new ArrayList (); 104 while (indexOfSep != -1) 105 { 106 if (indexOfSep == startPtr) 107 { 108 tmpKeys.add(null); 109 } 110 else 111 { 112 char keyType = keys.charAt(startPtr); 113 String keyString = keys.substring(startPtr + 1, indexOfSep); 114 115 SimpleKey newKey = null; 116 switch(keyType) 117 { 118 case 'N': 119 newKey = new NumberKey(keyString); 120 break; 121 case 'S': 122 newKey = new StringKey(keyString); 123 break; 124 case 'D': 125 try 126 { 127 newKey = new DateKey(keyString); 128 } 129 catch (NumberFormatException nfe) 130 { 131 newKey = new DateKey(); 132 } 133 break; 134 default: 135 } 137 tmpKeys.add(newKey); 138 } 139 startPtr = indexOfSep + 1; 140 indexOfSep = keys.indexOf(SEPARATOR, startPtr); 141 } 142 143 this.key = new SimpleKey[tmpKeys.size()]; 144 for (int i = 0; i < this.key.length; i++) 145 { 146 this.key[i] = (SimpleKey) tmpKeys.get(i); 147 } 148 } 149 150 155 public void setValue(ComboKey keys) 156 { 157 setValue((SimpleKey[]) keys.getValue()); 158 } 159 160 165 public Object getValue() 166 { 167 return key; 168 } 169 170 177 public boolean equals(Object keyObj) 178 { 179 boolean isEqual = false; 180 181 if (key != null) 182 { 183 isEqual = true; 185 SimpleKey[] keys = key; 186 for (int i = 0; i < keys.length && isEqual; i++) 187 { 188 isEqual &= keys[i] != null && keys[i].getValue() != null; 189 } 190 191 isEqual &= looseEquals(keyObj); 192 } 193 194 return isEqual; 195 } 196 197 215 public boolean looseEquals(Object keyObj) 216 { 217 boolean isEqual = false; 218 219 if (key != null) 220 { 221 if (keyObj instanceof String ) 226 { 227 isEqual = toString().equals(keyObj); 228 } 229 else if (keyObj instanceof ComboKey) 232 { 233 SimpleKey[] obj = (SimpleKey[]) 234 ((ComboKey) keyObj).getValue(); 235 236 SimpleKey[] keys1 = key; 237 SimpleKey[] keys2 = obj; 238 isEqual = keys1.length == keys2.length; 239 for (int i = 0; i < keys1.length && isEqual; i++) 240 { 241 isEqual &= ObjectUtils.equals(keys1[i], keys2[i]); 242 } 243 } 244 else if (keyObj instanceof SimpleKey[]) 245 { 246 SimpleKey[] keys1 = key; 247 SimpleKey[] keys2 = (SimpleKey[]) keyObj; 248 isEqual = keys1.length == keys2.length; 249 for (int i = 0; i < keys1.length && isEqual; i++) 250 { 251 isEqual &= ObjectUtils.equals(keys1[i], keys2[i]); 252 } 253 } 254 } 255 return isEqual; 256 } 257 258 263 public void appendTo(StringBuffer sb) 264 { 265 if (key != null) 266 { 267 SimpleKey[] keys = key; 268 for (int i = 0; i < keys.length; i++) 269 { 270 if (keys[i] != null) 271 { 272 if (keys[i] instanceof StringKey) 273 { 274 sb.append("S"); 275 } 276 else if (keys[i] instanceof NumberKey) 277 { 278 sb.append("N"); 279 } 280 else if (keys[i] instanceof DateKey) 281 { 282 sb.append("D"); 283 } 284 else 285 { 286 sb.append("U"); 288 } 289 keys[i].appendTo(sb); 290 } 291 sb.append(SEPARATOR); 293 } 294 } 295 } 296 297 304 public int hashCode() 305 { 306 if (key == null) 307 { 308 return super.hashCode(); 309 } 310 311 SimpleKey sk = key[0]; 312 if (sk == null) 313 { 314 return super.hashCode(); 315 } 316 317 return sk.hashCode(); 318 } 319 320 330 public String toString() 331 { 332 StringBuffer sbuf = new StringBuffer (); 333 appendTo(sbuf); 334 return sbuf.toString(); 335 } 336 } 337 | Popular Tags |