1 21 package oracle.toplink.essentials.internal.sessions; 23 24 import java.io.*; 25 import java.util.*; 26 import oracle.toplink.essentials.internal.helper.*; 27 import oracle.toplink.essentials.exceptions.*; 28 import oracle.toplink.essentials.sessions.Record; 29 import oracle.toplink.essentials.internal.helper.DatabaseField; 30 31 42 public abstract class AbstractRecord implements Record, Cloneable , Serializable, Map { 43 44 45 protected Vector fields; 46 47 48 protected Vector values; 49 50 51 protected DatabaseField lookupField; 52 53 54 public static AbstractRecord.NoEntry noEntry = new AbstractRecord.NoEntry(); 55 56 62 public static class NoEntry { 63 private NoEntry() { 64 } 65 } 66 67 71 public AbstractRecord() { 72 this.fields = new Vector(); 73 this.values = new Vector(); 74 } 75 76 80 public AbstractRecord(int initialCapacity) { 81 this.fields = new Vector(initialCapacity); 82 this.values = new Vector(initialCapacity); 83 } 84 85 89 public AbstractRecord(Vector fields, Vector values) { 90 this.fields = fields; 91 this.values = values; 92 } 93 94 99 public void add(DatabaseField key, Object value) { 100 getFields().addElement(key); 101 getValues().addElement(value); 102 } 103 104 108 public void clear() { 109 this.fields = new Vector(); 110 this.values = new Vector(); 111 } 112 113 117 public Object clone() { 118 try { 119 AbstractRecord clone = (AbstractRecord)super.clone(); 120 clone.setFields((Vector)getFields().clone()); 121 clone.setValues((Vector)getValues().clone()); 122 return clone; 123 } catch (CloneNotSupportedException exception) { 124 } 125 126 return null; 127 } 128 129 133 public boolean contains(Object value) { 134 return containsValue(value); 135 } 136 137 142 public boolean containsKey(Object key) { 143 if (key instanceof String ) { 144 return containsKey((String )key); 145 } 146 if (key instanceof DatabaseField) { 147 return containsKey((DatabaseField)key); 148 } 149 150 return false; 151 } 152 153 157 public boolean containsKey(String fieldName) { 158 if (this.lookupField == null) { 160 this.lookupField = new DatabaseField(fieldName); 161 } else { 162 this.lookupField.resetQualifiedName(fieldName); 163 } 164 return containsKey(this.lookupField); 165 } 166 167 171 public boolean containsKey(DatabaseField key) { 172 int index = key.getIndex(); 174 if ((index >= 0) && (index < getFields().size())) { 175 DatabaseField field = (DatabaseField)getFields().elementAt(index); 176 if ((field == key) || field.equals(key)) { 177 return true; 178 } 179 } 180 return getFields().contains(key); 181 } 182 183 187 public boolean containsValue(Object value) { 188 return getValues().contains(value); 189 } 190 191 195 public Enumeration elements() { 196 return getValues().elements(); 197 } 198 199 203 public Set entrySet() { 204 int size = this.size(); 205 Map tempMap = new HashMap(size); 206 for (int i = 0; i < size; i++) { 207 tempMap.put(this.getFields().elementAt(i), this.getValues().elementAt(i)); 208 } 209 return tempMap.entrySet(); 210 } 211 212 218 public Object get(Object key) { 219 if (key instanceof String ) { 220 return get((String )key); 221 } else if (key instanceof DatabaseField) { 222 return get((DatabaseField)key); 223 } 224 return null; 225 } 226 227 233 public Object get(String fieldName) { 234 Object value = getIndicatingNoEntry(fieldName); 235 if (value == oracle.toplink.essentials.internal.sessions.AbstractRecord.noEntry) { 236 return null; 237 } 238 return value; 239 } 240 241 247 public Object getIndicatingNoEntry(String fieldName) { 248 if (this.lookupField == null) { 250 this.lookupField = new DatabaseField(fieldName); 251 } else { 252 this.lookupField.resetQualifiedName(fieldName); 253 } 254 return getIndicatingNoEntry(this.lookupField); 255 } 256 257 261 public Object get(DatabaseField key) { 262 Object value = getIndicatingNoEntry(key); 263 if (value == oracle.toplink.essentials.internal.sessions.AbstractRecord.noEntry) { 264 return null; 265 } 266 return value; 267 } 268 269 public Object getValues(DatabaseField key) { 271 return get(key); 272 } 273 274 public Object getValues(String key) { 275 return get(key); 276 } 277 278 280 284 public Object getIndicatingNoEntry(DatabaseField key) { 285 int index = key.getIndex(); 288 if ((index >= 0) && (index < this.fields.size())) { 289 DatabaseField field = (DatabaseField)this.fields.elementAt(index); 290 if ((field == key) || field.equals(key)) { 291 return this.values.elementAt(index); 292 } 293 } 294 index = this.fields.indexOf(key); 295 if (index >= 0) { 296 if (key.getIndex() == -1) { 298 key.setIndex(index); 299 } 300 return this.values.elementAt(index); 301 } else { 302 return oracle.toplink.essentials.internal.sessions.AbstractRecord.noEntry; 303 } 304 } 305 306 310 public DatabaseField getField(DatabaseField key) { 311 int index = key.getIndex(); 313 if ((index >= 0) && (index < getFields().size())) { 314 DatabaseField field = (DatabaseField)getFields().elementAt(index); 315 if ((field == key) || field.equals(key)) { 316 return field; 317 } 318 } 319 for (index = 0; index < getFields().size(); index++) { 320 DatabaseField field = (DatabaseField)getFields().elementAt(index); 321 if ((field == key) || field.equals(key)) { 322 return field; 323 } 324 } 325 return null; 326 } 327 328 331 public Vector getFields() { 332 return fields; 333 } 334 335 338 public Vector getValues() { 339 return values; 340 } 341 342 346 public boolean isEmpty() { 347 return size() == 0; 348 } 349 350 354 public Enumeration keys() { 355 return getFields().elements(); 356 } 357 358 362 public Set keySet() { 363 return new HashSet(getFields()); 364 } 365 366 372 373 public void mergeFrom(AbstractRecord row){ 374 for (int index = 0; index < row.size(); ++index){ 375 this.put(row.getFields().get(index), row.getValues().get(index)); 376 } 377 } 378 379 383 public Object put(Object key, Object value) throws ValidationException { 384 if (key instanceof String ) { 385 return put((String )key, value); 386 } else if (key instanceof DatabaseField) { 387 return put((DatabaseField)key, value); 388 } else { 389 throw ValidationException.onlyFieldsAreValidKeysForDatabaseRows(); 390 } 391 } 392 393 397 public Object put(String key, Object value) { 398 return put(new DatabaseField(key), value); 399 } 400 401 405 public Object put(DatabaseField key, Object value) { 406 int index = getFields().indexOf(key); 407 if (index >= 0) { 408 Object oldValue = getValues().elementAt(index); 409 replaceAt(value, index); 410 return oldValue; 411 } else { 412 add(key, value); 413 } 414 415 return null; 416 } 417 418 422 public void putAll(Map map) { 423 Iterator entriesIterator = map.entrySet().iterator(); 424 while (entriesIterator.hasNext()) { 425 Map.Entry entry = (Map.Entry)entriesIterator.next(); 426 put(entry.getKey(), entry.getValue()); 427 } 428 } 429 430 434 public Object remove(Object key) { 435 if (key instanceof String ) { 436 return remove((String )key); 437 } else if (key instanceof DatabaseField) { 438 return remove((DatabaseField)key); 439 } 440 return null; 441 } 442 443 447 public Object remove(String fieldName) { 448 return remove(new DatabaseField(fieldName)); 449 } 450 451 455 public Object remove(DatabaseField key) { 456 int index = getFields().indexOf(key); 457 if (index >= 0) { 458 getFields().removeElementAt(index); 459 Object value = getValues().elementAt(index); 460 getValues().removeElementAt(index); 461 return value; 462 } 463 return null; 464 } 465 466 470 public void replaceAt(Object value, int index) { 471 getValues().setElementAt(value, index); 472 } 473 474 protected void setFields(Vector fields) { 475 this.fields = fields; 476 } 477 478 protected void setValues(Vector values) { 479 this.values = values; 480 } 481 482 486 public int size() { 487 return getFields().size(); 488 } 489 490 493 public String toString() { 494 StringWriter writer = new StringWriter(); 495 writer.write(Helper.getShortClassName(getClass())); 496 writer.write("("); 497 498 for (int index = 0; index < getFields().size(); index++) { 499 writer.write(Helper.cr()); 500 writer.write("\t"); 501 writer.write(String.valueOf((getFields().elementAt(index)))); 502 writer.write(" => "); 503 writer.write(String.valueOf((getValues().elementAt(index)))); 504 } 505 writer.write(")"); 506 507 return writer.toString(); 508 } 509 510 514 public Collection values() { 515 return getValues(); 516 } 517 } 518 | Popular Tags |