1 23 24 package org.objectweb.jorm.runtime.gcindex; 25 26 import org.objectweb.jorm.api.PGenClassAccessor; 27 import org.objectweb.jorm.api.PIndexedElem; 28 import org.objectweb.jorm.api.PException; 29 import org.objectweb.jorm.type.api.PType; 30 import org.objectweb.jorm.naming.api.PName; 31 32 import java.util.Iterator ; 33 import java.util.Date ; 34 import java.util.Map ; 35 import java.util.HashMap ; 36 import java.io.Serializable ; 37 import java.math.BigDecimal ; 38 import java.math.BigInteger ; 39 40 43 public class GcIndexAccessor implements PGenClassAccessor { 44 private String [] stringarray_value; 45 private Map map_value; 46 private int indexTypeCode, nbElem; 47 48 public GcIndexAccessor(int tc, int nbelem) throws PException { 49 indexTypeCode = tc; 50 nbElem = nbelem; 51 switch (indexTypeCode) { 52 case PType.TYPECODE_DATE: 53 case PType.TYPECODE_STRING: 54 map_value = new HashMap (); 55 break; 56 case PType.TYPECODE_BYTE: 57 case PType.TYPECODE_CHAR: 58 case PType.TYPECODE_SHORT: 59 case PType.TYPECODE_INT: 60 case PType.TYPECODE_LONG: 61 case PType.TYPECODE_OBJBYTE: 62 case PType.TYPECODE_OBJCHAR: 63 case PType.TYPECODE_OBJSHORT: 64 case PType.TYPECODE_OBJINT: 65 case PType.TYPECODE_OBJLONG: 66 stringarray_value = new String [nbelem]; 67 break; 68 default: 69 throw new PException("Wrong index type."); 70 } 71 } 72 73 public void setElem(int index, String o) { 74 stringarray_value[index] = o; 75 } 76 77 public void setElem(String index, String o) { 78 map_value.put(index, o); 79 } 80 81 public void setElem(Date index, String o) { 82 map_value.put(new Long (index.getTime()), o); 83 } 84 85 public boolean equals(Object other) { 86 switch (indexTypeCode) { 87 case PType.TYPECODE_DATE: 88 case PType.TYPECODE_STRING: 89 return map_value.equals(((GcIndexAccessor) other).map_value); 90 default: 91 if (((GcIndexAccessor) other).stringarray_value.length != stringarray_value.length) { 92 return false; 93 } 94 for (int i = 0; i < ((GcIndexAccessor) other).stringarray_value.length; i++) { 95 if (stringarray_value[i] == ((GcIndexAccessor) other).stringarray_value[i]) { 96 continue; 97 } 98 if ((((GcIndexAccessor) other).stringarray_value[i] == null) 99 || (stringarray_value[i] == null)) { 100 return false; 101 } 102 if (!((GcIndexAccessor) other).stringarray_value[i].equals(stringarray_value[i])) { 103 return false; 104 } 105 } 106 return true; 107 } 108 } 109 110 112 public void paAdd(PIndexedElem elem, Object conn) throws PException { 113 switch (indexTypeCode) { 114 case PType.TYPECODE_DATE: 115 map_value.put(new Long (elem.pieGetDateIndexField("index").getTime()), elem.pieGetStringElem()); 116 break; 117 case PType.TYPECODE_STRING: 118 map_value.put(elem.pieGetStringIndexField("index"), elem.pieGetStringElem()); 119 break; 120 case PType.TYPECODE_BYTE: 121 stringarray_value[elem.pieGetByteIndexField("index")] = elem.pieGetStringElem(); 122 break; 123 case PType.TYPECODE_CHAR: 124 stringarray_value[elem.pieGetCharIndexField("index") - ' ' - 1] = elem.pieGetStringElem(); 125 break; 126 case PType.TYPECODE_SHORT: 127 stringarray_value[elem.pieGetShortIndexField("index")] = elem.pieGetStringElem(); 128 break; 129 case PType.TYPECODE_INT: 130 stringarray_value[elem.pieGetIntIndexField("index")] = elem.pieGetStringElem(); 131 break; 132 case PType.TYPECODE_LONG: 133 stringarray_value[(int) elem.pieGetLongIndexField("index")] = elem.pieGetStringElem(); 134 break; 135 case PType.TYPECODE_OBJBYTE: 136 stringarray_value[elem.pieGetObyteIndexField("index").intValue()] = elem.pieGetStringElem(); 137 break; 138 case PType.TYPECODE_OBJCHAR: 139 stringarray_value[elem.pieGetOcharIndexField("index").charValue() - ' ' - 1] = elem.pieGetStringElem(); 140 break; 141 case PType.TYPECODE_OBJSHORT: 142 stringarray_value[elem.pieGetOshortIndexField("index").intValue()] = elem.pieGetStringElem(); 143 break; 144 case PType.TYPECODE_OBJINT: 145 stringarray_value[elem.pieGetOintIndexField("index").intValue()] = elem.pieGetStringElem(); 146 break; 147 case PType.TYPECODE_OBJLONG: 148 stringarray_value[elem.pieGetOlongIndexField("index").intValue()] = elem.pieGetStringElem(); 149 break; 150 default: 151 } 152 } 153 154 public boolean paDeltaSupported() { 155 return false; 156 } 157 158 public int paGetNbElem() { 159 return nbElem; 160 } 161 162 public Iterator paIterator() { 163 return new GcIterator(this); 164 } 165 166 public void paSetNbElem(int nbelem) { 167 } 168 169 171 public Object getMemoryInstance() { 172 return this; 173 } 174 175 177 public PIndexedElem createPIndexedElem() throws PException { 178 return new IStringIndexedElem(indexTypeCode); 179 } 180 181 class GcIterator implements Iterator { 183 private int nextToRead = 0; 184 private GcIndexAccessor theGC; 185 private Iterator mapIter; 186 187 GcIterator(GcIndexAccessor gc) { 188 theGC = gc; 189 switch (theGC.indexTypeCode) { 190 case PType.TYPECODE_DATE: 191 case PType.TYPECODE_STRING: 192 mapIter = theGC.map_value.keySet().iterator(); 193 break; 194 default: 195 } 196 } 197 198 public boolean hasNext() { 199 switch (theGC.indexTypeCode) { 200 case PType.TYPECODE_DATE: 201 case PType.TYPECODE_STRING: 202 return mapIter.hasNext(); 203 default: 204 return nextToRead < theGC.nbElem; 205 } 206 } 207 208 public Object next() { 209 if (!hasNext()) { 210 return null; 211 } 212 try { 213 PIndexedElem pie = theGC.createPIndexedElem(); 214 switch (theGC.indexTypeCode) { 215 case PType.TYPECODE_DATE: 216 Long ld = (Long ) mapIter.next(); 217 pie.pieSetDateIndexField("index", new Date (ld.longValue())); 218 pie.pieSetStringElem((String ) theGC.map_value.get(ld)); 219 break; 220 case PType.TYPECODE_STRING: 221 pie.pieSetStringIndexField("index", (String ) mapIter.next()); 222 pie.pieSetStringElem((String ) theGC.map_value.get(pie.pieGetStringIndexField("index"))); 223 break; 224 case PType.TYPECODE_BYTE: 225 pie.pieSetByteIndexField("index", (byte) nextToRead); 226 pie.pieSetStringElem(theGC.stringarray_value[nextToRead]); 227 break; 228 case PType.TYPECODE_CHAR: 229 pie.pieSetCharIndexField("index", (char) (nextToRead + ' ' + 1)); 230 pie.pieSetStringElem(theGC.stringarray_value[nextToRead]); 231 break; 232 case PType.TYPECODE_SHORT: 233 pie.pieSetShortIndexField("index", (short) nextToRead); 234 pie.pieSetStringElem(theGC.stringarray_value[nextToRead]); 235 break; 236 case PType.TYPECODE_INT: 237 pie.pieSetIntIndexField("index", nextToRead); 238 pie.pieSetStringElem(theGC.stringarray_value[nextToRead]); 239 break; 240 case PType.TYPECODE_LONG: 241 pie.pieSetLongIndexField("index", nextToRead); 242 pie.pieSetStringElem(theGC.stringarray_value[nextToRead]); 243 break; 244 case PType.TYPECODE_OBJBYTE: 245 pie.pieSetObyteIndexField("index", new Byte ((byte) nextToRead)); 246 pie.pieSetStringElem(theGC.stringarray_value[nextToRead]); 247 break; 248 case PType.TYPECODE_OBJCHAR: 249 pie.pieSetOcharIndexField("index", new Character ((char) (nextToRead + ' ' + 1))); 250 pie.pieSetStringElem(theGC.stringarray_value[nextToRead]); 251 break; 252 case PType.TYPECODE_OBJSHORT: 253 pie.pieSetOshortIndexField("index", new Short ((short) nextToRead)); 254 pie.pieSetStringElem(theGC.stringarray_value[nextToRead]); 255 break; 256 case PType.TYPECODE_OBJINT: 257 pie.pieSetOintIndexField("index", new Integer (nextToRead)); 258 pie.pieSetStringElem(theGC.stringarray_value[nextToRead]); 259 break; 260 case PType.TYPECODE_OBJLONG: 261 pie.pieSetOlongIndexField("index", new Long (nextToRead)); 262 pie.pieSetStringElem(theGC.stringarray_value[nextToRead]); 263 break; 264 } 265 nextToRead++; 266 return pie; 267 } catch (PException e) { 268 e.printStackTrace(System.out); } 270 return null; 271 } 272 273 public void remove() { 274 } 275 } 276 } 277 278 class IStringIndexedElem implements PIndexedElem { 279 private byte byte_index; 280 private char char_index; 281 private short short_index; 282 private int int_index; 283 private long long_index; 284 private Object object_index; 285 private int typeCode; 286 private String value; 287 288 public IStringIndexedElem(int typecode) throws PException { 289 typeCode = typecode; 290 } 291 292 public byte getElemStatus() { 293 return 0; 294 } 295 296 public boolean pieGetBooleanElem() throws PException { 297 throw new PException("Wrong type!!"); 298 } 299 300 public Boolean pieGetObooleanElem() throws PException { 301 throw new PException("Wrong type!!"); 302 } 303 304 public byte pieGetByteElem() throws PException { 305 throw new PException("Wrong type!!"); 306 } 307 308 public Byte pieGetObyteElem() throws PException { 309 throw new PException("Wrong type!!"); 310 } 311 312 public byte pieGetByteIndexField(String fn) throws PException { 313 if (typeCode != PType.TYPECODE_BYTE) { 314 throw new PException("Wrong index type."); 315 } 316 if (fn.equals("index")) { 317 return byte_index; 318 } 319 throw new PException("Unknown index name."); 320 } 321 322 public Byte pieGetObyteIndexField(String fn) throws PException { 323 if (typeCode != PType.TYPECODE_OBJBYTE) { 324 throw new PException("Wrong index type."); 325 } 326 if (fn.equals("index")) { 327 return (Byte ) object_index; 328 } 329 throw new PException("Unknown index name."); 330 } 331 332 public char pieGetCharElem() throws PException { 333 throw new PException("Wrong type!!"); 334 } 335 336 public Character pieGetOcharElem() throws PException { 337 throw new PException("Wrong type!!"); 338 } 339 340 public char pieGetCharIndexField(String fn) throws PException { 341 if (typeCode != PType.TYPECODE_CHAR) { 342 throw new PException("Wrong index type."); 343 } 344 if (fn.equals("index")) { 345 return char_index; 346 } 347 throw new PException("Unknown index name."); 348 } 349 350 public Character pieGetOcharIndexField(String fn) throws PException { 351 if (typeCode != PType.TYPECODE_OBJCHAR) { 352 throw new PException("Wrong index type."); 353 } 354 if (fn.equals("index")) { 355 return (Character ) object_index; 356 } 357 throw new PException("Unknown index name."); 358 } 359 360 public short pieGetShortElem() throws PException { 361 throw new PException("Wrong type!!"); 362 } 363 364 public Short pieGetOshortElem() throws PException { 365 throw new PException("Wrong type!!"); 366 } 367 368 public short pieGetShortIndexField(String fn) throws PException { 369 if (typeCode != PType.TYPECODE_SHORT) { 370 throw new PException("Wrong index type."); 371 } 372 if (fn.equals("index")) { 373 return short_index; 374 } 375 throw new PException("Unknown index name."); 376 } 377 378 public Short pieGetOshortIndexField(String fn) throws PException { 379 if (typeCode != PType.TYPECODE_OBJSHORT) { 380 throw new PException("Wrong index type."); 381 } 382 if (fn.equals("index")) { 383 return (Short ) object_index; 384 } 385 throw new PException("Unknown index name."); 386 } 387 388 public int pieGetIntElem() throws PException { 389 throw new PException("Wrong type!!"); 390 } 391 392 public Integer pieGetOintElem() throws PException { 393 throw new PException("Wrong type!!"); 394 } 395 396 public int pieGetIntIndexField(String fn) throws PException { 397 if (typeCode != PType.TYPECODE_INT) { 398 throw new PException("Wrong index type."); 399 } 400 if (fn.equals("index")) { 401 return int_index; 402 } 403 throw new PException("Unknown index name."); 404 } 405 406 public Integer pieGetOintIndexField(String fn) throws PException { 407 if (typeCode != PType.TYPECODE_OBJINT) { 408 throw new PException("Wrong index type."); 409 } 410 if (fn.equals("index")) { 411 return (Integer ) object_index; 412 } 413 throw new PException("Unknown index name."); 414 } 415 416 public long pieGetLongElem() throws PException { 417 throw new PException("Wrong type!!"); 418 } 419 420 public Long pieGetOlongElem() throws PException { 421 throw new PException("Wrong type!!"); 422 } 423 424 public long pieGetLongIndexField(String fn) throws PException { 425 if (typeCode != PType.TYPECODE_LONG) { 426 throw new PException("Wrong index type."); 427 } 428 if (fn.equals("index")) { 429 return long_index; 430 } 431 throw new PException("Unknown index name."); 432 } 433 434 public Long pieGetOlongIndexField(String fn) throws PException { 435 if (typeCode != PType.TYPECODE_OBJLONG) { 436 throw new PException("Wrong index type."); 437 } 438 if (fn.equals("index")) { 439 return (Long ) object_index; 440 } 441 throw new PException("Unknown index name."); 442 } 443 444 public float pieGetFloatElem() throws PException { 445 throw new PException("Wrong type!!"); 446 } 447 448 public Float pieGetOfloatElem() throws PException { 449 throw new PException("Wrong type!!"); 450 } 451 452 public double pieGetDoubleElem() throws PException { 453 throw new PException("Wrong type!!"); 454 } 455 456 public Double pieGetOdoubleElem() throws PException { 457 throw new PException("Wrong type!!"); 458 } 459 460 public String pieGetStringElem() throws PException { 461 return value; 462 } 463 464 public String pieGetStringIndexField(String fn) throws PException { 465 if (typeCode != PType.TYPECODE_STRING) { 466 throw new PException("Wrong index type."); 467 } 468 if (fn.equals("index")) { 469 return (String ) object_index; 470 } 471 throw new PException("Unknown index name."); 472 } 473 474 public Date pieGetDateElem() throws PException { 475 throw new PException("Wrong type!!"); 476 } 477 478 public Date pieGetDateIndexField(String fn) throws PException { 479 if (typeCode != PType.TYPECODE_DATE) { 480 throw new PException("Wrong index type."); 481 } 482 if (fn.equals("index")) { 483 return (Date ) object_index; 484 } 485 throw new PException("Unknown index name."); 486 } 487 488 public char[] pieGetCharArrayElem() throws PException { 489 throw new PException("Wrong type!!"); 490 } 491 492 public byte[] pieGetByteArrayElem() throws PException { 493 throw new PException("Wrong type!!"); 494 } 495 496 public Serializable pieGetSerializedElem() throws PException { 497 throw new PException("Wrong type!!"); 498 } 499 500 public BigDecimal pieGetBigDecimalElem() throws PException { 501 throw new PException("Wrong type!!"); 502 } 503 504 public BigInteger pieGetBigIntegerElem() throws PException { 505 throw new PException("Wrong type!!"); 506 } 507 508 public PName pieGetRefElem() throws PException { 509 throw new PException("Wrong type!!"); 510 } 511 512 public void pieSetBooleanElem(boolean value) throws PException { 513 throw new PException("Wrong type!!"); 514 } 515 516 public void pieSetObooleanElem(Boolean value) throws PException { 517 throw new PException("Wrong type!!"); 518 } 519 520 public void pieSetByteElem(byte value) throws PException { 521 throw new PException("Wrong type!!"); 522 } 523 524 public void pieSetObyteElem(Byte value) throws PException { 525 throw new PException("Wrong type!!"); 526 } 527 528 public void pieSetByteIndexField(String fn, byte value) throws PException { 529 if (typeCode != PType.TYPECODE_BYTE) { 530 throw new PException("Wrong index type."); 531 } 532 if (fn.equals("index")) { 533 byte_index = value; 534 return; 535 } 536 throw new PException("Unknown index name."); 537 } 538 539 public void pieSetObyteIndexField(String fn, Byte value) throws PException { 540 if (typeCode != PType.TYPECODE_OBJBYTE) { 541 throw new PException("Wrong index type."); 542 } 543 if (fn.equals("index")) { 544 object_index = value; 545 return; 546 } 547 throw new PException("Unknown index name."); 548 } 549 550 public void pieSetCharElem(char value) throws PException { 551 throw new PException("Wrong type!!"); 552 } 553 554 public void pieSetOcharElem(Character value) throws PException { 555 throw new PException("Wrong type!!"); 556 } 557 558 public void pieSetCharIndexField(String fn, char value) throws PException { 559 if (typeCode != PType.TYPECODE_CHAR) { 560 throw new PException("Wrong index type."); 561 } 562 if (fn.equals("index")) { 563 char_index = value; 564 return; 565 } 566 throw new PException("Unknown index name."); 567 } 568 569 public void pieSetOcharIndexField(String fn, Character value) throws PException { 570 if (typeCode != PType.TYPECODE_OBJCHAR) { 571 throw new PException("Wrong index type."); 572 } 573 if (fn.equals("index")) { 574 object_index = value; 575 return; 576 } 577 throw new PException("Unknown index name."); 578 } 579 580 public void pieSetShortElem(short value) throws PException { 581 throw new PException("Wrong type!!"); 582 } 583 584 public void pieSetOshortElem(Short value) throws PException { 585 throw new PException("Wrong type!!"); 586 } 587 588 public void pieSetShortIndexField(String fn, short value) throws PException { 589 if (typeCode != PType.TYPECODE_SHORT) { 590 throw new PException("Wrong index type."); 591 } 592 if (fn.equals("index")) { 593 short_index = value; 594 return; 595 } 596 throw new PException("Unknown index name."); 597 } 598 599 public void pieSetOshortIndexField(String fn, Short value) throws PException { 600 if (typeCode != PType.TYPECODE_OBJSHORT) { 601 throw new PException("Wrong index type."); 602 } 603 if (fn.equals("index")) { 604 object_index = value; 605 return; 606 } 607 throw new PException("Unknown index name."); 608 } 609 610 public void pieSetIntElem(int value) throws PException { 611 throw new PException("Wrong type!!"); 612 } 613 614 public void pieSetOintElem(Integer value) throws PException { 615 throw new PException("Wrong type!!"); 616 } 617 618 public void pieSetIntIndexField(String fn, int value) throws PException { 619 if (typeCode != PType.TYPECODE_INT) { 620 throw new PException("Wrong index type."); 621 } 622 if (fn.equals("index")) { 623 int_index = value; 624 return; 625 } 626 throw new PException("Unknown index name."); 627 } 628 629 public void pieSetOintIndexField(String fn, Integer value) throws PException { 630 if (typeCode != PType.TYPECODE_OBJINT) { 631 throw new PException("Wrong index type."); 632 } 633 if (fn.equals("index")) { 634 object_index = value; 635 return; 636 } 637 throw new PException("Unknown index name."); 638 } 639 640 public void pieSetLongElem(long value) throws PException { 641 throw new PException("Wrong type!!"); 642 } 643 644 public void pieSetOlongElem(Long value) throws PException { 645 throw new PException("Wrong type!!"); 646 } 647 648 public void pieSetLongIndexField(String fn, long value) throws PException { 649 if (typeCode != PType.TYPECODE_LONG) { 650 throw new PException("Wrong index type."); 651 } 652 if (fn.equals("index")) { 653 long_index = value; 654 return; 655 } 656 throw new PException("Unknown index name."); 657 } 658 659 public void pieSetOlongIndexField(String fn, Long value) throws PException { 660 if (typeCode != PType.TYPECODE_OBJLONG) { 661 throw new PException("Wrong index type."); 662 } 663 if (fn.equals("index")) { 664 object_index = value; 665 return; 666 } 667 throw new PException("Unknown index name."); 668 } 669 670 public void pieSetFloatElem(float value) throws PException { 671 throw new PException("Wrong type!!"); 672 } 673 674 public void pieSetOfloatElem(Float value) throws PException { 675 throw new PException("Wrong type!!"); 676 } 677 678 public void pieSetDoubleElem(double value) throws PException { 679 throw new PException("Wrong type!!"); 680 } 681 682 public void pieSetOdoubleElem(Double value) throws PException { 683 throw new PException("Wrong type!!"); 684 } 685 686 public void pieSetStringElem(String value) throws PException { 687 this.value = value; 688 } 689 690 public void pieSetStringIndexField(String fn, String value) throws PException { 691 if (typeCode != PType.TYPECODE_STRING) { 692 throw new PException("Wrong index type."); 693 } 694 if (fn.equals("index")) { 695 object_index = value; 696 return; 697 } 698 throw new PException("Unknown index name."); 699 } 700 701 public void pieSetDateElem(Date value) throws PException { 702 throw new PException("Wrong type!!"); 703 } 704 705 public void pieSetDateIndexField(String fn, Date value) throws PException { 706 if (typeCode != PType.TYPECODE_DATE) { 707 throw new PException("Wrong index type."); 708 } 709 if (fn.equals("index")) { 710 object_index = value; 711 return; 712 } 713 throw new PException("Unknown index name."); 714 } 715 716 public void pieSetCharArrayElem(char[] value) throws PException { 717 throw new PException("Wrong type!!"); 718 } 719 720 public void pieSetByteArrayElem(byte[] value) throws PException { 721 throw new PException("Wrong type!!"); 722 } 723 724 public void pieSetSerializedElem(Serializable value) throws PException { 725 throw new PException("Wrong type!!"); 726 } 727 728 public void pieSetBigDecimalElem(BigDecimal value) throws PException { 729 throw new PException("Wrong type!!"); 730 } 731 732 public void pieSetBigIntegerElem(BigInteger value) throws PException { 733 throw new PException("Wrong type!!"); 734 } 735 736 public void pieSetRefElem(PName value) throws PException { 737 throw new PException("Wrong type!!"); 738 } 739 } 740 | Popular Tags |