1 8 9 package com.sleepycat.persist.impl; 10 11 import java.io.Serializable ; 12 import java.util.HashSet ; 13 import java.util.IdentityHashMap ; 14 import java.util.List ; 15 import java.util.Map ; 16 import java.util.Set ; 17 18 import com.sleepycat.persist.evolve.Converter; 19 import com.sleepycat.persist.model.ClassMetadata; 20 import com.sleepycat.persist.model.EntityMetadata; 21 import com.sleepycat.persist.raw.RawField; 22 import com.sleepycat.persist.raw.RawObject; 23 import com.sleepycat.persist.raw.RawType; 24 25 236 public abstract class Format implements Reader, RawType, Serializable { 237 238 private static final long serialVersionUID = 545633644568489850L; 239 240 241 static final int ID_NULL = 0; 242 243 static final int ID_OBJECT = 1; 244 245 static final int ID_BOOL = 2; 246 static final int ID_BOOL_W = 3; 247 248 static final int ID_BYTE = 4; 249 static final int ID_BYTE_W = 5; 250 251 static final int ID_SHORT = 6; 252 static final int ID_SHORT_W = 7; 253 254 static final int ID_INT = 8; 255 static final int ID_INT_W = 9; 256 257 static final int ID_LONG = 10; 258 static final int ID_LONG_W = 11; 259 260 static final int ID_FLOAT = 12; 261 static final int ID_FLOAT_W = 13; 262 263 static final int ID_DOUBLE = 14; 264 static final int ID_DOUBLE_W = 15; 265 266 static final int ID_CHAR = 16; 267 static final int ID_CHAR_W = 17; 268 269 static final int ID_STRING = 18; 270 271 static final int ID_BIGINT = 19; 272 273 static final int ID_BIGDEC = 20; 274 275 static final int ID_DATE = 21; 276 277 static final int ID_NUMBER = 22; 278 279 280 static final int ID_SIMPLE_MIN = 2; 281 282 static final int ID_SIMPLE_MAX = 21; 283 284 static final int ID_PREDEFINED = 30; 285 286 static boolean isPredefined(Format format) { 287 return format.getId() <= ID_PREDEFINED; 288 } 289 290 private int id; 291 private String className; 292 private Reader reader; 293 private Format superFormat; 294 private Format latestFormat; 295 private Format previousFormat; 296 private Set <String > supertypes; 297 private boolean deleted; 298 private boolean unused; 299 private transient Catalog catalog; 300 private transient Class type; 301 private transient Format proxiedFormat; 302 private transient boolean initialized; 303 304 307 Format(Class type) { 308 this(type.getName()); 309 this.type = type; 310 addSupertypes(); 311 } 312 313 316 Format(String className) { 317 this.className = className; 318 latestFormat = this; 319 supertypes = new HashSet <String >(); 320 } 321 322 325 void migrateFromBeta(Map <String ,Format> formatMap) { 326 if (latestFormat == null) { 327 latestFormat = this; 328 } 329 } 330 331 final boolean isNew() { 332 return id == 0; 333 } 334 335 final Catalog getCatalog() { 336 return catalog; 337 } 338 339 342 final int getId() { 343 return id; 344 } 345 346 350 final void setId(int id) { 351 this.id = id; 352 } 353 354 358 final Class getType() { 359 return type; 360 } 361 362 366 final Class getExistingType() { 367 if (type == null) { 368 try { 369 type = SimpleCatalog.classForName(className); 370 } catch (ClassNotFoundException e) { 371 throw new IllegalStateException (e); 372 } 373 } 374 return type; 375 } 376 377 382 final Reader getReader() { 383 return reader; 384 } 385 386 389 final void setReader(Reader reader) { 390 this.reader = reader; 391 } 392 393 396 final Format getSuperFormat() { 397 return superFormat; 398 } 399 400 403 final void setSuperFormat(Format superFormat) { 404 this.superFormat = superFormat; 405 } 406 407 411 final Format getProxiedFormat() { 412 return proxiedFormat; 413 } 414 415 418 final void setProxiedFormat(Format proxiedFormat) { 419 this.proxiedFormat = proxiedFormat; 420 } 421 422 427 final Format getLatestVersion() { 428 return latestFormat; 429 } 430 431 435 public final Format getPreviousVersion() { 436 return previousFormat; 437 } 438 439 443 final void setLatestVersion(Format newFormat) { 444 445 450 if (latestFormat == this) { 451 newFormat.previousFormat = this; 452 } 453 454 latestFormat = newFormat; 455 } 456 457 460 final boolean isDeleted() { 461 return deleted; 462 } 463 464 467 final void setDeleted(boolean deleted) { 468 this.deleted = deleted; 469 } 470 471 474 final void setUnused(boolean unused) { 475 this.unused = unused; 476 } 477 478 483 void setEvolveNeeded(boolean needed) { 484 throw new UnsupportedOperationException (); 485 } 486 487 490 boolean getEvolveNeeded() { 491 throw new UnsupportedOperationException (); 492 } 493 494 final boolean isInitialized() { 495 return initialized; 496 } 497 498 505 final void initializeIfNeeded(Catalog catalog) { 506 if (!initialized) { 507 initialized = true; 508 this.catalog = catalog; 509 510 511 if (latestFormat == null) { 512 latestFormat = this; 513 } 514 if (reader == null) { 515 reader = this; 516 } 517 518 522 if (type == null && 523 isCurrentVersion() && 524 (isSimple() || !catalog.isRawAccess())) { 525 getExistingType(); 526 } 527 528 529 initialize(catalog); 530 reader.initializeReader(catalog, this); 531 } 532 } 533 534 538 public void initializeReader(Catalog catalog, Format oldFormat) { 539 } 540 541 544 private void addSupertypes() { 545 addInterfaces(type); 546 Class stype = type.getSuperclass(); 547 while (stype != null && stype != Object .class) { 548 supertypes.add(stype.getName()); 549 addInterfaces(stype); 550 stype = stype.getSuperclass(); 551 } 552 } 553 554 557 private void addInterfaces(Class cls) { 558 Class [] interfaces = cls.getInterfaces(); 559 for (Class iface : interfaces) { 560 if (iface != Enhanced.class) { 561 supertypes.add(iface.getName()); 562 addInterfaces(iface); 563 } 564 } 565 } 566 567 568 569 public String getClassName() { 570 return className; 571 } 572 573 public int getVersion() { 574 ClassMetadata meta = getClassMetadata(); 575 if (meta != null) { 576 return meta.getVersion(); 577 } else { 578 return 0; 579 } 580 } 581 582 public Format getSuperType() { 583 return superFormat; 584 } 585 586 587 588 public boolean isSimple() { 589 return false; 590 } 591 592 public boolean isPrimitive() { 593 return false; 594 } 595 596 public boolean isEnum() { 597 return false; 598 } 599 600 public List <String > getEnumConstants() { 601 return null; 602 } 603 604 public boolean isArray() { 605 return false; 606 } 607 608 public int getDimensions() { 609 return 0; 610 } 611 612 public Format getComponentType() { 613 return null; 614 } 615 616 public Map <String ,RawField> getFields() { 617 return null; 618 } 619 620 621 622 623 624 628 boolean isAssignableTo(Format format) { 629 if (proxiedFormat != null) { 630 return proxiedFormat.isAssignableTo(format); 631 } else { 632 return format == this || 633 format.id == ID_OBJECT || 634 supertypes.contains(format.className); 635 } 636 } 637 638 641 Format getWrapperFormat() { 642 return null; 643 } 644 645 648 boolean isEntity() { 649 return false; 650 } 651 652 656 boolean isModelClass() { 657 return false; 658 } 659 660 664 ClassMetadata getClassMetadata() { 665 return null; 666 } 667 668 672 EntityMetadata getEntityMetadata() { 673 return null; 674 } 675 676 680 Format getEntityFormat() { 681 return null; 682 } 683 684 695 abstract boolean evolve(Format newFormat, Evolver evolver); 696 697 701 boolean evolveMetadata(Format newFormat, 702 Converter converter, 703 Evolver evolver) { 704 return true; 705 } 706 707 711 final boolean isCurrentVersion() { 712 return latestFormat == this && !deleted; 713 } 714 715 719 final boolean isSameClass(Format other) { 720 return latestFormat == other.latestFormat; 721 } 722 723 724 725 729 abstract void initialize(Catalog catalog); 730 731 735 abstract void collectRelatedFormats(Catalog catalog, 736 Map <String ,Format> newFormats); 737 738 750 751 757 abstract Object newArray(int len); 758 759 780 public abstract Object newInstance(EntityInput input, boolean rawAccess); 781 782 792 public abstract Object readObject(Object o, 793 EntityInput input, 794 boolean rawAccess); 795 796 800 abstract void writeObject(Object o, EntityOutput output, boolean rawAccess); 801 802 809 abstract void skipContents(RecordInput input); 810 811 812 813 818 Format skipToSecKey(RecordInput input, String keyName) { 819 throw new UnsupportedOperationException (toString()); 820 } 821 822 826 void copySecKey(RecordInput input, RecordOutput output) { 827 throw new UnsupportedOperationException (toString()); 828 } 829 830 834 void copySecMultiKey(RecordInput input, Format keyFormat, Set results) { 835 throw new UnsupportedOperationException (toString()); 836 } 837 838 842 boolean nullifySecKey(Catalog catalog, 843 Object entity, 844 String keyName, 845 Object keyElement) { 846 throw new UnsupportedOperationException (toString()); 847 } 848 849 853 boolean isPriKeyNullOrZero(Object o, boolean rawAccess) { 854 throw new UnsupportedOperationException (toString()); 855 } 856 857 863 void writePriKey(Object o, EntityOutput output, boolean rawAccess) { 864 throw new UnsupportedOperationException (toString()); 865 } 866 867 876 public void readPriKey(Object o, EntityInput input, boolean rawAccess) { 877 throw new UnsupportedOperationException (toString()); 878 } 879 880 884 Object convertRawObject(Catalog catalog, 885 boolean rawAccess, 886 RawObject rawObject, 887 IdentityHashMap converted) { 888 throw new UnsupportedOperationException (toString()); 889 } 890 891 @Override 892 public String toString() { 893 return "[RawType class: " + getClassName() + 894 " version: " + getVersion() + 895 " internal: " + getClass().getName() + 896 ((reader != null) ? 897 (" reader: " + reader.getClass().getName()) : "") + 898 ']'; 899 } 900 } 901 | Popular Tags |