1 18 package org.objectweb.speedo.genclass.collection; 19 20 import java.io.Serializable ; 21 import java.math.BigDecimal ; 22 import java.math.BigInteger ; 23 import java.util.Date ; 24 25 import javax.jdo.PersistenceManager; 26 27 import org.objectweb.jorm.api.PException; 28 import org.objectweb.jorm.api.PIndexedElem; 29 import org.objectweb.jorm.naming.api.PName; 30 import org.objectweb.jorm.naming.api.PNamingContext; 31 import org.objectweb.jorm.type.api.PExceptionTyping; 32 import org.objectweb.speedo.genclass.GenClassAccessor; 33 import org.objectweb.speedo.genclass.GenClassElement; 34 import org.objectweb.speedo.mim.api.SpeedoAccessor; 35 import org.objectweb.speedo.mim.api.SpeedoProxy; 36 import org.objectweb.util.monolog.api.BasicLevel; 37 import org.objectweb.util.monolog.api.Logger; 38 39 42 public class CollectionElem implements PIndexedElem, GenClassElement { 43 44 49 public final static String ERROR_MESSAGE_BAD_FIELD_NAME = 50 "Wrong index field name"; 51 52 57 public final static String ERROR_MESSAGE_NO_NULL_INDEX = 58 "Null value not supported for index field"; 59 60 64 public final static String INDEX_FIELD_NAME = "idx"; 65 66 69 protected Object element; 70 71 75 protected Object index; 76 77 81 protected byte status = ELEM_CREATED; 82 83 protected GenClassAccessor gca; 84 85 public CollectionElem(GenClassAccessor gca) { 86 this.gca = gca; 87 } 88 89 90 public GenClassElement cloneGCE() { 91 return cloneGCE(new CollectionElem(gca)); 92 } 93 94 public GenClassElement cloneGCE(GenClassElement gce) { 95 ((CollectionElem) gce).status = status; 96 ((CollectionElem) gce).element = element; 97 ((CollectionElem) gce).index = index; 98 return gce; 99 } 100 101 104 public Object getIndex() { 105 return index; 106 } 107 108 public void setIndex(Object index) { 109 this.index = index; 110 } 111 112 115 public synchronized Object getElement() { 116 return getElement(null); 117 } 118 119 123 public void setElement(Object element) { 124 this.element = element; 125 } 126 127 133 public Object getElement(PersistenceManager pm) { 134 Object res = element; 135 if (res instanceof PName) { 136 synchronized(this) { 137 if (res instanceof PName) { 138 if (pm == null) { 139 pm = gca.jdoProxy.jdoGetPersistenceManager(); 140 } 141 res = pm.getObjectById(res, false); 142 } 143 if (status != PIndexedElem.ELEM_DELETED) { 144 element = res; 148 } 149 } 150 } 151 return res; 152 } 153 154 public SpeedoAccessor getSpeedoAccessor() { 155 return gca; 156 } 157 158 public void unSwizzle() { 159 if (element instanceof SpeedoProxy) { 160 synchronized(this) { 161 if (element instanceof SpeedoProxy) { 162 element = ((SpeedoProxy) element).getPName(); 163 } 164 } 165 } 166 } 167 168 byte statusForMerge = PIndexedElem.ELEM_UNMODIFIED; 169 170 public void cleanStatusForMerge() { 171 statusForMerge = PIndexedElem.ELEM_UNMODIFIED; 172 } 173 public byte getStatusForMerge() { 174 return statusForMerge; 175 } 176 public byte retainStatusForMerge() { 177 statusForMerge = status; 178 return statusForMerge; 179 } 180 181 184 public byte getElemStatus() { 185 return status; 186 } 187 188 public String pieGetStringElem() throws PExceptionTyping { 189 return (String ) element; 190 } 191 192 public Date pieGetDateElem() throws PExceptionTyping { 193 return (Date ) element; 194 } 195 196 public char[] pieGetCharArrayElem() throws PExceptionTyping { 197 return (char[]) element; 198 } 199 200 public byte[] pieGetByteArrayElem() throws PExceptionTyping { 201 return (byte[]) element; 202 } 203 204 public Serializable pieGetSerializedElem() throws PExceptionTyping { 205 return (Serializable ) element; 206 } 207 208 public PName pieGetRefElem() { 209 PName pn = (PName) (element instanceof SpeedoProxy 210 ? ((SpeedoProxy) element).getPName() 211 : element); 212 try { 213 pn = ((PNamingContext) gca.getSpeedoProxy().getPClassMapping() 214 .getPNameCoder()).export(null, pn); 215 } catch (Exception e) { 216 } 217 return pn; 218 } 219 220 public boolean pieGetBooleanElem() throws PExceptionTyping { 221 return ((Boolean ) element).booleanValue(); 222 } 223 224 public Boolean pieGetObooleanElem() throws PExceptionTyping { 225 return (Boolean ) element; 226 } 227 228 public byte pieGetByteElem() throws PExceptionTyping { 229 return ((Byte ) element).byteValue(); 230 } 231 232 public Byte pieGetObyteElem() throws PExceptionTyping { 233 return (Byte ) element; 234 } 235 236 public char pieGetCharElem() throws PExceptionTyping { 237 return ((Character ) element).charValue(); 238 } 239 240 public Character pieGetOcharElem() throws PExceptionTyping { 241 return (Character ) element; 242 } 243 244 public short pieGetShortElem() throws PExceptionTyping { 245 return ((Short ) element).shortValue(); 246 } 247 248 public Short pieGetOshortElem() throws PExceptionTyping { 249 return (Short ) element; 250 } 251 252 public int pieGetIntElem() throws PExceptionTyping { 253 return ((Integer ) element).intValue(); 254 } 255 256 public Integer pieGetOintElem() throws PExceptionTyping { 257 return (Integer ) element; 258 } 259 260 public long pieGetLongElem() throws PExceptionTyping { 261 return ((Long ) element).longValue(); 262 } 263 264 public Long pieGetOlongElem() throws PExceptionTyping { 265 return (Long ) element; 266 } 267 268 public float pieGetFloatElem() throws PExceptionTyping { 269 return ((Float ) element).floatValue(); 270 } 271 272 public Float pieGetOfloatElem() throws PExceptionTyping { 273 return (Float ) element; 274 } 275 276 public double pieGetDoubleElem() throws PExceptionTyping { 277 return ((Double ) element).doubleValue(); 278 } 279 280 public Double pieGetOdoubleElem() throws PExceptionTyping { 281 return (Double ) element; 282 } 283 284 public BigDecimal pieGetBigDecimalElem() throws PException { 285 return (BigDecimal ) element; 286 } 287 288 public BigInteger pieGetBigIntegerElem() throws PException { 289 return (BigInteger ) element; 290 } 291 292 public void pieSetStringElem(String value) throws PExceptionTyping { 293 element = value; 294 } 295 296 public void pieSetDateElem(Date value) throws PExceptionTyping { 297 element = value; 298 } 299 300 public void pieSetCharArrayElem(char[] value) throws PExceptionTyping { 301 element = value; 302 } 303 304 public void pieSetByteArrayElem(byte[] value) throws PExceptionTyping { 305 element = value; 306 } 307 308 public void pieSetSerializedElem(Serializable value) throws PExceptionTyping { 309 element = value; 310 } 311 312 public void pieSetRefElem(PName value) throws PExceptionTyping { 313 element = value; 314 324 } 325 326 public void pieSetBooleanElem(boolean value) throws PExceptionTyping { 327 element = new Boolean (value); 328 } 329 330 public void pieSetObooleanElem(Boolean value) throws PExceptionTyping { 331 element = value; 332 } 333 334 public void pieSetByteElem(byte value) throws PExceptionTyping { 335 element = new Byte (value); 336 } 337 338 public void pieSetObyteElem(Byte value) throws PExceptionTyping { 339 element = value; 340 } 341 342 public void pieSetCharElem(char value) throws PExceptionTyping { 343 element = new Character (value); 344 } 345 346 public void pieSetOcharElem(Character value) throws PExceptionTyping { 347 element = value; 348 } 349 350 public void pieSetShortElem(short value) throws PExceptionTyping { 351 element = new Short (value); 352 } 353 354 public void pieSetOshortElem(Short value) throws PExceptionTyping { 355 element = value; 356 } 357 358 public void pieSetIntElem(int value) throws PExceptionTyping { 359 element = new Integer (value); 360 } 361 362 public void pieSetOintElem(Integer value) throws PExceptionTyping { 363 element = value; 364 } 365 366 public void pieSetLongElem(long value) throws PExceptionTyping { 367 element = new Long (value); 368 } 369 370 public void pieSetOlongElem(Long value) throws PExceptionTyping { 371 element = value; 372 } 373 374 public void pieSetFloatElem(float value) throws PExceptionTyping { 375 element = new Float (value); 376 } 377 378 public void pieSetOfloatElem(Float value) throws PExceptionTyping { 379 element = value; 380 } 381 382 public void pieSetDoubleElem(double value) throws PExceptionTyping { 383 element = new Double (value); 384 } 385 386 public void pieSetOdoubleElem(Double value) throws PExceptionTyping { 387 element = value; 388 } 389 390 public void pieSetBigDecimalElem(BigDecimal bigDecimal) throws PException { 391 element = bigDecimal; 392 } 393 394 public void pieSetBigIntegerElem(BigInteger value) throws PException { 395 element = value; 396 } 397 401 public void pieSetByteIndexField(String fn, byte value) throws PExceptionTyping { 402 if (!INDEX_FIELD_NAME.equals(fn)) 403 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 404 index = new Byte (value); 405 } 406 407 public void pieSetObyteIndexField(String fn, Byte value) throws PExceptionTyping { 408 if (!INDEX_FIELD_NAME.equals(fn)) 409 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 410 if (value == null) 411 throw new PExceptionTyping(ERROR_MESSAGE_NO_NULL_INDEX); 412 index = value; 413 } 414 415 public void pieSetCharIndexField(String fn, char value) throws PExceptionTyping { 416 if (!INDEX_FIELD_NAME.equals(fn)) 417 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 418 index = new Character (value); 419 } 420 421 public void pieSetOcharIndexField(String fn, Character value) throws PExceptionTyping { 422 if (!INDEX_FIELD_NAME.equals(fn)) 423 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 424 if (value == null) 425 throw new PExceptionTyping(ERROR_MESSAGE_NO_NULL_INDEX); 426 index = value; 427 } 428 429 public void pieSetShortIndexField(String fn, short value) throws PExceptionTyping { 430 if (!INDEX_FIELD_NAME.equals(fn)) 431 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 432 index = new Short (value); 433 } 434 435 public void pieSetOshortIndexField(String fn, Short value) throws PExceptionTyping { 436 if (!INDEX_FIELD_NAME.equals(fn)) 437 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 438 if (value == null) 439 throw new PExceptionTyping(ERROR_MESSAGE_NO_NULL_INDEX); 440 index = value; 441 } 442 443 public void pieSetIntIndexField(String fn, int value) throws PExceptionTyping { 444 if (!INDEX_FIELD_NAME.equals(fn)) 445 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 446 index = new Integer (value); 447 } 448 449 public void pieSetOintIndexField(String fn, Integer value) throws PExceptionTyping { 450 if (!INDEX_FIELD_NAME.equals(fn)) 451 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 452 if (value == null) 453 throw new PExceptionTyping(ERROR_MESSAGE_NO_NULL_INDEX); 454 index = value; 455 } 456 457 public void pieSetLongIndexField(String fn, long value) throws PExceptionTyping { 458 if (!INDEX_FIELD_NAME.equals(fn)) 459 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 460 index = new Long (value); 461 } 462 463 public void pieSetOlongIndexField(String fn, Long value) throws PExceptionTyping { 464 if (!INDEX_FIELD_NAME.equals(fn)) 465 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 466 if (value == null) 467 throw new PExceptionTyping(ERROR_MESSAGE_NO_NULL_INDEX); 468 index = value; 469 } 470 471 public void pieSetStringIndexField(String fn, String value) throws PExceptionTyping { 472 if (!INDEX_FIELD_NAME.equals(fn)) 473 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 474 if (value == null) 475 throw new PExceptionTyping(ERROR_MESSAGE_NO_NULL_INDEX); 476 index = value; 477 } 478 479 public void pieSetDateIndexField(String fn, Date value) throws PExceptionTyping { 480 if (!INDEX_FIELD_NAME.equals(fn)) 481 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 482 if (value == null) 483 throw new PExceptionTyping(ERROR_MESSAGE_NO_NULL_INDEX); 484 index = value; 485 } 486 487 public short pieGetShortIndexField(String fn) throws PExceptionTyping { 488 if (!INDEX_FIELD_NAME.equals(fn)) 489 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 490 return ((Short ) index).shortValue(); 491 } 492 493 public Short pieGetOshortIndexField(String fn) throws PExceptionTyping { 494 if (!INDEX_FIELD_NAME.equals(fn)) 495 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 496 return (Short ) index; 497 } 498 499 public long pieGetLongIndexField(String fn) throws PExceptionTyping { 500 if (!INDEX_FIELD_NAME.equals(fn)) 501 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 502 return ((Long ) index).longValue(); 503 } 504 505 public Long pieGetOlongIndexField(String fn) throws PExceptionTyping { 506 if (!INDEX_FIELD_NAME.equals(fn)) 507 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 508 return (Long ) index; 509 } 510 511 public int pieGetIntIndexField(String fn) throws PExceptionTyping { 512 if (!INDEX_FIELD_NAME.equals(fn)) 513 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 514 return ((Integer ) index).intValue(); 515 } 516 517 public Integer pieGetOintIndexField(String fn) throws PExceptionTyping { 518 if (!INDEX_FIELD_NAME.equals(fn)) 519 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 520 return (Integer ) index; 521 } 522 523 public String pieGetStringIndexField(String fn) throws PExceptionTyping { 524 if (!INDEX_FIELD_NAME.equals(fn)) 525 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 526 return (String ) index; 527 } 528 529 public Date pieGetDateIndexField(String fn) throws PExceptionTyping { 530 if (!INDEX_FIELD_NAME.equals(fn)) 531 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 532 return (Date ) index; 533 } 534 535 public byte pieGetByteIndexField(String fn) throws PExceptionTyping { 536 if (!INDEX_FIELD_NAME.equals(fn)) 537 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 538 return ((Byte ) index).byteValue(); 539 } 540 541 public Byte pieGetObyteIndexField(String fn) throws PExceptionTyping { 542 if (!INDEX_FIELD_NAME.equals(fn)) 543 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 544 return (Byte ) index; 545 } 546 547 public char pieGetCharIndexField(String fn) throws PExceptionTyping { 548 if (!INDEX_FIELD_NAME.equals(fn)) 549 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 550 return ((Character ) index).charValue(); 551 } 552 553 public Character pieGetOcharIndexField(String fn) throws PExceptionTyping { 554 if (!INDEX_FIELD_NAME.equals(fn)) 555 throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME); 556 return (Character ) index; 557 } 558 559 563 public void setStatus(byte s) { 564 status = s; 565 if (status == PIndexedElem.ELEM_DELETED) { 566 unSwizzle(); 571 } 572 } 573 574 public boolean equals(Object o) { 575 if (!(o instanceof CollectionElem)) { 576 return false; 577 } 578 Object _index = ((CollectionElem) o).getIndex(); 579 if ((index == null && _index != null) || 580 (index != null && !index.equals(_index))) { 581 return false; 582 } 583 Object _element = element; 586 Object ceElem = ((CollectionElem) o).element; 590 if (_element == null) { 591 return ceElem == null; 592 } 593 if (ceElem == null) { 594 return false; 595 } 596 if (_element instanceof PName) { 597 if (ceElem instanceof PName) { 598 return _element.equals(ceElem); 599 } else if (ceElem instanceof SpeedoProxy) { 600 return _element.equals(((SpeedoProxy) ceElem).getPName()); 601 } else { 602 return false; 603 } 604 } else if (_element instanceof SpeedoProxy) { 605 if (ceElem instanceof PName) { 606 Object pn = ((SpeedoProxy) _element).getPName(); 607 if (pn == null) { 608 Logger l = gca.getLogger(); 609 if (l != null) { 610 l.log(BasicLevel.WARN, 611 "Internal error: a collection/set/map elemnent is no more persistent and it is impossible to compare it: ", 612 new Exception ()); 613 } 614 return false; 615 } else { 616 return pn.equals(ceElem); 617 } 618 } else if (ceElem instanceof SpeedoProxy) { 619 return _element == ceElem; 620 } else { 621 return false; 622 } 623 } else { 624 return _element.equals(ceElem); 625 } 626 } 627 } 628 | Popular Tags |