1 11 package org.eclipse.core.internal.indexing; 12 13 import java.util.Observable ; 14 15 public abstract class StoredObject extends Observable implements Referable, Insertable { 16 17 public static final int MAXIMUM_OBJECT_SIZE = ObjectStore.MAXIMUM_OBJECT_SIZE; 18 public static final int TYPE_OFFSET = 0; 19 public static final int TYPE_LENGTH = 2; 20 21 protected ObjectStore store; 22 protected ObjectAddress address; 23 protected int referenceCount; 24 protected int type; 25 26 29 protected StoredObject() { 30 type = getRequiredType(); 31 } 32 33 36 protected StoredObject(Field f, ObjectStore store, ObjectAddress address) throws ObjectStoreException { 37 if (f.length() < getMinimumSize()) { 38 throw new ObjectStoreException(ObjectStoreException.ObjectSizeFailure); 39 } 40 if (f.length() > getMaximumSize()) { 41 throw new ObjectStoreException(ObjectStoreException.ObjectSizeFailure); 42 } 43 extractValues(f); 44 setStore(store); 45 setAddress(address); 46 } 47 48 51 public abstract String toString(); 52 53 57 protected abstract int getRequiredType(); 58 59 62 public final byte[] toByteArray() { 63 Field f = new Field(length()); 64 insertValues(f); 65 return f.get(); 66 } 67 68 71 public final int addReference() { 72 referenceCount++; 73 return referenceCount; 74 } 75 76 79 public final int removeReference() { 80 if (referenceCount > 0) 81 referenceCount--; 82 return referenceCount; 83 } 84 85 88 public final boolean hasReferences() { 89 return referenceCount > 0; 90 } 91 92 96 public final ObjectAddress getAddress() { 97 return address; 98 } 99 100 public final void setStore(ObjectStore store) { 101 this.store = store; 102 } 103 104 public final void setAddress(ObjectAddress address) { 105 this.address = address; 106 } 107 108 112 protected void extractValues(Field f) throws ObjectStoreException { 113 type = f.subfield(TYPE_OFFSET, TYPE_LENGTH).getInt(); 114 if (type != getRequiredType()) 115 throw new ObjectStoreException(ObjectStoreException.ObjectTypeFailure); 116 } 117 118 122 protected void insertValues(Field f) { 123 f.subfield(TYPE_OFFSET, TYPE_LENGTH).put(type); 124 } 125 126 131 protected int getMaximumSize() { 132 return getMinimumSize(); 133 } 134 135 139 protected int getMinimumSize() { 140 return 2; 141 } 142 143 147 protected int length() { 148 return getMinimumSize(); 149 } 150 } 151 | Popular Tags |